1 | <?php |
||
41 | class SecondFactorController extends Controller |
||
42 | { |
||
43 | public function selectSecondFactorForVerificationAction() |
||
122 | |||
123 | public function verifyGssfAction() |
||
160 | |||
161 | public function gssfVerifiedAction() |
||
193 | |||
194 | /** |
||
195 | * @Template |
||
196 | * @param Request $request |
||
197 | * @return array|Response |
||
198 | */ |
||
199 | public function verifyYubiKeySecondFactorAction(Request $request) |
||
200 | { |
||
201 | $context = $this->getResponseContext(); |
||
202 | $originalRequestId = $context->getInResponseTo(); |
||
203 | |||
204 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
205 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
206 | |||
207 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
208 | |||
209 | $logger->notice('Verifying possession of Yubikey second factor'); |
||
210 | |||
211 | $command = new VerifyYubikeyOtpCommand(); |
||
212 | $command->secondFactorId = $selectedSecondFactor; |
||
213 | |||
214 | $form = $this->createForm('gateway_verify_yubikey_otp', $command)->handleRequest($request); |
||
215 | |||
216 | if ($form->get('cancel')->isClicked()) { |
||
|
|||
217 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendAuthenticationCancelledByUser'); |
||
218 | } |
||
219 | |||
220 | if (!$form->isValid()) { |
||
221 | // OTP field is rendered empty in the template. |
||
222 | return ['form' => $form->createView()]; |
||
223 | } |
||
224 | |||
225 | $result = $this->getStepupService()->verifyYubikeyOtp($command); |
||
226 | |||
227 | if ($result->didOtpVerificationFail()) { |
||
228 | $form->addError(new FormError('gateway.form.verify_yubikey.otp_verification_failed')); |
||
229 | |||
230 | // OTP field is rendered empty in the template. |
||
231 | return ['form' => $form->createView()]; |
||
232 | } elseif (!$result->didPublicIdMatch()) { |
||
233 | $form->addError(new FormError('gateway.form.verify_yubikey.public_id_mismatch')); |
||
234 | |||
235 | // OTP field is rendered empty in the template. |
||
236 | return ['form' => $form->createView()]; |
||
237 | } |
||
238 | |||
239 | $this->getResponseContext()->markSecondFactorVerified(); |
||
240 | $this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
||
241 | |||
242 | $logger->info( |
||
243 | sprintf( |
||
244 | 'Marked Yubikey Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
||
245 | $selectedSecondFactor |
||
246 | ) |
||
247 | ); |
||
248 | |||
249 | return $this->forward($context->getResponseAction()); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * @Template |
||
254 | * @param Request $request |
||
255 | * @return array|Response |
||
256 | */ |
||
257 | public function verifySmsSecondFactorAction(Request $request) |
||
258 | { |
||
259 | $context = $this->getResponseContext(); |
||
260 | $originalRequestId = $context->getInResponseTo(); |
||
261 | |||
262 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
263 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
264 | |||
265 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
266 | |||
267 | $logger->notice('Verifying possession of SMS second factor, preparing to send'); |
||
268 | |||
269 | $command = new SendSmsChallengeCommand(); |
||
270 | $command->secondFactorId = $selectedSecondFactor; |
||
271 | |||
272 | $form = $this->createForm('gateway_send_sms_challenge', $command)->handleRequest($request); |
||
273 | |||
274 | $stepupService = $this->getStepupService(); |
||
275 | $phoneNumber = InternationalPhoneNumber::fromStringFormat( |
||
276 | $stepupService->getSecondFactorIdentifier($selectedSecondFactor) |
||
277 | ); |
||
278 | |||
279 | $otpRequestsRemaining = $stepupService->getSmsOtpRequestsRemainingCount(); |
||
280 | $maximumOtpRequests = $stepupService->getSmsMaximumOtpRequestsCount(); |
||
281 | $viewVariables = ['otpRequestsRemaining' => $otpRequestsRemaining, 'maximumOtpRequests' => $maximumOtpRequests]; |
||
282 | |||
283 | if ($form->get('cancel')->isClicked()) { |
||
284 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendAuthenticationCancelledByUser'); |
||
285 | } |
||
286 | |||
287 | if (!$form->isValid()) { |
||
288 | return array_merge($viewVariables, ['phoneNumber' => $phoneNumber, 'form' => $form->createView()]); |
||
289 | } |
||
290 | |||
291 | $logger->notice('Verifying possession of SMS second factor, sending challenge per SMS'); |
||
292 | |||
293 | if (!$stepupService->sendSmsChallenge($command)) { |
||
294 | $form->addError(new FormError('gateway.form.send_sms_challenge.sms_sending_failed')); |
||
295 | |||
296 | return array_merge($viewVariables, ['phoneNumber' => $phoneNumber, 'form' => $form->createView()]); |
||
297 | } |
||
298 | |||
299 | return $this->redirect($this->generateUrl('gateway_verify_second_factor_sms_verify_challenge')); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @Template |
||
304 | * @param Request $request |
||
305 | * @return array|Response |
||
306 | */ |
||
307 | public function verifySmsSecondFactorChallengeAction(Request $request) |
||
308 | { |
||
309 | $context = $this->getResponseContext(); |
||
310 | $originalRequestId = $context->getInResponseTo(); |
||
311 | |||
312 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
313 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
314 | |||
315 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
316 | |||
317 | $command = new VerifyPossessionOfPhoneCommand(); |
||
318 | $form = $this->createForm('gateway_verify_sms_challenge', $command)->handleRequest($request); |
||
319 | |||
320 | if ($form->get('cancel')->isClicked()) { |
||
321 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendAuthenticationCancelledByUser'); |
||
322 | } |
||
323 | |||
324 | if (!$form->isValid()) { |
||
325 | return ['form' => $form->createView()]; |
||
326 | } |
||
327 | |||
328 | $logger->notice('Verifying input SMS challenge matches'); |
||
329 | |||
330 | $verification = $this->getStepupService()->verifySmsChallenge($command); |
||
331 | |||
332 | if ($verification->wasSuccessful()) { |
||
333 | $this->getStepupService()->clearSmsVerificationState(); |
||
334 | |||
335 | $this->getResponseContext()->markSecondFactorVerified(); |
||
336 | $this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId); |
||
337 | |||
338 | $logger->info( |
||
339 | sprintf( |
||
340 | 'Marked Sms Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
||
341 | $selectedSecondFactor |
||
342 | ) |
||
343 | ); |
||
344 | |||
345 | return $this->forward($context->getResponseAction()); |
||
346 | } elseif ($verification->didOtpExpire()) { |
||
347 | $logger->notice('SMS challenge expired'); |
||
348 | $form->addError(new FormError('gateway.form.send_sms_challenge.challenge_expired')); |
||
349 | } elseif ($verification->wasAttemptedTooManyTimes()) { |
||
350 | $logger->notice('SMS challenge verification was attempted too many times'); |
||
351 | $form->addError(new FormError('gateway.form.send_sms_challenge.too_many_attempts')); |
||
352 | } else { |
||
353 | $logger->notice('SMS challenge did not match'); |
||
354 | $form->addError(new FormError('gateway.form.send_sms_challenge.sms_challenge_incorrect')); |
||
355 | } |
||
356 | |||
357 | return ['form' => $form->createView()]; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @Template |
||
362 | */ |
||
363 | public function initiateU2fAuthenticationAction() |
||
411 | |||
412 | /** |
||
413 | * @Template("SurfnetStepupGatewayGatewayBundle:SecondFactor:initiateU2fAuthentication.html.twig") |
||
414 | * |
||
415 | * @param Request $request |
||
416 | * @return array|Response |
||
417 | */ |
||
418 | public function verifyU2fAuthenticationAction(Request $request) |
||
480 | |||
481 | public function cancelU2fAuthenticationAction() |
||
485 | |||
486 | /** |
||
487 | * @return \Surfnet\StepupGateway\GatewayBundle\Service\StepupAuthenticationService |
||
488 | */ |
||
489 | private function getStepupService() |
||
493 | |||
494 | /** |
||
495 | * @return ResponseContext |
||
496 | */ |
||
497 | private function getResponseContext() |
||
501 | |||
502 | /** |
||
503 | * @return \Surfnet\StepupGateway\GatewayBundle\Monolog\Logger\AuthenticationLogger |
||
504 | */ |
||
505 | private function getAuthenticationLogger() |
||
509 | |||
510 | /** |
||
511 | * @param ResponseContext $context |
||
512 | * @param LoggerInterface $logger |
||
513 | * @return string |
||
514 | */ |
||
515 | private function getSelectedSecondFactor(ResponseContext $context, LoggerInterface $logger) |
||
527 | } |
||
528 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: