| Total Complexity | 54 |
| Total Lines | 603 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SecondFactorController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SecondFactorController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class SecondFactorController extends Controller |
||
| 55 | { |
||
| 56 | const MODE_SFO = 'sfo'; |
||
| 57 | const MODE_SSO = 'sso'; |
||
| 58 | |||
| 59 | public function selectSecondFactorForVerificationSsoAction(Request $request) |
||
| 60 | { |
||
| 61 | return $this->selectSecondFactorForVerificationAction(self::MODE_SSO, $request); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function selectSecondFactorForVerificationSfoAction(Request $request) |
||
| 67 | } |
||
| 68 | |||
| 69 | public function selectSecondFactorForVerificationAction($authenticationMode, Request $request) |
||
| 70 | { |
||
| 71 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 72 | $context = $this->getResponseContext($authenticationMode); |
||
| 73 | $originalRequestId = $context->getInResponseTo(); |
||
| 74 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 75 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 76 | $logger->notice('Determining which second factor to use...'); |
||
| 77 | try { |
||
| 78 | // Retrieve all requirements to determine the required LoA |
||
| 79 | $requestedLoa = $context->getRequiredLoa(); |
||
| 80 | $spConfiguredLoas = $context->getServiceProvider()->get('configuredLoas'); |
||
| 81 | $identityNameId = $context->getIdentityNameId(); |
||
| 82 | $normalizedIdpSho = $context->getNormalizedSchacHomeOrganization(); |
||
| 83 | $normalizedUserSho = $this->getStepupService()->getNormalizedUserShoByIdentityNameId($identityNameId); |
||
| 84 | $requiredLoa = $this |
||
| 85 | ->getStepupService() |
||
| 86 | ->resolveHighestRequiredLoa( |
||
| 87 | $requestedLoa, |
||
| 88 | $spConfiguredLoas, |
||
| 89 | $normalizedIdpSho, |
||
| 90 | $normalizedUserSho |
||
| 91 | ); |
||
| 92 | } catch (LoaCannotBeGivenException $e) { |
||
| 93 | // Log the message of the domain exception, this contains a meaningful message. |
||
| 94 | $logger->notice($e->getMessage()); |
||
| 95 | return $this->forward( |
||
| 96 | 'SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven', |
||
| 97 | ['authenticationMode' => $authenticationMode] |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $logger->notice(sprintf('Determined that the required Loa is "%s"', $requiredLoa)); |
||
| 102 | if ($this->getStepupService()->isIntrinsicLoa($requiredLoa)) { |
||
| 103 | $this->get('gateway.authentication_logger')->logIntrinsicLoaAuthentication($originalRequestId); |
||
| 104 | return $this->forward($context->getResponseAction()); |
||
| 105 | } |
||
| 106 | |||
| 107 | $secondFactorCollection = $this |
||
| 108 | ->getStepupService() |
||
| 109 | ->determineViableSecondFactors( |
||
| 110 | $context->getIdentityNameId(), |
||
| 111 | $requiredLoa, |
||
| 112 | $this->get('gateway.service.whitelist') |
||
| 113 | ); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var CookieService $ssoCookieService |
||
| 117 | */ |
||
| 118 | $ssoCookieService = $this->get('gateway.service.sso_2fa_cookie'); |
||
| 119 | $ssoCookie = $ssoCookieService->read($request); |
||
| 120 | if ($ssoCookieService->shouldSkip2faAuthentication( |
||
| 121 | $context, |
||
| 122 | $requiredLoa->getLevel(), |
||
| 123 | $identityNameId, |
||
| 124 | $ssoCookie |
||
| 125 | )) { |
||
| 126 | $logger->notice('Skipping second factor authentication. Required LoA was met by the LoA recorded in the cookie'); |
||
| 127 | // We use the SF from the cookie as the SF that was used for authenticating the second factor authentication |
||
| 128 | $secondFactor = $this->getSecondFactorService()->findByUuid($ssoCookie->secondFactorId()); |
||
| 129 | $this->getResponseContext($authenticationMode)->saveSelectedSecondFactor($secondFactor); |
||
| 130 | $this->getResponseContext($authenticationMode)->markSecondFactorVerified(); |
||
| 131 | $this->getResponseContext($authenticationMode)->markVerifiedBySsoOn2faCookie($ssoCookieService->getCookieFingerprint($request)); |
||
| 132 | $this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId, $authenticationMode); |
||
| 133 | return $this->forward($context->getResponseAction()); |
||
| 134 | } |
||
| 135 | switch (count($secondFactorCollection)) { |
||
| 136 | case 0: |
||
| 137 | $logger->notice('No second factors can give the determined Loa'); |
||
| 138 | return $this->forward( |
||
| 139 | 'SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven', |
||
| 140 | ['authenticationMode' => $authenticationMode] |
||
| 141 | ); |
||
| 142 | case 1: |
||
| 143 | $secondFactor = $secondFactorCollection->first(); |
||
| 144 | $logger->notice(sprintf( |
||
| 145 | 'Found "%d" second factors, using second factor of type "%s"', |
||
| 146 | count($secondFactorCollection), |
||
| 147 | $secondFactor->secondFactorType |
||
| 148 | )); |
||
| 149 | return $this->selectAndRedirectTo($secondFactor, $context, $authenticationMode); |
||
| 150 | default: |
||
| 151 | return $this->forward( |
||
| 152 | 'SurfnetStepupGatewayGatewayBundle:SecondFactor:chooseSecondFactor', |
||
| 153 | ['authenticationMode' => $authenticationMode, 'secondFactors' => $secondFactorCollection] |
||
| 154 | ); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * The main WAYG screen |
||
| 160 | * - Shows the token selection screen if you own > 1 token |
||
| 161 | * - Directly goes to SF auth when identity owns 1 token |
||
| 162 | * |
||
| 163 | * @Template |
||
| 164 | * @param Request $request |
||
| 165 | * @param string $authenticationMode |
||
| 166 | * @return array|RedirectResponse|Response |
||
| 167 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
| 168 | */ |
||
| 169 | public function chooseSecondFactorAction(Request $request, $authenticationMode) |
||
| 170 | { |
||
| 171 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 172 | $context = $this->getResponseContext($authenticationMode); |
||
| 173 | $originalRequestId = $context->getInResponseTo(); |
||
| 174 | |||
| 175 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 176 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 177 | $logger->notice('Ask the user which one of his suitable second factor tokens to use...'); |
||
| 178 | |||
| 179 | try { |
||
| 180 | // Retrieve all requirements to determine the required LoA |
||
| 181 | $requestedLoa = $context->getRequiredLoa(); |
||
| 182 | $spConfiguredLoas = $context->getServiceProvider()->get('configuredLoas'); |
||
| 183 | |||
| 184 | $normalizedIdpSho = $context->getNormalizedSchacHomeOrganization(); |
||
| 185 | $normalizedUserSho = $this->getStepupService()->getNormalizedUserShoByIdentityNameId($context->getIdentityNameId()); |
||
| 186 | |||
| 187 | $requiredLoa = $this |
||
| 188 | ->getStepupService() |
||
| 189 | ->resolveHighestRequiredLoa( |
||
| 190 | $requestedLoa, |
||
| 191 | $spConfiguredLoas, |
||
| 192 | $normalizedIdpSho, |
||
| 193 | $normalizedUserSho |
||
| 194 | ); |
||
| 195 | } catch (LoaCannotBeGivenException $e) { |
||
| 196 | // Log the message of the domain exception, this contains a meaningful message. |
||
| 197 | $logger->notice($e->getMessage()); |
||
| 198 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendLoaCannotBeGiven'); |
||
| 199 | } |
||
| 200 | |||
| 201 | $logger->notice(sprintf('Determined that the required Loa is "%s"', $requiredLoa)); |
||
| 202 | |||
| 203 | $secondFactors = $this |
||
| 204 | ->getStepupService() |
||
| 205 | ->determineViableSecondFactors( |
||
| 206 | $context->getIdentityNameId(), |
||
| 207 | $requiredLoa, |
||
| 208 | $this->get('gateway.service.whitelist') |
||
| 209 | ); |
||
| 210 | |||
| 211 | $command = new ChooseSecondFactorCommand(); |
||
| 212 | $command->secondFactors = $secondFactors; |
||
| 213 | |||
| 214 | $form = $this |
||
| 215 | ->createForm( |
||
| 216 | ChooseSecondFactorType::class, |
||
| 217 | $command, |
||
| 218 | ['action' => $this->generateUrl('gateway_verify_second_factor_choose_second_factor', ['authenticationMode' => $authenticationMode])] |
||
| 219 | ) |
||
| 220 | ->handleRequest($request); |
||
| 221 | $cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
||
| 222 | |||
| 223 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 224 | $buttonName = $form->getClickedButton()->getName(); |
||
| 225 | $formResults = $request->request->get('gateway_choose_second_factor', false); |
||
| 226 | |||
| 227 | if (!isset($formResults[$buttonName])) { |
||
| 228 | throw new InvalidArgumentException( |
||
| 229 | sprintf( |
||
| 230 | 'Second factor type "%s" could not be found in the posted form results.', |
||
| 231 | $buttonName |
||
| 232 | ) |
||
| 233 | ); |
||
| 234 | } |
||
| 235 | |||
| 236 | $secondFactorType = $formResults[$buttonName]; |
||
| 237 | |||
| 238 | // Filter the selected second factor from the array collection |
||
| 239 | $secondFactorFiltered = $secondFactors->filter( |
||
| 240 | function ($secondFactor) use ($secondFactorType) { |
||
| 241 | return $secondFactorType === $secondFactor->secondFactorType; |
||
| 242 | } |
||
| 243 | ); |
||
| 244 | |||
| 245 | if ($secondFactorFiltered->isEmpty()) { |
||
| 246 | throw new InvalidArgumentException( |
||
| 247 | sprintf( |
||
| 248 | 'Second factor type "%s" could not be found in the collection of available second factors.', |
||
| 249 | $secondFactorType |
||
| 250 | ) |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | $secondFactor = $secondFactorFiltered->first(); |
||
| 255 | |||
| 256 | $logger->notice(sprintf('User chose "%s" to use as second factor', $secondFactorType)); |
||
| 257 | |||
| 258 | // Forward to action to verify possession of second factor |
||
| 259 | return $this->selectAndRedirectTo($secondFactor, $context, $authenticationMode); |
||
| 260 | } else if ($form->isSubmitted() && !$form->isValid()) { |
||
| 261 | $form->addError( |
||
| 262 | new FormError( |
||
| 263 | $this->get('translator') |
||
| 264 | ->trans('gateway.form.gateway_choose_second_factor.unknown_second_factor_type') |
||
| 265 | ) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | return [ |
||
| 270 | 'form' => $form->createView(), |
||
| 271 | 'cancelForm' => $cancelForm->createView(), |
||
| 272 | 'secondFactors' => $secondFactors, |
||
| 273 | ]; |
||
| 274 | } |
||
| 275 | |||
| 276 | public function verifyGssfAction(Request $request) |
||
| 277 | { |
||
| 278 | if (!$request->get('authenticationMode', false)) { |
||
| 279 | throw new RuntimeException('Unable to determine the authentication mode in the GSSP verification action'); |
||
| 280 | } |
||
| 281 | $authenticationMode = $request->get('authenticationMode'); |
||
| 282 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 283 | $context = $this->getResponseContext($authenticationMode); |
||
| 284 | |||
| 285 | $originalRequestId = $context->getInResponseTo(); |
||
| 286 | |||
| 287 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 288 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 289 | $logger->info('Received request to verify GSSF'); |
||
| 290 | |||
| 291 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
| 292 | |||
| 293 | $logger->info(sprintf( |
||
| 294 | 'Selected GSSF "%s" for verfication, forwarding to Saml handling', |
||
| 295 | $selectedSecondFactor |
||
| 296 | )); |
||
| 297 | |||
| 298 | /** @var \Surfnet\StepupGateway\GatewayBundle\Service\SecondFactorService $secondFactorService */ |
||
| 299 | $secondFactorService = $this->get('gateway.service.second_factor_service'); |
||
| 300 | /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor */ |
||
| 301 | $secondFactor = $secondFactorService->findByUuid($selectedSecondFactor); |
||
| 302 | if (!$secondFactor) { |
||
| 303 | throw new RuntimeException(sprintf( |
||
| 304 | 'Requested verification of GSSF "%s", however that Second Factor no longer exists', |
||
| 305 | $selectedSecondFactor |
||
| 306 | )); |
||
| 307 | } |
||
| 308 | |||
| 309 | // Also send the response context service id, as later we need to know if this is regular SSO or SFO authn. |
||
| 310 | $responseContextServiceId = $context->getResponseContextServiceId(); |
||
| 311 | |||
| 312 | return $this->forward( |
||
| 313 | 'SurfnetStepupGatewaySamlStepupProviderBundle:SamlProxy:sendSecondFactorVerificationAuthnRequest', |
||
| 314 | [ |
||
| 315 | 'provider' => $secondFactor->secondFactorType, |
||
| 316 | 'subjectNameId' => $secondFactor->secondFactorIdentifier, |
||
| 317 | 'responseContextServiceId' => $responseContextServiceId, |
||
| 318 | ] |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | |||
| 322 | public function gssfVerifiedAction(Request $request) |
||
| 323 | { |
||
| 324 | $authenticationMode = $request->get('authenticationMode'); |
||
| 325 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 326 | $context = $this->getResponseContext($authenticationMode); |
||
| 327 | |||
| 328 | $originalRequestId = $context->getInResponseTo(); |
||
| 329 | |||
| 330 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 331 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 332 | $logger->info('Attempting to mark GSSF as verified'); |
||
| 333 | |||
| 334 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
| 335 | |||
| 336 | /** @var \Surfnet\StepupGateway\GatewayBundle\Entity\SecondFactor $secondFactor */ |
||
| 337 | $secondFactor = $this->get('gateway.service.second_factor_service')->findByUuid($selectedSecondFactor); |
||
| 338 | if (!$secondFactor) { |
||
| 339 | throw new RuntimeException( |
||
| 340 | sprintf( |
||
| 341 | 'Verification of GSSF "%s" succeeded, however that Second Factor no longer exists', |
||
| 342 | $selectedSecondFactor |
||
| 343 | ) |
||
| 344 | ); |
||
| 345 | } |
||
| 346 | |||
| 347 | $this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId, $authenticationMode); |
||
| 348 | $context->markSecondFactorVerified(); |
||
| 349 | |||
| 350 | $logger->info(sprintf( |
||
| 351 | 'Marked GSSF "%s" as verified, forwarding to Gateway controller to respond', |
||
| 352 | $selectedSecondFactor |
||
| 353 | )); |
||
| 354 | return $this->forward($context->getResponseAction()); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Template |
||
| 359 | * @param Request $request |
||
| 360 | * @return array|Response |
||
| 361 | */ |
||
| 362 | public function verifyYubiKeySecondFactorAction(Request $request) |
||
| 363 | { |
||
| 364 | if (!$request->get('authenticationMode', false)) { |
||
| 365 | throw new RuntimeException('Unable to determine the authentication mode in Yubikey verification action'); |
||
| 366 | } |
||
| 367 | $authenticationMode = $request->get('authenticationMode'); |
||
| 368 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 369 | $context = $this->getResponseContext($authenticationMode); |
||
| 370 | $originalRequestId = $context->getInResponseTo(); |
||
| 371 | |||
| 372 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 373 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 374 | |||
| 375 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
| 376 | |||
| 377 | $logger->notice('Verifying possession of Yubikey second factor'); |
||
| 378 | |||
| 379 | $command = new VerifyYubikeyOtpCommand(); |
||
| 380 | $command->secondFactorId = $selectedSecondFactor; |
||
| 381 | |||
| 382 | $form = $this->createForm(VerifyYubikeyOtpType::class, $command)->handleRequest($request); |
||
| 383 | $cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
||
| 384 | |||
| 385 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 386 | $result = $this->getStepupService()->verifyYubikeyOtp($command); |
||
| 387 | if ($result->didOtpVerificationFail()) { |
||
| 388 | $form->addError( |
||
| 389 | new FormError($this->get('translator')->trans('gateway.form.verify_yubikey.otp_verification_failed')) |
||
| 390 | ); |
||
| 391 | |||
| 392 | // OTP field is rendered empty in the template. |
||
| 393 | return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
||
| 394 | } elseif (!$result->didPublicIdMatch()) { |
||
| 395 | $form->addError( |
||
| 396 | new FormError($this->get('translator')->trans('gateway.form.verify_yubikey.public_id_mismatch')) |
||
| 397 | ); |
||
| 398 | |||
| 399 | // OTP field is rendered empty in the template. |
||
| 400 | return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
||
| 401 | } |
||
| 402 | |||
| 403 | $this->getResponseContext($authenticationMode)->markSecondFactorVerified(); |
||
| 404 | $this->getAuthenticationLogger()->logSecondFactorAuthentication($originalRequestId, $authenticationMode); |
||
| 405 | |||
| 406 | $logger->info( |
||
| 407 | sprintf( |
||
| 408 | 'Marked Yubikey Second Factor "%s" as verified, forwarding to Saml Proxy to respond', |
||
| 409 | $selectedSecondFactor |
||
| 410 | ) |
||
| 411 | ); |
||
| 412 | return $this->forward($context->getResponseAction()); |
||
| 413 | } |
||
| 414 | |||
| 415 | // OTP field is rendered empty in the template. |
||
| 416 | return ['form' => $form->createView(), 'cancelForm' => $cancelForm->createView()]; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @Template |
||
| 421 | * @param Request $request |
||
| 422 | * @param string $authenticationMode |
||
| 423 | * @return array|Response |
||
| 424 | */ |
||
| 425 | public function verifySmsSecondFactorAction(Request $request) |
||
| 426 | { |
||
| 427 | if (!$request->get('authenticationMode', false)) { |
||
| 428 | throw new RuntimeException('Unable to determine the authentication mode in the SMS verification action'); |
||
| 429 | } |
||
| 430 | $authenticationMode = $request->get('authenticationMode'); |
||
| 431 | $this->supportsAuthenticationMode($authenticationMode); |
||
| 432 | $context = $this->getResponseContext($authenticationMode); |
||
| 433 | $originalRequestId = $context->getInResponseTo(); |
||
| 434 | |||
| 435 | /** @var \Surfnet\SamlBundle\Monolog\SamlAuthenticationLogger $logger */ |
||
| 436 | $logger = $this->get('surfnet_saml.logger')->forAuthentication($originalRequestId); |
||
| 437 | |||
| 438 | $selectedSecondFactor = $this->getSelectedSecondFactor($context, $logger); |
||
| 439 | |||
| 440 | $logger->notice('Verifying possession of SMS second factor, preparing to send'); |
||
| 441 | |||
| 442 | $command = new SendSmsChallengeCommand(); |
||
| 443 | $command->secondFactorId = $selectedSecondFactor; |
||
| 444 | |||
| 445 | $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request); |
||
| 446 | $cancelForm = $this->buildCancelAuthenticationForm($authenticationMode)->handleRequest($request); |
||
| 447 | |||
| 448 | $stepupService = $this->getStepupService(); |
||
| 449 | $phoneNumber = InternationalPhoneNumber::fromStringFormat( |
||
| 450 | $stepupService->getSecondFactorIdentifier($selectedSecondFactor) |
||
| 451 | ); |
||
| 452 | |||
| 453 | $otpRequestsRemaining = $stepupService->getSmsOtpRequestsRemainingCount($selectedSecondFactor); |
||
| 454 | $maximumOtpRequests = $stepupService->getSmsMaximumOtpRequestsCount(); |
||
| 455 | $viewVariables = ['otpRequestsRemaining' => $otpRequestsRemaining, 'maximumOtpRequests' => $maximumOtpRequests]; |
||
| 456 | |||
| 457 | if ($form->isSubmitted() && !$form->isValid()) { |
||
| 458 | return array_merge( |
||
| 459 | $viewVariables, |
||
| 460 | [ |
||
| 461 | 'phoneNumber' => $phoneNumber, |
||
| 462 | 'form' => $form->createView(), |
||
| 463 | 'cancelForm' => $cancelForm->createView() |
||
| 464 | ] |
||
| 465 | ); |
||
| 466 | } |
||
| 467 | |||
| 468 | $logger->notice('Verifying possession of SMS second factor, sending challenge per SMS'); |
||
| 469 | |||
| 470 | if (!$stepupService->sendSmsChallenge($command)) { |
||
| 471 | $form->addError( |
||
| 472 | new FormError($this->get('translator')->trans('gateway.form.send_sms_challenge.sms_sending_failed')) |
||
| 473 | ); |
||
| 474 | |||
| 475 | return array_merge( |
||
| 476 | $viewVariables, |
||
| 477 | [ |
||
| 478 | 'phoneNumber' => $phoneNumber, |
||
| 479 | 'form' => $form->createView(), |
||
| 480 | 'cancelForm' => $cancelForm->createView() |
||
| 481 | ] |
||
| 482 | ); |
||
| 483 | } |
||
| 484 | return $this->redirect( |
||
| 485 | $this->generateUrl( |
||
| 486 | 'gateway_verify_second_factor_sms_verify_challenge', |
||
| 487 | ['authenticationMode' => $authenticationMode] |
||
| 488 | ) |
||
| 489 | ); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @Template |
||
| 494 | * @param Request $request |
||
| 495 | * @param string $authenticationMode |
||
| 496 | * @return array|Response |
||
| 497 | */ |
||
| 498 | public function verifySmsSecondFactorChallengeAction(Request $request) |
||
| 555 | } |
||
| 556 | |||
| 557 | public function cancelAuthenticationAction() |
||
| 558 | { |
||
| 559 | return $this->forward('SurfnetStepupGatewayGatewayBundle:Gateway:sendAuthenticationCancelledByUser'); |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return \Surfnet\StepupGateway\GatewayBundle\Service\StepupAuthenticationService |
||
| 564 | */ |
||
| 565 | private function getStepupService() |
||
| 566 | { |
||
| 567 | return $this->get('gateway.service.stepup_authentication'); |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * @return ResponseContext |
||
| 572 | */ |
||
| 573 | private function getResponseContext($authenticationMode) |
||
| 574 | { |
||
| 575 | switch ($authenticationMode) { |
||
| 576 | case self::MODE_SFO: |
||
| 577 | return $this->get($this->get('gateway.proxy.sfo.state_handler')->getResponseContextServiceId()); |
||
| 578 | break; |
||
| 579 | case self::MODE_SSO: |
||
| 580 | return $this->get($this->get('gateway.proxy.sso.state_handler')->getResponseContextServiceId()); |
||
| 581 | break; |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @return \Surfnet\StepupGateway\GatewayBundle\Monolog\Logger\AuthenticationLogger |
||
| 587 | */ |
||
| 588 | private function getAuthenticationLogger() |
||
| 589 | { |
||
| 590 | return $this->get('gateway.authentication_logger'); |
||
| 591 | } |
||
| 592 | |||
| 593 | private function getSecondFactorService(): SecondFactorService |
||
| 594 | { |
||
| 595 | return $this->get('gateway.service.second_factor_service'); |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * @param ResponseContext $context |
||
| 600 | * @param LoggerInterface $logger |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | private function getSelectedSecondFactor(ResponseContext $context, LoggerInterface $logger) |
||
| 604 | { |
||
| 605 | $selectedSecondFactor = $context->getSelectedSecondFactor(); |
||
| 606 | |||
| 607 | if (!$selectedSecondFactor) { |
||
| 608 | $logger->error('Cannot verify possession of an unknown second factor'); |
||
| 609 | |||
| 610 | throw new BadRequestHttpException('Cannot verify possession of an unknown second factor.'); |
||
| 611 | } |
||
| 612 | |||
| 613 | return $selectedSecondFactor; |
||
| 614 | } |
||
| 615 | |||
| 616 | private function selectAndRedirectTo(SecondFactor $secondFactor, ResponseContext $context, $authenticationMode) |
||
| 617 | { |
||
| 618 | $context->saveSelectedSecondFactor($secondFactor); |
||
| 619 | |||
| 620 | $this->getStepupService()->clearSmsVerificationState($secondFactor->secondFactorId); |
||
| 621 | |||
| 622 | $secondFactorTypeService = $this->get('surfnet_stepup.service.second_factor_type'); |
||
| 623 | $secondFactorType = new SecondFactorType($secondFactor->secondFactorType); |
||
| 624 | |||
| 625 | $route = 'gateway_verify_second_factor_'; |
||
| 626 | if ($secondFactorTypeService->isGssf($secondFactorType)) { |
||
| 627 | $route .= 'gssf'; |
||
| 628 | } else { |
||
| 629 | $route .= strtolower($secondFactor->secondFactorType); |
||
| 630 | } |
||
| 631 | |||
| 632 | return $this->redirect($this->generateUrl($route, ['authenticationMode' => $authenticationMode])); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param string $authenticationMode |
||
| 637 | * @return FormInterface |
||
| 638 | */ |
||
| 639 | private function buildCancelAuthenticationForm($authenticationMode) |
||
| 650 | ); |
||
| 651 | } |
||
| 652 | |||
| 653 | private function supportsAuthenticationMode($authenticationMode) |
||
| 654 | { |
||
| 655 | if (!($authenticationMode === self::MODE_SSO || $authenticationMode === self::MODE_SFO)) { |
||
| 656 | throw new InvalidArgumentException('Invalid authentication mode requested'); |
||
| 657 | } |
||
| 658 | } |
||
| 659 | } |
||
| 660 |