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 Psr\Log\LoggerInterface; |
22
|
|
|
use SAML2\Response; |
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 SecondFactorRespondService |
32
|
|
|
{ |
33
|
|
|
/** @var LoggerInterface */ |
34
|
|
|
private $logger; |
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 LoggerInterface $logger |
54
|
|
|
* @param LoaResolutionService $loaResolutionService |
55
|
|
|
* @param LoaAliasLookupService $loaAliasLookupService |
56
|
|
|
* @param ResponseFactory $responseFactory |
57
|
|
|
* @param SecondFactorService $secondFactorService |
58
|
|
|
* @param SecondFactorTypeService $secondFactorTypeService |
59
|
|
|
*/ |
60
|
|
|
public function __construct( |
61
|
|
|
LoggerInterface $logger, |
62
|
|
|
LoaResolutionService $loaResolutionService, |
63
|
|
|
LoaAliasLookupService $loaAliasLookupService, |
64
|
|
|
ResponseFactory $responseFactory, |
65
|
|
|
SecondFactorService $secondFactorService, |
66
|
|
|
SecondFactorTypeService $secondFactorTypeService |
67
|
|
|
) { |
68
|
|
|
$this->logger = $logger; |
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
|
|
|
$this->logger->notice('Creating second-factor-only Response'); |
90
|
|
|
|
91
|
|
|
$selectedSecondFactorUuid = $responseContext->getSelectedSecondFactor(); |
92
|
|
|
if (!$selectedSecondFactorUuid) { |
93
|
|
|
throw new InvalidSecondFactorMethodException( |
94
|
|
|
'Cannot verify possession of an unknown second factor.' |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$responseContext->isSecondFactorVerified()) { |
99
|
|
|
throw new InvalidSecondFactorMethodException( |
100
|
|
|
'Second factor was not verified' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$secondFactor = $this->secondFactorService->findByUuid($selectedSecondFactorUuid); |
105
|
|
|
$grantedLoa = $this->loaResolutionService |
106
|
|
|
->getLoaByLevel($secondFactor->getLoaLevel($this->secondFactorTypeService)); |
107
|
|
|
|
108
|
|
|
$authnContextClassRef = $this->loaAliasLookupService->findAliasByLoa($grantedLoa); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$response = $this->responseFactory->createSecondFactorOnlyResponse( |
111
|
|
|
$responseContext->getIdentityNameId(), |
112
|
|
|
$responseContext->getDestination(), |
113
|
|
|
$authnContextClassRef |
|
|
|
|
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->logger->notice(sprintf( |
117
|
|
|
'Responding to request "%s" with newly created response "%s"', |
118
|
|
|
$responseContext->getInResponseTo(), |
119
|
|
|
$response->getId() |
120
|
|
|
)); |
121
|
|
|
|
122
|
|
|
return $response; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Reset the state of the response |
127
|
|
|
* |
128
|
|
|
* @param ResponseContext $responseContext |
129
|
|
|
*/ |
130
|
|
|
public function resetRespondState(ResponseContext $responseContext) |
131
|
|
|
{ |
132
|
|
|
$responseContext->responseSent(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: