1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 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\GatewayBundle\Controller; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
23
|
|
|
use Surfnet\StepupBundle\Command\VerifyPossessionOfPhoneCommand; |
24
|
|
|
use Surfnet\StepupBundle\Value\PhoneNumber\InternationalPhoneNumber; |
25
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
26
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Command\ChooseSecondFactorCommand; |
27
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Command\SendSmsChallengeCommand; |
28
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Command\VerifyYubikeyOtpCommand; |
29
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor; |
30
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException; |
31
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\LoaCannotBeGivenException; |
32
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException; |
33
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\CancelAuthenticationType; |
34
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\CancelSecondFactorVerificationType; |
35
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\ChooseSecondFactorType; |
36
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\SendSmsChallengeType; |
37
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\VerifySmsChallengeType; |
38
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Form\Type\VerifyYubikeyOtpType; |
39
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Saml\ResponseContext; |
40
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Value\KeyHandle; |
41
|
|
|
use Surfnet\StepupU2fBundle\Dto\SignResponse; |
42
|
|
|
use Surfnet\StepupU2fBundle\Form\Type\VerifyDeviceAuthenticationType; |
43
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
44
|
|
|
use Symfony\Component\Form\Form; |
45
|
|
|
use Symfony\Component\Form\FormError; |
46
|
|
|
use Symfony\Component\Form\FormInterface; |
47
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
48
|
|
|
use Symfony\Component\HttpFoundation\Request; |
49
|
|
|
use Symfony\Component\HttpFoundation\Response; |
50
|
|
|
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; |
51
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
55
|
|
|
*/ |
56
|
|
|
class SecondFactorController extends Controller |
57
|
|
|
{ |
58
|
|
|
const MODE_SFO = 'sfo'; |
59
|
|
|
const MODE_SSO = 'sso'; |
60
|
|
|
|
61
|
|
|
public function selectSecondFactorForVerificationAction($authenticationMode) |
62
|
|
|
{ |
63
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
64
|
|
|
$context = $this->getResponseContext($authenticationMode); |
65
|
|
|
|
66
|
|
|
$originalRequestId = $context->getInResponseTo(); |
67
|
|
|
|
68
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
69
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
70
|
|
|
$logger->notice('Determining which second factor to use...'); |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
// Retrieve all requirements to determine the required LoA |
74
|
|
|
$requestedLoa = $context->getRequiredLoa(); |
75
|
|
|
$spConfiguredLoas = $context->getServiceProvider()->get('configuredLoas'); |
76
|
|
|
|
77
|
|
|
$normalizedIdpSho = $context->getNormalizedSchacHomeOrganization(); |
78
|
|
|
$normalizedUserSho = $this->getStepupService()->getNormalizedUserShoByIdentityNameId($context->getIdentityNameId()); |
79
|
|
|
|
80
|
|
|
$requiredLoa = $this |
81
|
|
|
->getStepupService() |
82
|
|
|
->resolveHighestRequiredLoa( |
83
|
|
|
$requestedLoa, |
84
|
|
|
$spConfiguredLoas, |
85
|
|
|
$normalizedIdpSho, |
86
|
|
|
$normalizedUserSho |
87
|
|
|
); |
88
|
|
|
} catch (LoaCannotBeGivenException $e) { |
89
|
|
|
// Log the message of the domain exception, this contains a meaningful message. |
90
|
|
|
$logger->notice($e->getMessage()); |
91
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$logger->notice(sprintf('Determined that the required Loa is "%s"', $requiredLoa)); |
95
|
|
|
|
96
|
|
|
if ($this->getStepupService()->isIntrinsicLoa($requiredLoa)) { |
97
|
|
|
$this->get('gateway.authentication_logger')->logIntrinsicLoaAuthentication($originalRequestId); |
98
|
|
|
|
99
|
|
|
return $this->forward($context->getResponseAction()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$secondFactorCollection = $this |
103
|
|
|
->getStepupService() |
104
|
|
|
->determineViableSecondFactors( |
105
|
|
|
$context->getIdentityNameId(), |
106
|
|
|
$requiredLoa, |
107
|
|
|
$this->get('gateway.service.whitelist') |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
switch (count($secondFactorCollection)) { |
111
|
|
|
case 0: |
112
|
|
|
$logger->notice('No second factors can give the determined Loa'); |
113
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven'); |
114
|
|
|
break; |
|
|
|
|
115
|
|
|
|
116
|
|
|
case 1: |
117
|
|
|
$secondFactor = $secondFactorCollection->first(); |
118
|
|
|
$logger->notice(sprintf( |
119
|
|
|
'Found "%d" second factors, using second factor of type "%s"', |
120
|
|
|
count($secondFactorCollection), |
121
|
|
|
$secondFactor->secondFactorType |
122
|
|
|
)); |
123
|
|
|
|
124
|
|
|
return $this->selectAndRedirectTo($secondFactor, $context, $authenticationMode); |
|
|
|
|
125
|
|
|
break; |
|
|
|
|
126
|
|
|
|
127
|
|
|
default: |
128
|
|
|
return $this->forward( |
129
|
|
|
'SurfnetStepupGatewayGatewayBundle:SecondFactor:chooseSecondFactor', |
130
|
|
|
['authenticationMode' => $authenticationMode, 'secondFactors' => $secondFactorCollection] |
131
|
|
|
); |
132
|
|
|
break; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @Template |
138
|
|
|
* @param Request $request |
139
|
|
|
* @param string $authenticationMode |
140
|
|
|
* @return array|RedirectResponse|Response |
141
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
142
|
|
|
*/ |
143
|
|
|
public function chooseSecondFactorAction(Request $request, $authenticationMode) |
144
|
|
|
{ |
145
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
146
|
|
|
$context = $this->getResponseContext($authenticationMode); |
147
|
|
|
$originalRequestId = $context->getInResponseTo(); |
148
|
|
|
|
149
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
150
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
151
|
|
|
$logger->notice('Ask the user which one of his suitable second factor tokens to use...'); |
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
// Retrieve all requirements to determine the required LoA |
155
|
|
|
$requestedLoa = $context->getRequiredLoa(); |
156
|
|
|
$spConfiguredLoas = $context->getServiceProvider()->get('configuredLoas'); |
157
|
|
|
|
158
|
|
|
$normalizedIdpSho = $context->getNormalizedSchacHomeOrganization(); |
159
|
|
|
$normalizedUserSho = $this->getStepupService()->getNormalizedUserShoByIdentityNameId($context->getIdentityNameId()); |
160
|
|
|
|
161
|
|
|
$requiredLoa = $this |
162
|
|
|
->getStepupService() |
163
|
|
|
->resolveHighestRequiredLoa( |
164
|
|
|
$requestedLoa, |
165
|
|
|
$spConfiguredLoas, |
166
|
|
|
$normalizedIdpSho, |
167
|
|
|
$normalizedUserSho |
168
|
|
|
); |
169
|
|
|
} catch (LoaCannotBeGivenException $e) { |
170
|
|
|
// Log the message of the domain exception, this contains a meaningful message. |
171
|
|
|
$logger->notice($e->getMessage()); |
172
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$logger->notice(sprintf('Determined that the required Loa is "%s"', $requiredLoa)); |
176
|
|
|
|
177
|
|
|
$secondFactors = $this |
178
|
|
|
->getStepupService() |
179
|
|
|
->determineViableSecondFactors( |
180
|
|
|
$context->getIdentityNameId(), |
181
|
|
|
$requiredLoa, |
182
|
|
|
$this->get('gateway.service.whitelist') |
183
|
|
|
); |
184
|
|
|
|
185
|
|
|
$command = new ChooseSecondFactorCommand(); |
186
|
|
|
$command->secondFactors = $secondFactors; |
|
|
|
|
187
|
|
|
|
188
|
|
|
$form = $this |
189
|
|
|
->createForm( |
190
|
|
|
ChooseSecondFactorType::class, |
191
|
|
|
$command, |
192
|
|
|
['action' => $this->generateUrl('gateway_verify_second_factor_choose_second_factor', ['authenticationMode' => $authenticationMode])] |
193
|
|
|
) |
194
|
|
|
->handleRequest($request); |
195
|
|
|
$cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
196
|
|
|
|
197
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
198
|
|
|
$buttonName = $form->getClickedButton()->getName(); |
199
|
|
|
$formResults = $request->request->get('gateway_choose_second_factor', false); |
200
|
|
|
|
201
|
|
|
if (!isset($formResults[$buttonName])) { |
202
|
|
|
throw new InvalidArgumentException( |
203
|
|
|
sprintf( |
204
|
|
|
'Second factor type "%s" could not be found in the posted form results.', |
205
|
|
|
$buttonName |
206
|
|
|
) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$secondFactorType = $formResults[$buttonName]; |
211
|
|
|
|
212
|
|
|
// Filter the selected second factor from the array collection |
213
|
|
|
$secondFactorFiltered = $secondFactors->filter( |
214
|
|
|
function ($secondFactor) use ($secondFactorType) { |
215
|
|
|
return $secondFactorType === $secondFactor->secondFactorType; |
216
|
|
|
} |
217
|
|
|
); |
218
|
|
|
|
219
|
|
|
if ($secondFactorFiltered->isEmpty()) { |
220
|
|
|
throw new InvalidArgumentException( |
221
|
|
|
sprintf( |
222
|
|
|
'Second factor type "%s" could not be found in the collection of available second factors.', |
223
|
|
|
$secondFactorType |
224
|
|
|
) |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$secondFactor = $secondFactorFiltered->first(); |
229
|
|
|
|
230
|
|
|
$logger->notice(sprintf('User chose "%s" to use as second factor', $secondFactorType)); |
231
|
|
|
|
232
|
|
|
// Forward to action to verify possession of second factor |
233
|
|
|
return $this->selectAndRedirectTo($secondFactor, $context, $authenticationMode); |
|
|
|
|
234
|
|
|
} else if ($form->isSubmitted() && !$form->isValid()) { |
235
|
|
|
$form->addError(new FormError('gateway.form.gateway_choose_second_factor.unknown_second_factor_type')); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return [ |
239
|
|
|
'form' => $form->createView(), |
240
|
|
|
'cancelForm' => $cancelForm->createView(), |
241
|
|
|
'secondFactors' => $secondFactors, |
242
|
|
|
]; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function verifyGssfAction(Request $request) |
246
|
|
|
{ |
247
|
|
|
if (!$request->get('authenticationMode', false)) { |
248
|
|
|
throw new RuntimeException('Unable to determine the authentication mode in the GSSP verification action'); |
249
|
|
|
} |
250
|
|
|
$authenticationMode = $request->get('authenticationMode'); |
251
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
252
|
|
|
$context = $this->getResponseContext($authenticationMode); |
253
|
|
|
|
254
|
|
|
$originalRequestId = $context->getInResponseTo(); |
255
|
|
|
|
256
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
257
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
258
|
|
|
$logger->info('Received request to verify GSSF'); |
259
|
|
|
|
260
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
261
|
|
|
|
262
|
|
|
$logger->info(sprintf( |
263
|
|
|
'Selected GSSF "%s" for verfication, forwarding to Saml handling', |
264
|
|
|
$selectedSecondFactor |
265
|
|
|
)); |
266
|
|
|
|
267
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService $secondFactorService */ |
268
|
|
|
$secondFactorService = $this->get('gateway.service.second_factor_service'); |
269
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor */ |
270
|
|
|
$secondFactor = $secondFactorService->findByUuid($selectedSecondFactor); |
271
|
|
|
if (!$secondFactor) { |
272
|
|
|
throw new RuntimeException(sprintf( |
273
|
|
|
'Requested verification of GSSF "%s", however that Second Factor no longer exists', |
274
|
|
|
$selectedSecondFactor |
275
|
|
|
)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this->forward( |
279
|
|
|
'SurfnetStepupGatewaySamlStepupProviderBundle:SamlProxy:sendSecondFactorVerificationAuthnRequest', |
280
|
|
|
[ |
281
|
|
|
'provider' => $secondFactor->secondFactorType, |
282
|
|
|
'subjectNameId' => $secondFactor->secondFactorIdentifier |
283
|
|
|
] |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function gssfVerifiedAction($authenticationMode) |
288
|
|
|
{ |
289
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
290
|
|
|
$context = $this->getResponseContext($authenticationMode); |
291
|
|
|
|
292
|
|
|
$originalRequestId = $context->getInResponseTo(); |
293
|
|
|
|
294
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
295
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
296
|
|
|
$logger->info('Attempting to mark GSSF as verified'); |
297
|
|
|
|
298
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
299
|
|
|
|
300
|
|
|
/** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor */ |
301
|
|
|
$secondFactor = $this->get('gateway.service.second_factor_service')->findByUuid($selectedSecondFactor); |
302
|
|
|
if (!$secondFactor) { |
303
|
|
|
throw new RuntimeException( |
304
|
|
|
sprintf( |
305
|
|
|
'Verification of GSSF "%s" succeeded, however that Second Factor no longer exists', |
306
|
|
|
$selectedSecondFactor |
307
|
|
|
) |
308
|
|
|
); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$context->markSecondFactorVerified(); |
312
|
|
|
$this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
313
|
|
|
|
314
|
|
|
$logger->info(sprintf( |
315
|
|
|
'Marked GSSF "%s" as verified, forwarding to Gateway controller to respond', |
316
|
|
|
$selectedSecondFactor |
317
|
|
|
)); |
318
|
|
|
|
319
|
|
|
return $this->forward($context->getResponseAction()); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @Template |
324
|
|
|
* @param Request $request |
325
|
|
|
* @return array|Response |
326
|
|
|
*/ |
327
|
|
|
public function verifyYubiKeySecondFactorAction(Request $request) |
328
|
|
|
{ |
329
|
|
|
if (!$request->get('authenticationMode', false)) { |
330
|
|
|
throw new RuntimeException('Unable to determine the authentication mode in Yubikey verification action'); |
331
|
|
|
} |
332
|
|
|
$authenticationMode = $request->get('authenticationMode'); |
333
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
334
|
|
|
$context = $this->getResponseContext($authenticationMode); |
335
|
|
|
$originalRequestId = $context->getInResponseTo(); |
336
|
|
|
|
337
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
338
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
339
|
|
|
|
340
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
341
|
|
|
|
342
|
|
|
$logger->notice('Verifying possession of Yubikey second factor'); |
343
|
|
|
|
344
|
|
|
$command = new VerifyYubikeyOtpCommand(); |
345
|
|
|
$command->secondFactorId = $selectedSecondFactor; |
346
|
|
|
|
347
|
|
|
$form = $this->createForm(VerifyYubikeyOtpType::class, $command)->handleRequest($request); |
348
|
|
|
$cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
349
|
|
|
|
350
|
|
|
if (!$form->isValid()) { |
351
|
|
|
// OTP field is rendered empty in the template. |
352
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
$result = $this->getStepupService()->verifyYubikeyOtp($command); |
356
|
|
|
|
357
|
|
|
if ($result->didOtpVerificationFail()) { |
358
|
|
|
$form->addError(new FormError('gateway.form.verify_yubikey.otp_verification_failed')); |
359
|
|
|
|
360
|
|
|
// OTP field is rendered empty in the template. |
361
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
362
|
|
|
} elseif (!$result->didPublicIdMatch()) { |
363
|
|
|
$form->addError(new FormError('gateway.form.verify_yubikey.public_id_mismatch')); |
364
|
|
|
|
365
|
|
|
// OTP field is rendered empty in the template. |
366
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
$this->getResponseContext($authenticationMode)->markSecondFactorVerified(); |
370
|
|
|
$this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
371
|
|
|
|
372
|
|
|
$logger->info( |
373
|
|
|
sprintf( |
374
|
|
|
'Marked Yubikey Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
375
|
|
|
$selectedSecondFactor |
376
|
|
|
) |
377
|
|
|
); |
378
|
|
|
|
379
|
|
|
return $this->forward($context->getResponseAction()); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @Template |
384
|
|
|
* @param Request $request |
385
|
|
|
* @param string $authenticationMode |
|
|
|
|
386
|
|
|
* @return array|Response |
387
|
|
|
*/ |
388
|
|
|
public function verifySmsSecondFactorAction(Request $request) |
389
|
|
|
{ |
390
|
|
|
if (!$request->get('authenticationMode', false)) { |
391
|
|
|
throw new RuntimeException('Unable to determine the authentication mode in the SMS verification action'); |
392
|
|
|
} |
393
|
|
|
$authenticationMode = $request->get('authenticationMode'); |
394
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
395
|
|
|
$context = $this->getResponseContext($authenticationMode); |
396
|
|
|
$originalRequestId = $context->getInResponseTo(); |
397
|
|
|
|
398
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
399
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
400
|
|
|
|
401
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
402
|
|
|
|
403
|
|
|
$logger->notice('Verifying possession of SMS second factor, preparing to send'); |
404
|
|
|
|
405
|
|
|
$command = new SendSmsChallengeCommand(); |
406
|
|
|
$command->secondFactorId = $selectedSecondFactor; |
407
|
|
|
|
408
|
|
|
$form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request); |
409
|
|
|
$cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
410
|
|
|
|
411
|
|
|
$stepupService = $this->getStepupService(); |
412
|
|
|
$phoneNumber = InternationalPhoneNumber::fromStringFormat( |
413
|
|
|
$stepupService->getSecondFactorIdentifier($selectedSecondFactor) |
414
|
|
|
); |
415
|
|
|
|
416
|
|
|
$otpRequestsRemaining = $stepupService->getSmsOtpRequestsRemainingCount(); |
417
|
|
|
$maximumOtpRequests = $stepupService->getSmsMaximumOtpRequestsCount(); |
418
|
|
|
$viewVariables = ['otpRequestsRemaining' => $otpRequestsRemaining, 'maximumOtpRequests' => $maximumOtpRequests]; |
419
|
|
|
|
420
|
|
|
if (!$form->isValid()) { |
421
|
|
|
return array_merge( |
422
|
|
|
$viewVariables, |
423
|
|
|
['phoneNumber' => $phoneNumber, 'form' => $form->createView(), 'cancelForm' => $cancelForm->createView()] |
424
|
|
|
); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
$logger->notice('Verifying possession of SMS second factor, sending challenge per SMS'); |
428
|
|
|
|
429
|
|
|
if (!$stepupService->sendSmsChallenge($command)) { |
430
|
|
|
$form->addError(new FormError('gateway.form.send_sms_challenge.sms_sending_failed')); |
431
|
|
|
|
432
|
|
|
return array_merge( |
433
|
|
|
$viewVariables, |
434
|
|
|
['phoneNumber' => $phoneNumber, 'form' => $form->createView(), 'cancelForm' => $cancelForm->createView()] |
435
|
|
|
); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
return $this->redirect($this->generateUrl('gateway_verify_second_factor_sms_verify_challenge', ['authenticationMode' => $authenticationMode])); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* @Template |
443
|
|
|
* @param Request $request |
444
|
|
|
* @param string $authenticationMode |
|
|
|
|
445
|
|
|
* @return array|Response |
446
|
|
|
*/ |
447
|
|
|
public function verifySmsSecondFactorChallengeAction(Request $request) |
448
|
|
|
{ |
449
|
|
|
if (!$request->get('authenticationMode', false)) { |
450
|
|
|
throw new RuntimeException('Unable to determine the authentication mode in the SMS challenge action'); |
451
|
|
|
} |
452
|
|
|
$authenticationMode = $request->get('authenticationMode'); |
453
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
454
|
|
|
$context = $this->getResponseContext($authenticationMode); |
455
|
|
|
$originalRequestId = $context->getInResponseTo(); |
456
|
|
|
|
457
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
458
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
459
|
|
|
|
460
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
461
|
|
|
|
462
|
|
|
$command = new VerifyPossessionOfPhoneCommand(); |
463
|
|
|
$form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request); |
464
|
|
|
$cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
465
|
|
|
|
466
|
|
|
if (!$form->isValid()) { |
467
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
$logger->notice('Verifying input SMS challenge matches'); |
471
|
|
|
|
472
|
|
|
$verification = $this->getStepupService()->verifySmsChallenge($command); |
473
|
|
|
|
474
|
|
|
if ($verification->wasSuccessful()) { |
475
|
|
|
$this->getStepupService()->clearSmsVerificationState(); |
476
|
|
|
|
477
|
|
|
$this->getResponseContext()->markSecondFactorVerified(); |
|
|
|
|
478
|
|
|
$this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
479
|
|
|
|
480
|
|
|
$logger->info( |
481
|
|
|
sprintf( |
482
|
|
|
'Marked Sms Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
483
|
|
|
$selectedSecondFactor |
484
|
|
|
) |
485
|
|
|
); |
486
|
|
|
|
487
|
|
|
return $this->forward($context->getResponseAction()); |
488
|
|
|
} elseif ($verification->didOtpExpire()) { |
489
|
|
|
$logger->notice('SMS challenge expired'); |
490
|
|
|
$form->addError(new FormError('gateway.form.send_sms_challenge.challenge_expired')); |
491
|
|
|
} elseif ($verification->wasAttemptedTooManyTimes()) { |
492
|
|
|
$logger->notice('SMS challenge verification was attempted too many times'); |
493
|
|
|
$form->addError(new FormError('gateway.form.send_sms_challenge.too_many_attempts')); |
494
|
|
|
} else { |
495
|
|
|
$logger->notice('SMS challenge did not match'); |
496
|
|
|
$form->addError(new FormError('gateway.form.send_sms_challenge.sms_challenge_incorrect')); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @Template |
504
|
|
|
* @param string $authenticationMode |
505
|
|
|
* @return array |
506
|
|
|
*/ |
507
|
|
|
public function initiateU2fAuthenticationAction($authenticationMode) |
508
|
|
|
{ |
509
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
510
|
|
|
$context = $this->getResponseContext($authenticationMode); |
511
|
|
|
|
512
|
|
|
$originalRequestId = $context->getInResponseTo(); |
513
|
|
|
|
514
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
515
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
516
|
|
|
|
517
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
518
|
|
|
$stepupService = $this->getStepupService(); |
519
|
|
|
|
520
|
|
|
$cancelFormAction = $this->generateUrl('gateway_verify_second_factor_u2f_cancel_authentication', ['authenticationMode' => $authenticationMode]); |
521
|
|
|
$cancelForm = |
522
|
|
|
$this->createForm(CancelSecondFactorVerificationType::class, null, ['action' => $cancelFormAction]); |
523
|
|
|
|
524
|
|
|
$logger->notice('Verifying possession of U2F second factor, looking for registration matching key handle'); |
525
|
|
|
|
526
|
|
|
$service = $this->get('surfnet_stepup_u2f_verification.service.u2f_verification'); |
527
|
|
|
$keyHandle = new KeyHandle($stepupService->getSecondFactorIdentifier($selectedSecondFactor)); |
528
|
|
|
$registration = $service->findRegistrationByKeyHandle($keyHandle); |
529
|
|
|
|
530
|
|
|
if ($registration === null) { |
531
|
|
|
$logger->critical( |
532
|
|
|
sprintf('No known registration for key handle of second factor "%s"', $selectedSecondFactor) |
533
|
|
|
); |
534
|
|
|
$this->addFlash('error', 'gateway.u2f.alert.unknown_registration'); |
535
|
|
|
|
536
|
|
|
return ['authenticationFailed' => true, 'cancelForm' => $cancelForm->createView()]; |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
$logger->notice('Creating sign request'); |
540
|
|
|
|
541
|
|
|
$signRequest = $service->createSignRequest($registration); |
542
|
|
|
$signResponse = new SignResponse(); |
543
|
|
|
|
544
|
|
|
/** @var AttributeBagInterface $session */ |
545
|
|
|
$session = $this->get('gateway.session.u2f'); |
546
|
|
|
$session->set('request', $signRequest); |
547
|
|
|
|
548
|
|
|
$formAction = $this->generateUrl( |
549
|
|
|
'gateway_verify_second_factor_u2f_verify_authentication', |
550
|
|
|
['authenticationMode' => $authenticationMode] |
551
|
|
|
); |
552
|
|
|
$form = $this->createForm( |
553
|
|
|
VerifyDeviceAuthenticationType::class, |
554
|
|
|
$signResponse, |
555
|
|
|
['sign_request' => $signRequest, 'action' => $formAction] |
556
|
|
|
); |
557
|
|
|
|
558
|
|
|
return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* @Template("SurfnetStepupGatewayGatewayBundle:SecondFactor:initiateU2fAuthentication.html.twig") |
563
|
|
|
* |
564
|
|
|
* @param Request $request |
565
|
|
|
* @param string $authenticationMode |
|
|
|
|
566
|
|
|
* @return array|Response |
567
|
|
|
*/ |
568
|
|
|
public function verifyU2fAuthenticationAction(Request $request) |
569
|
|
|
{ |
570
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
571
|
|
|
$context = $this->getResponseContext($authenticationMode); |
572
|
|
|
|
573
|
|
|
$originalRequestId = $context->getInResponseTo(); |
574
|
|
|
|
575
|
|
|
/** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
576
|
|
|
$logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
577
|
|
|
|
578
|
|
|
$selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
|
|
|
|
579
|
|
|
|
580
|
|
|
$logger->notice('Received sign response from device'); |
581
|
|
|
|
582
|
|
|
/** @var AttributeBagInterface $session */ |
583
|
|
|
$session = $this->get('gateway.session.u2f'); |
584
|
|
|
$signRequest = $session->get('request'); |
585
|
|
|
$signResponse = new SignResponse(); |
586
|
|
|
|
587
|
|
|
$formAction = $this->generateUrl( |
588
|
|
|
'gateway_verify_second_factor_u2f_verify_authentication', |
589
|
|
|
['authenticationMode' => $authenticationMode] |
590
|
|
|
); |
591
|
|
|
$form = $this |
592
|
|
|
->createForm( |
593
|
|
|
VerifyDeviceAuthenticationType::class, |
594
|
|
|
$signResponse, |
595
|
|
|
['sign_request' => $signRequest, 'action' => $formAction] |
596
|
|
|
) |
597
|
|
|
->handleRequest($request); |
598
|
|
|
|
599
|
|
|
$cancelFormAction = $this->generateUrl( |
600
|
|
|
'gateway_verify_second_factor_u2f_cancel_authentication', |
601
|
|
|
['authenticationMode' => $authenticationMode] |
602
|
|
|
); |
603
|
|
|
$cancelForm = |
604
|
|
|
$this->createForm(CancelSecondFactorVerificationType::class, null, ['action' => $cancelFormAction]); |
605
|
|
|
|
606
|
|
|
if (!$form->isValid()) { |
607
|
|
|
$logger->error('U2F authentication verification could not be started because device send illegal data'); |
608
|
|
|
$this->addFlash('error', 'gateway.u2f.alert.error'); |
609
|
|
|
|
610
|
|
|
return ['authenticationFailed' => true, 'cancelForm' => $cancelForm->createView()]; |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
$service = $this->get('surfnet_stepup_u2f_verification.service.u2f_verification'); |
614
|
|
|
$result = $service->verifyAuthentication($signRequest, $signResponse); |
615
|
|
|
|
616
|
|
|
if ($result->wasSuccessful()) { |
617
|
|
|
$context->markSecondFactorVerified(); |
618
|
|
|
$this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
619
|
|
|
|
620
|
|
|
$logger->info( |
621
|
|
|
sprintf( |
622
|
|
|
'Marked U2F second factor "%s" as verified, forwarding to Saml Proxy to respond', |
623
|
|
|
$selectedSecondFactor |
624
|
|
|
) |
625
|
|
|
); |
626
|
|
|
|
627
|
|
|
return $this->forward($context->getResponseAction()); |
628
|
|
|
} elseif ($result->didDeviceReportError()) { |
629
|
|
|
$logger->error('U2F device reported error during authentication'); |
630
|
|
|
$this->addFlash('error', 'gateway.u2f.alert.device_reported_an_error'); |
631
|
|
|
} else { |
632
|
|
|
$logger->error('U2F authentication verification failed'); |
633
|
|
|
$this->addFlash('error', 'gateway.u2f.alert.error'); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
return ['authenticationFailed' => true, 'cancelForm' => $cancelForm->createView()]; |
637
|
|
|
} |
638
|
|
|
|
639
|
|
|
public function cancelAuthenticationAction($authenticationMode) |
640
|
|
|
{ |
641
|
|
|
// Might not need the authenticationMode? |
642
|
|
|
$this->supportsAuthenticationMode($authenticationMode); |
|
|
|
|
643
|
|
|
|
644
|
|
|
return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendAuthenticationCancelledByUser'); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Service\StepupAuthenticationService |
649
|
|
|
*/ |
650
|
|
|
private function getStepupService() |
651
|
|
|
{ |
652
|
|
|
return $this->get('gateway.service.stepup_authentication'); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* @return ResponseContext |
|
|
|
|
657
|
|
|
*/ |
658
|
|
|
private function getResponseContext($authenticationMode) |
659
|
|
|
{ |
660
|
|
|
switch ($authenticationMode) { |
661
|
|
|
case self::MODE_SFO: |
662
|
|
|
return $this->get($this->get('gateway.proxy.sfo.state_handler')->getResponseContextServiceId()); |
663
|
|
|
break; |
|
|
|
|
664
|
|
|
case self::MODE_SSO: |
665
|
|
|
return $this->get($this->get('gateway.proxy.state_handler')->getResponseContextServiceId()); |
666
|
|
|
break; |
|
|
|
|
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* @return \Surfnet\StepupGateway\GatewayBundle\Monolog\Logger\AuthenticationLogger |
672
|
|
|
*/ |
673
|
|
|
private function getAuthenticationLogger() |
674
|
|
|
{ |
675
|
|
|
return $this->get('gateway.authentication_logger'); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* @param ResponseContext $context |
680
|
|
|
* @param LoggerInterface $logger |
681
|
|
|
* @return string |
682
|
|
|
*/ |
683
|
|
|
private function getSelectedSecondFactor(ResponseContext $context, LoggerInterface $logger) |
684
|
|
|
{ |
685
|
|
|
$selectedSecondFactor = $context->getSelectedSecondFactor(); |
686
|
|
|
|
687
|
|
|
if (!$selectedSecondFactor) { |
688
|
|
|
$logger->error('Cannot verify possession of an unknown second factor'); |
689
|
|
|
|
690
|
|
|
throw new BadRequestHttpException('Cannot verify possession of an unknown second factor.'); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
return $selectedSecondFactor; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
private function selectAndRedirectTo(SecondFactor $secondFactor, ResponseContext $context, $authenticationMode) |
697
|
|
|
{ |
698
|
|
|
$context->saveSelectedSecondFactor($secondFactor); |
699
|
|
|
|
700
|
|
|
$this->getStepupService()->clearSmsVerificationState(); |
701
|
|
|
|
702
|
|
|
$secondFactorTypeService = $this->get('surfnet_stepup.service.second_factor_type'); |
703
|
|
|
$secondFactorType = new SecondFactorType($secondFactor->secondFactorType); |
704
|
|
|
|
705
|
|
|
$route = 'gateway_verify_second_factor_'; |
706
|
|
|
if ($secondFactorTypeService->isGssf($secondFactorType)) { |
707
|
|
|
$route .= 'gssf'; |
708
|
|
|
} else { |
709
|
|
|
$route .= strtolower($secondFactor->secondFactorType); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
return $this->redirect($this->generateUrl($route, ['authenticationMode' => $authenticationMode])); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* @param string $authenticationMode |
717
|
|
|
* @return FormInterface |
718
|
|
|
*/ |
719
|
|
|
private function buildCancelAuthenticationForm($authenticationMode) |
720
|
|
|
{ |
721
|
|
|
$cancelFormAction = $this->generateUrl( |
722
|
|
|
'gateway_cancel_authentication', |
723
|
|
|
['authenticationMode' => $authenticationMode] |
724
|
|
|
); |
725
|
|
|
|
726
|
|
|
return $this->createForm( |
727
|
|
|
CancelAuthenticationType::class, |
728
|
|
|
null, |
729
|
|
|
['action' => $cancelFormAction] |
730
|
|
|
); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
private function supportsAuthenticationMode($authenticationMode) |
734
|
|
|
{ |
735
|
|
|
if ($authenticationMode === self::MODE_SSO || $authenticationMode === self::MODE_SFO) { |
|
|
|
|
736
|
|
|
return true; |
737
|
|
|
} |
738
|
|
|
return false; |
739
|
|
|
} |
740
|
|
|
} |
741
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: