|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Guard\Firewall; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
| 17 | +use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; |
| 18 | +use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken; |
| 19 | +use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; |
| 20 | +use Symfony\Component\Security\Guard\GuardAuthenticatorInterface; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 23 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 24 | +use Symfony\Component\Security\Http\Firewall\ListenerInterface; |
| 25 | +use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Authentication listener for the "guard" system. |
| 29 | + * |
| 30 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 31 | + */ |
| 32 | +class GuardAuthenticationListener implements ListenerInterface |
| 33 | +{ |
| 34 | + private $guardHandler; |
| 35 | + private $authenticationManager; |
| 36 | + private $providerKey; |
| 37 | + private $guardAuthenticators; |
| 38 | + private $logger; |
| 39 | + private $rememberMeServices; |
| 40 | + |
| 41 | + /** |
| 42 | + * @param GuardAuthenticatorHandler $guardHandler The Guard handler |
| 43 | + * @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance |
| 44 | + * @param string $providerKey The provider (i.e. firewall) key |
| 45 | + * @param GuardAuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationProvider |
| 46 | + * @param LoggerInterface $logger A LoggerInterface instance |
| 47 | + */ |
| 48 | + public function __construct(GuardAuthenticatorHandler $guardHandler, AuthenticationManagerInterface $authenticationManager, $providerKey, array $guardAuthenticators, LoggerInterface $logger = null) |
| 49 | + { |
| 50 | + if (empty($providerKey)) { |
| 51 | + throw new \InvalidArgumentException('$providerKey must not be empty.'); |
| 52 | + } |
| 53 | + |
| 54 | + $this->guardHandler = $guardHandler; |
| 55 | + $this->authenticationManager = $authenticationManager; |
| 56 | + $this->providerKey = $providerKey; |
| 57 | + $this->guardAuthenticators = $guardAuthenticators; |
| 58 | + $this->logger = $logger; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Iterates over each authenticator to see if each wants to authenticate the request. |
| 63 | + * |
| 64 | + * @param GetResponseEvent $event |
| 65 | + */ |
| 66 | + public function handle(GetResponseEvent $event) |
| 67 | + { |
| 68 | + if (null !== $this->logger) { |
| 69 | + $this->logger->info('Checking for guard authentication credentials.', array('firewall_key' => $this->providerKey, 'authenticators' => count($this->guardAuthenticators))); |
| 70 | + } |
| 71 | + |
| 72 | + foreach ($this->guardAuthenticators as $key => $guardAuthenticator) { |
| 73 | + // get a key that's unique to *this* guard authenticator |
| 74 | + // this MUST be the same as GuardAuthenticationProvider |
| 75 | + $uniqueGuardKey = $this->providerKey.'_'.$key; |
| 76 | + |
| 77 | + $this->executeGuardAuthenticator($uniqueGuardKey, $guardAuthenticator, $event); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private function executeGuardAuthenticator($uniqueGuardKey, GuardAuthenticatorInterface $guardAuthenticator, GetResponseEvent $event) |
| 82 | + { |
| 83 | + $request = $event->getRequest(); |
| 84 | + try { |
| 85 | + if (null !== $this->logger) { |
| 86 | + $this->logger->info('Calling getCredentials on guard configurator.', array('firewall_key' => $this->providerKey, 'authenticator' => get_class($guardAuthenticator))); |
| 87 | + } |
| 88 | + |
| 89 | + // allow the authenticator to fetch authentication info from the request |
| 90 | + $credentials = $guardAuthenticator->getCredentials($request); |
| 91 | + |
| 92 | + // allow null to be returned to skip authentication |
| 93 | + if (null === $credentials) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // create a token with the unique key, so that the provider knows which authenticator to use |
| 98 | + $token = new PreAuthenticationGuardToken($credentials, $uniqueGuardKey); |
| 99 | + |
| 100 | + if (null !== $this->logger) { |
| 101 | + $this->logger->info('Passing guard token information to the GuardAuthenticationProvider', array('firewall_key' => $this->providerKey, 'authenticator' => get_class($guardAuthenticator))); |
| 102 | + } |
| 103 | + // pass the token into the AuthenticationManager system |
| 104 | + // this indirectly calls GuardAuthenticationProvider::authenticate() |
| 105 | + $token = $this->authenticationManager->authenticate($token); |
| 106 | + |
| 107 | + if (null !== $this->logger) { |
| 108 | + $this->logger->info('Guard authentication successful!', array('token' => $token, 'authenticator' => get_class($guardAuthenticator))); |
| 109 | + } |
| 110 | + |
| 111 | + // sets the token on the token storage, etc |
| 112 | + $this->guardHandler->authenticateWithToken($token, $request); |
| 113 | + } catch (AuthenticationException $e) { |
| 114 | + // oh no! Authentication failed! |
| 115 | + |
| 116 | + if (null !== $this->logger) { |
| 117 | + $this->logger->info('Guard authentication failed.', array('exception' => $e, 'authenticator' => get_class($guardAuthenticator))); |
| 118 | + } |
| 119 | + |
| 120 | + $response = $this->guardHandler->handleAuthenticationFailure($e, $request, $guardAuthenticator, $this->providerKey); |
| 121 | + |
| 122 | + if ($response instanceof Response) { |
| 123 | + $event->setResponse($response); |
| 124 | + } |
| 125 | + |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + // success! |
| 130 | + $response = $this->guardHandler->handleAuthenticationSuccess($token, $request, $guardAuthenticator, $this->providerKey); |
| 131 | + if ($response instanceof Response) { |
| 132 | + if (null !== $this->logger) { |
| 133 | + $this->logger->info('Guard authenticator set success response.', array('response' => $response, 'authenticator' => get_class($guardAuthenticator))); |
| 134 | + } |
| 135 | + |
| 136 | + $event->setResponse($response); |
| 137 | + } else { |
| 138 | + if (null !== $this->logger) { |
| 139 | + $this->logger->info('Guard authenticator set no success response: request continues.', array('authenticator' => get_class($guardAuthenticator))); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // attempt to trigger the remember me functionality |
| 144 | + $this->triggerRememberMe($guardAuthenticator, $request, $token, $response); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Should be called if this listener will support remember me. |
| 149 | + * |
| 150 | + * @param RememberMeServicesInterface $rememberMeServices |
| 151 | + */ |
| 152 | + public function setRememberMeServices(RememberMeServicesInterface $rememberMeServices) |
| 153 | + { |
| 154 | + $this->rememberMeServices = $rememberMeServices; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Checks to see if remember me is supported in the authenticator and |
| 159 | + * on the firewall. If it is, the RememberMeServicesInterface is notified. |
| 160 | + * |
| 161 | + * @param GuardAuthenticatorInterface $guardAuthenticator |
| 162 | + * @param Request $request |
| 163 | + * @param TokenInterface $token |
| 164 | + * @param Response $response |
| 165 | + */ |
| 166 | + private function triggerRememberMe(GuardAuthenticatorInterface $guardAuthenticator, Request $request, TokenInterface $token, Response $response = null) |
| 167 | + { |
| 168 | + if (null === $this->rememberMeServices) { |
| 169 | + if (null !== $this->logger) { |
| 170 | + $this->logger->info('Remember me skipped: it is not configured for the firewall.', array('authenticator' => get_class($guardAuthenticator))); |
| 171 | + } |
| 172 | + |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + if (!$guardAuthenticator->supportsRememberMe()) { |
| 177 | + if (null !== $this->logger) { |
| 178 | + $this->logger->info('Remember me skipped: your authenticator does not support it.', array('authenticator' => get_class($guardAuthenticator))); |
| 179 | + } |
| 180 | + |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (!$response instanceof Response) { |
| 185 | + throw new \LogicException(sprintf( |
| 186 | + '%s::onAuthenticationSuccess *must* return a Response if you want to use the remember me functionality. Return a Response, or set remember_me to false under the guard configuration.', |
| 187 | + get_class($guardAuthenticator) |
| 188 | + )); |
| 189 | + } |
| 190 | + |
| 191 | + $this->rememberMeServices->loginSuccess($request, $response, $token); |
| 192 | + } |
| 193 | +} |
0 commit comments