| Conditions | 18 |
| Paths | 226 |
| Total Lines | 89 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 79 | public function handle(GetResponseEvent $event) |
||
| 80 | { |
||
| 81 | $request = $event->getRequest(); |
||
| 82 | |||
| 83 | $attributes = $this->shibbolethServiceProvider->getAttributes(); |
||
| 84 | |||
| 85 | if (empty($attributes)) { |
||
| 86 | $this->log('Shibboleth attributes not found'); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->log(sprintf('Shibboleth attributes found: %s', json_encode($attributes))); |
||
| 91 | |||
| 92 | if (!$this->shibbolethServiceProvider->isAuthenticated()) { |
||
| 93 | $this->log('Shibboleth has not authenticated your request.'); |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | |||
| 97 | $username = $this->shibbolethServiceProvider->getUsername(); |
||
| 98 | |||
| 99 | if (empty($username)) { |
||
| 100 | $this->log('Username not found'); |
||
| 101 | return; |
||
| 102 | } |
||
| 103 | |||
| 104 | $this->log(sprintf('Username found: %s', $username)); |
||
| 105 | $token = $this->tokenStorage->getToken(); |
||
| 106 | |||
| 107 | if (!empty($token)) { |
||
| 108 | if ($token instanceof KuleuvenUserToken && $token->isAuthenticated()) { |
||
| 109 | $this->log(sprintf('Token found: %s', $token)); |
||
| 110 | if ($token->getUsername() === $username && count($token->getRoles()) === count($token->getUser()->getRoles())) { |
||
| 111 | $this->log(sprintf('Token authenticated for username "%s": %s', $username, $token)); |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | $roles = $token->getRoles(); |
||
| 115 | foreach ($roles as $role) { |
||
| 116 | if ($role instanceof SwitchUserRole) { |
||
| 117 | if ($role->getSource()->getUser()->getUsername() === $username) { |
||
| 118 | $this->log(sprintf('Token authenticated for username "%s", impersonating "%s": %s', $username, $token->getUsername(), $token)); |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | try { |
||
| 128 | $token = new KuleuvenUserToken( |
||
| 129 | $username, |
||
| 130 | $attributes, |
||
| 131 | $this->providerKey, |
||
| 132 | $this->defaultRoles |
||
| 133 | ); |
||
| 134 | $this->log(sprintf('Token created for username "%s": %s', $username, $token)); |
||
| 135 | |||
| 136 | $authenticationToken = $this->authenticationManager->authenticate($token); |
||
| 137 | if ($authenticationToken instanceof TokenInterface) { |
||
| 138 | $this->log(sprintf('Set authentication token: %s', $authenticationToken)); |
||
| 139 | $this->tokenStorage->setToken($authenticationToken); |
||
| 140 | if (null !== $this->eventDispatcher) { |
||
| 141 | $loginEvent = new InteractiveLoginEvent($request, $authenticationToken); |
||
| 142 | $this->log('Dispatch login event'); |
||
| 143 | $this->eventDispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $loginEvent); |
||
| 144 | } |
||
| 145 | } elseif ($authenticationToken instanceof Response) { |
||
| 146 | $this->log('Using authentication token as response...'); |
||
| 147 | $event->setResponse($authenticationToken); |
||
| 148 | } |
||
| 149 | } catch (AuthenticationException $failed) { |
||
| 150 | $this->log(sprintf('Authentication request failed for username "%s": %s', $username, $failed->getMessage())); |
||
| 151 | |||
| 152 | $token = $this->tokenStorage->getToken(); |
||
| 153 | if ($token instanceof KuleuvenUserToken) { |
||
| 154 | $this->tokenStorage->setToken(null); |
||
| 155 | $this->log(sprintf('Token removed from storage', $token)); |
||
| 156 | } |
||
| 157 | |||
| 158 | try { |
||
| 159 | $event->setResponse($this->authenticationEntryPoint->start($request, $failed)); |
||
| 160 | } catch (AuthenticationException $failed) { |
||
| 161 | $this->log('Entry point failed, sending forbidden response...'); |
||
| 162 | $response = (new Response()); |
||
| 163 | $response->setStatusCode(Response::HTTP_FORBIDDEN); |
||
| 164 | $event->setResponse($response); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 |