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