| Conditions | 8 |
| Paths | 8 |
| Total Lines | 85 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 67 | private function handleEvent(GetResponseEvent $event) |
||
| 68 | { |
||
| 69 | /** @var SessionHandler $sessionHandler */ |
||
| 70 | $sessionHandler = $this->container->get('ra.security.authentication.session_handler'); |
||
| 71 | |||
| 72 | // reinstate the token from the session. Could be expanded with logout check if needed |
||
| 73 | if ($this->getTokenStorage()->getToken()) { |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** @var SamlInteractionProvider $samlInteractionProvider */ |
||
| 78 | $samlInteractionProvider = $this->container->get('ra.security.authentication.saml'); |
||
| 79 | |||
| 80 | if (!$samlInteractionProvider->isSamlAuthenticationInitiated()) { |
||
| 81 | $sessionHandler->setCurrentRequestUri($event->getRequest()->getUri()); |
||
| 82 | $event->setResponse($samlInteractionProvider->initiateSamlRequest()); |
||
| 83 | |||
| 84 | /** @var SamlAuthenticationLogger $logger */ |
||
| 85 | $logger = $this->container->get('surfnet_saml.logger')->forAuthentication($sessionHandler->getRequestId()); |
||
| 86 | $logger->notice('Sending AuthnRequest'); |
||
| 87 | |||
| 88 | return; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** @var SamlAuthenticationLogger $logger */ |
||
| 92 | $logger = $this->container->get('surfnet_saml.logger')->forAuthentication($sessionHandler->getRequestId()); |
||
| 93 | $expectedInResponseTo = $sessionHandler->getRequestId(); |
||
| 94 | try { |
||
| 95 | $assertion = $samlInteractionProvider->processSamlResponse($event->getRequest()); |
||
| 96 | } catch (PreconditionNotMetException $e) { |
||
| 97 | $logger->notice(sprintf('SAML response precondition not met: "%s"', $e->getMessage())); |
||
| 98 | $event->setResponse($this->renderPreconditionExceptionResponse($e)); |
||
| 99 | return; |
||
| 100 | } catch (Exception $e) { |
||
| 101 | $logger->error(sprintf('Failed SAMLResponse Parsing: "%s"', $e->getMessage())); |
||
| 102 | throw new AuthenticationException('Failed SAMLResponse parsing', 0, $e); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!InResponseTo::assertEquals($assertion, $expectedInResponseTo)) { |
||
| 106 | $logger->error('Unknown or unexpected InResponseTo in SAMLResponse'); |
||
| 107 | |||
| 108 | throw new AuthenticationException('Unknown or unexpected InResponseTo in SAMLResponse'); |
||
| 109 | } |
||
| 110 | |||
| 111 | $logger->notice('Successfully processed SAMLResponse, attempting to authenticate'); |
||
| 112 | |||
| 113 | $loaResolutionService = $this->container->get('surfnet_stepup.service.loa_resolution'); |
||
| 114 | $loa = $loaResolutionService->getLoa($assertion->getAuthnContextClassRef()); |
||
| 115 | |||
| 116 | $token = new SamlToken($loa); |
||
| 117 | $token->assertion = $assertion; |
||
| 118 | |||
| 119 | /** @var AuthenticationProviderManager $authenticationManager */ |
||
| 120 | $authenticationManager = $this->container->get('security.authentication.manager'); |
||
| 121 | |||
| 122 | try { |
||
| 123 | $authToken = $authenticationManager->authenticate($token); |
||
| 124 | } catch (BadCredentialsException $exception) { |
||
| 125 | $logger->error( |
||
| 126 | sprintf('Bad credentials, reason: "%s"', $exception->getMessage()), |
||
| 127 | ['exception' => $exception] |
||
| 128 | ); |
||
| 129 | |||
| 130 | $event->setResponse($this->renderBadCredentialsResponse($exception)); |
||
| 131 | return; |
||
| 132 | } catch (AuthenticationException $failed) { |
||
| 133 | $logger->error( |
||
| 134 | sprintf('Authentication Failed, reason: "%s"', $failed->getMessage()), |
||
| 135 | ['exception' => $failed] |
||
| 136 | ); |
||
| 137 | |||
| 138 | $event->setResponse($this->renderAuthenticationExceptionResponse($failed)); |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | // for the current request |
||
| 143 | $this->getTokenStorage()->setToken($authToken); |
||
| 144 | |||
| 145 | // migrate the session to prevent session hijacking |
||
| 146 | $sessionHandler->migrate(); |
||
| 147 | |||
| 148 | $event->setResponse(new RedirectResponse($sessionHandler->getCurrentRequestUri())); |
||
| 149 | |||
| 150 | $logger->notice('Authentication succeeded, redirecting to original location'); |
||
| 151 | } |
||
| 152 | |||
| 206 |