|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2021 SURFnet B.V. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller; |
|
22
|
|
|
|
|
23
|
|
|
use Exception; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use Surfnet\SamlBundle\Entity\IdentityProvider; |
|
26
|
|
|
use Surfnet\SamlBundle\Entity\ServiceProvider; |
|
27
|
|
|
use Surfnet\SamlBundle\Http\PostBinding; |
|
28
|
|
|
use Surfnet\SamlBundle\Http\RedirectBinding; |
|
29
|
|
|
use Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger; |
|
30
|
|
|
use Surfnet\SamlBundle\SAML2\Response\Assertion\InResponseTo; |
|
31
|
|
|
use Surfnet\StepupBundle\Service\LoaResolutionService; |
|
32
|
|
|
use Surfnet\StepupBundle\Service\SecondFactorTypeService; |
|
33
|
|
|
use Surfnet\StepupBundle\Value\Loa; |
|
34
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
|
35
|
|
|
use Surfnet\StepupBundle\Value\VettingType; |
|
36
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SelfVetCommand; |
|
37
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\AuthorizationService; |
|
38
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService; |
|
39
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfVetMarshaller; |
|
40
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\TestSecondFactor\TestAuthenticationRequestFactory; |
|
|
|
|
|
|
41
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Value\SelfVetRequestId; |
|
42
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
43
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
44
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
45
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
46
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
47
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
48
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
49
|
|
|
use function sprintf; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) - Controllers are prone to higher coupling. This one is no exception |
|
53
|
|
|
* TODO: Split up into smaller controllers |
|
54
|
|
|
*/ |
|
|
|
|
|
|
55
|
|
|
class SelfVetController extends AbstractController |
|
56
|
|
|
{ |
|
57
|
|
|
final public const SELF_VET_SESSION_ID = 'second_factor_self_vet_request_id'; |
|
58
|
|
|
|
|
59
|
|
|
public function __construct( |
|
|
|
|
|
|
60
|
|
|
private readonly LoggerInterface $logger, |
|
61
|
|
|
private readonly TestAuthenticationRequestFactory $authenticationRequestFactory, |
|
62
|
|
|
private readonly SecondFactorService $secondFactorService, |
|
63
|
|
|
private readonly SecondFactorTypeService $secondFactorTypeService, |
|
64
|
|
|
private readonly SelfVetMarshaller $selfVetMarshaller, |
|
65
|
|
|
private readonly AuthorizationService $authorizationService, |
|
66
|
|
|
private readonly ServiceProvider $serviceProvider, |
|
67
|
|
|
private readonly IdentityProvider $identityProvider, |
|
68
|
|
|
private readonly RedirectBinding $redirectBinding, |
|
69
|
|
|
private readonly PostBinding $postBinding, |
|
70
|
|
|
private readonly LoaResolutionService $loaResolutionService, |
|
71
|
|
|
private readonly SamlAuthenticationLogger $samlAuthenticationLogger, |
|
72
|
|
|
private readonly RequestStack $requestStack, |
|
73
|
|
|
) { |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
#[Route( |
|
77
|
|
|
path: '/second-factor/{secondFactorId}/self-vet-consume-assertion', |
|
78
|
|
|
name: 'ss_second_factor_self_vet_consume_assertion', |
|
79
|
|
|
methods: ['POST'], |
|
80
|
|
|
)] |
|
81
|
|
|
public function consumeSelfVetAssertion(Request $httpRequest, string $secondFactorId): RedirectResponse |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$identity = $this->getUser()->getIdentity(); |
|
|
|
|
|
|
84
|
|
|
if (!$this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) { |
|
85
|
|
|
throw $this->createNotFoundException(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$this->requestStack->getSession()->has(self::SELF_VET_SESSION_ID)) { |
|
89
|
|
|
$this->logger->error( |
|
90
|
|
|
'Received an authentication response for self vetting a second factor, but no response was expected' |
|
91
|
|
|
); |
|
92
|
|
|
throw new AccessDeniedHttpException('Did not expect an authentication response'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$this->logger->notice('Received an authentication response for self vetting a second factor'); |
|
96
|
|
|
|
|
97
|
|
|
/** @var SelfVetRequestId $initiatedRequestId */ |
|
|
|
|
|
|
98
|
|
|
$initiatedRequestId = $this->requestStack->getSession()->get(self::SELF_VET_SESSION_ID); |
|
99
|
|
|
|
|
100
|
|
|
$samlLogger = $this->samlAuthenticationLogger->forAuthentication($initiatedRequestId->requestId()); |
|
101
|
|
|
|
|
102
|
|
|
$this->requestStack->getSession()->remove(self::SELF_VET_SESSION_ID); |
|
103
|
|
|
|
|
104
|
|
|
try { |
|
105
|
|
|
$assertion = $this->postBinding->processResponse( |
|
106
|
|
|
$httpRequest, |
|
107
|
|
|
$this->identityProvider, |
|
108
|
|
|
$this->serviceProvider |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
if (!InResponseTo::assertEquals($assertion, $initiatedRequestId->requestId())) { |
|
112
|
|
|
$samlLogger->error( |
|
113
|
|
|
sprintf( |
|
114
|
|
|
'Expected a response to the request with ID "%s", but the SAMLResponse was a response to a different request', |
|
115
|
|
|
$initiatedRequestId |
|
|
|
|
|
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
throw new AuthenticationException('Unexpected InResponseTo in SAMLResponse'); |
|
119
|
|
|
} |
|
120
|
|
|
$candidateSecondFactor = $this->secondFactorService->findOneVerified($secondFactorId); |
|
121
|
|
|
// Proof of possession of higher/equal LoA was successful, now apply the self vet command on Middleware |
|
122
|
|
|
$command = new SelfVetCommand(); |
|
123
|
|
|
$command->identity = $this->getUser()->getIdentity(); |
|
124
|
|
|
$command->secondFactor = $candidateSecondFactor; |
|
125
|
|
|
$command->authoringLoa = $assertion->getAuthnContextClassRef(); |
|
126
|
|
|
|
|
127
|
|
|
if ($this->secondFactorService->selfVet($command)) { |
|
128
|
|
|
$this->addFlash('success', 'ss.self_vet.second_factor.alert.successful'); |
|
129
|
|
|
} else { |
|
130
|
|
|
$this->addFlash('error', 'ss.self_vet.second_factor.alert.failed'); |
|
131
|
|
|
} |
|
132
|
|
|
} catch (Exception) { |
|
133
|
|
|
$this->addFlash('error', 'ss.self_vet.second_factor.verification_failed'); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->redirectToRoute('ss_second_factor_list'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
#[Route( |
|
139
|
|
|
path: '/second-factor/{secondFactorId}/self-vet', |
|
140
|
|
|
name: 'ss_second_factor_self_vet', |
|
141
|
|
|
methods: ['GET'], |
|
142
|
|
|
)] |
|
143
|
|
|
public function selfVet(string $secondFactorId): RedirectResponse |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$this->logger->notice('Starting self vet proof of possession using higher or equal LoA token'); |
|
146
|
|
|
$identity = $this->getUser()->getIdentity(); |
|
147
|
|
|
|
|
148
|
|
|
if (!$this->selfVetMarshaller->isAllowed($identity, $secondFactorId)) { |
|
149
|
|
|
throw $this->createNotFoundException(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
// Start with some assumptions that are overwritten with the correct values in the code below |
|
154
|
|
|
$candidateSecondFactorLoa = $this->loaResolutionService->getLoaByLevel(Loa::LOA_SELF_VETTED); |
|
|
|
|
|
|
155
|
|
|
$isSelfVetOfSatToken = false; |
|
156
|
|
|
|
|
157
|
|
|
// Determine if we are dealing with a SelfVet action of a SAT token |
|
158
|
|
|
if ($this->authorizationService->maySelfVetSelfAssertedTokens($identity)) { |
|
159
|
|
|
$this->logger->notice('Determined we are self vetting a token using a self-asserted token'); |
|
160
|
|
|
$isSelfVetOfSatToken = true; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
// When a regular self-vet action is performed grab the candidate second factor loa from the SF projection |
|
164
|
|
|
if (!$isSelfVetOfSatToken) { |
|
165
|
|
|
$this->logger->notice('Determined we are self vetting a token using an identity vetted token'); |
|
166
|
|
|
$candidateSecondFactor = $this->secondFactorService->findOneVerified($secondFactorId); |
|
167
|
|
|
$candidateSecondFactorLoa = $this->secondFactorTypeService->getLevel( |
|
168
|
|
|
new SecondFactorType($candidateSecondFactor->type), |
|
169
|
|
|
new VettingType(VettingType::TYPE_SELF_VET) |
|
170
|
|
|
); |
|
171
|
|
|
$candidateSecondFactorLoa = $this->loaResolutionService->getLoaByLevel($candidateSecondFactorLoa); |
|
172
|
|
|
} |
|
173
|
|
|
$this->logger->notice( |
|
174
|
|
|
sprintf( |
|
175
|
|
|
'Creating AuthNRequest requiring a LoA %s or higher token for self vetting.', |
|
176
|
|
|
$candidateSecondFactorLoa |
|
177
|
|
|
) |
|
178
|
|
|
); |
|
179
|
|
|
$authenticationRequest = $this->authenticationRequestFactory->createSecondFactorTestRequest( |
|
180
|
|
|
$identity->nameId, |
|
181
|
|
|
$candidateSecondFactorLoa |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
$this->requestStack->getSession()->set( |
|
185
|
|
|
self::SELF_VET_SESSION_ID, |
|
186
|
|
|
new SelfVetRequestId($authenticationRequest->getRequestId(), $secondFactorId) |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
$samlLogger = $this->samlAuthenticationLogger->forAuthentication($authenticationRequest->getRequestId()); |
|
190
|
|
|
$samlLogger->notice('Sending authentication request to the second factor only IdP'); |
|
191
|
|
|
|
|
192
|
|
|
return $this->redirectBinding->createResponseFor($authenticationRequest); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|