1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\Gateway; |
20
|
|
|
|
21
|
|
|
use SAML2\Response; |
22
|
|
|
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger; |
23
|
|
|
use Surfnet\StepupBundle\Service\SecondFactorTypeService; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
25
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService; |
26
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Exception\InvalidSecondFactorMethodException; |
27
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Saml\ResponseFactory; |
28
|
|
|
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Service\LoaAliasLookupService; |
29
|
|
|
use Surfnet\StepupBundle\Service\LoaResolutionService; |
30
|
|
|
|
31
|
|
|
class RespondService |
32
|
|
|
{ |
33
|
|
|
/** @var SamlAuthenticationLogger */ |
34
|
|
|
private $samlLogger; |
35
|
|
|
|
36
|
|
|
/** @var LoaResolutionService */ |
37
|
|
|
private $loaResolutionService; |
38
|
|
|
|
39
|
|
|
/** @var LoaAliasLookupService */ |
40
|
|
|
private $loaAliasLookupService; |
41
|
|
|
|
42
|
|
|
/** @var ResponseFactory */ |
43
|
|
|
private $responseFactory; |
44
|
|
|
|
45
|
|
|
/** @var SecondFactorService */ |
46
|
|
|
private $secondFactorService; |
47
|
|
|
|
48
|
|
|
/** @var SecondFactorTypeService */ |
49
|
|
|
private $secondFactorTypeService; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* SecondFactorRespondService constructor. |
53
|
|
|
* @param SamlAuthenticationLogger $samlLogger |
54
|
|
|
* @param LoaResolutionService $loaResolutionService |
55
|
|
|
* @param LoaAliasLookupService $loaAliasLookupService |
56
|
|
|
* @param ResponseFactory $responseFactory |
57
|
|
|
* @param SecondFactorService $secondFactorService |
58
|
|
|
* @param SecondFactorTypeService $secondFactorTypeService |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
61
|
|
|
SamlAuthenticationLogger $samlLogger, |
62
|
|
|
LoaResolutionService $loaResolutionService, |
63
|
|
|
LoaAliasLookupService $loaAliasLookupService, |
64
|
|
|
ResponseFactory $responseFactory, |
65
|
|
|
SecondFactorService $secondFactorService, |
66
|
|
|
SecondFactorTypeService $secondFactorTypeService |
67
|
|
|
) { |
68
|
|
|
$this->samlLogger = $samlLogger; |
69
|
|
|
$this->loaResolutionService = $loaResolutionService; |
70
|
|
|
$this->loaAliasLookupService = $loaAliasLookupService; |
71
|
|
|
$this->responseFactory = $responseFactory; |
72
|
|
|
$this->secondFactorService = $secondFactorService; |
73
|
|
|
$this->secondFactorTypeService = $secondFactorTypeService; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Send a SAML response back to the service provider. |
79
|
|
|
* |
80
|
|
|
* Second factor verification is finished. This method builds a AuthnResponse |
81
|
|
|
* to send back to the service provider in response to the AuthnRequest received in |
82
|
|
|
* the SecondFactorLoginService. |
83
|
|
|
* |
84
|
|
|
* @param ResponseContext $responseContext |
85
|
|
|
* @return Response |
86
|
|
|
*/ |
87
|
|
|
public function respond(ResponseContext $responseContext) |
88
|
|
|
{ |
89
|
|
|
$originalRequestId = $responseContext->getInResponseTo(); |
90
|
|
|
$logger = $this->samlLogger->forAuthentication($originalRequestId); |
91
|
|
|
|
92
|
|
|
$logger->notice('Creating second-factor-only Response'); |
93
|
|
|
|
94
|
|
|
$selectedSecondFactorUuid = $responseContext->getSelectedSecondFactor(); |
95
|
|
|
if (!$selectedSecondFactorUuid) { |
96
|
|
|
throw new InvalidSecondFactorMethodException( |
97
|
|
|
'Cannot verify possession of an unknown second factor.' |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if (!$responseContext->isSecondFactorVerified()) { |
102
|
|
|
throw new InvalidSecondFactorMethodException( |
103
|
|
|
'Second factor was not verified' |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$secondFactor = $this->secondFactorService->findByUuid($selectedSecondFactorUuid); |
108
|
|
|
$grantedLoa = $this->loaResolutionService |
109
|
|
|
->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService)); |
110
|
|
|
|
111
|
|
|
$authnContextClassRef = $this->loaAliasLookupService->findAliasByLoa($grantedLoa); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$response = $this->responseFactory->createSecondFactorOnlyResponse( |
114
|
|
|
$responseContext->getIdentityNameId(), |
115
|
|
|
$responseContext->getDestination(), |
116
|
|
|
$authnContextClassRef |
|
|
|
|
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$logger->notice(sprintf( |
120
|
|
|
'Responding to request "%s" with newly created response "%s"', |
121
|
|
|
$responseContext->getInResponseTo(), |
122
|
|
|
$response->getId() |
123
|
|
|
)); |
124
|
|
|
|
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Reset the state of the response |
130
|
|
|
* |
131
|
|
|
* @param ResponseContext $responseContext |
132
|
|
|
*/ |
133
|
|
|
public function resetRespondState(ResponseContext $responseContext) |
134
|
|
|
{ |
135
|
|
|
$responseContext->responseSent(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.