|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Core\Authentication; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 15 | +use Symfony\Component\Security\Core\AuthenticationEvents; |
| 16 | +use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent; |
| 17 | +use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent; |
| 18 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 19 | +use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException; |
| 20 | +use Symfony\Component\Security\Core\Exception\ProviderNotFoundException; |
| 21 | +use Symfony\Component\Security\Core\User\UserCheckerInterface; |
| 22 | +use Symfony\Component\Security\Guard\AuthenticatorInterface; |
| 23 | +use Symfony\Component\Security\Guard\Provider\GuardAuthenticationProviderTrait; |
| 24 | +use Symfony\Component\Security\Guard\Token\GuardTokenInterface; |
| 25 | +use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken; |
| 26 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 27 | + |
| 28 | +class GuardAuthenticationManager implements AuthenticationManagerInterface |
| 29 | +{ |
| 30 | + use GuardAuthenticationProviderTrait; |
| 31 | + |
| 32 | + private $guardAuthenticators; |
| 33 | + private $userChecker; |
| 34 | + private $eraseCredentials; |
| 35 | + /** @var EventDispatcherInterface */ |
| 36 | + private $eventDispatcher; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param iterable|AuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationListener |
| 40 | + */ |
| 41 | + public function __construct($guardAuthenticators, UserCheckerInterface $userChecker, bool $eraseCredentials = true) |
| 42 | + { |
| 43 | + $this->guardAuthenticators = $guardAuthenticators; |
| 44 | + $this->userChecker = $userChecker; |
| 45 | + $this->eraseCredentials = $eraseCredentials; |
| 46 | + } |
| 47 | + |
| 48 | + public function setEventDispatcher(EventDispatcherInterface $dispatcher) |
| 49 | + { |
| 50 | + $this->eventDispatcher = $dispatcher; |
| 51 | + } |
| 52 | + |
| 53 | + public function authenticate(TokenInterface $token) |
| 54 | + { |
| 55 | + if (!$token instanceof GuardTokenInterface) { |
| 56 | + throw new \InvalidArgumentException('GuardAuthenticationManager only supports GuardTokenInterface.'); |
| 57 | + } |
| 58 | + |
| 59 | + if (!$token instanceof PreAuthenticationGuardToken) { |
| 60 | + /* |
| 61 | + * The listener *only* passes PreAuthenticationGuardToken instances. |
| 62 | + * This means that an authenticated token (e.g. PostAuthenticationGuardToken) |
| 63 | + * is being passed here, which happens if that token becomes |
| 64 | + * "not authenticated" (e.g. happens if the user changes between |
| 65 | + * requests). In this case, the user should be logged out. |
| 66 | + */ |
| 67 | + |
| 68 | + // this should never happen - but technically, the token is |
| 69 | + // authenticated... so it could just be returned |
| 70 | + if ($token->isAuthenticated()) { |
| 71 | + return $token; |
| 72 | + } |
| 73 | + |
| 74 | + // this AccountStatusException causes the user to be logged out |
| 75 | + throw new AuthenticationExpiredException(); |
| 76 | + } |
| 77 | + |
| 78 | + $guard = $this->findOriginatingAuthenticator($token); |
| 79 | + if (null === $guard) { |
| 80 | + $this->handleFailure(new ProviderNotFoundException(sprintf('Token with provider key "%s" did not originate from any of the guard authenticators.', $token->getGuardProviderKey())), $token); |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + $result = $this->authenticateViaGuard($guard, $token); |
| 85 | + } catch (AuthenticationException $exception) { |
| 86 | + $this->handleFailure($exception, $token); |
| 87 | + } |
| 88 | + |
| 89 | + if (true === $this->eraseCredentials) { |
| 90 | + $result->eraseCredentials(); |
| 91 | + } |
| 92 | + |
| 93 | + if (null !== $this->eventDispatcher) { |
| 94 | + $this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($result), AuthenticationEvents::AUTHENTICATION_SUCCESS); |
| 95 | + } |
| 96 | + |
| 97 | + return $result; |
| 98 | + } |
| 99 | + |
| 100 | + private function handleFailure(AuthenticationException $exception, TokenInterface $token) |
| 101 | + { |
| 102 | + if (null !== $this->eventDispatcher) { |
| 103 | + $this->eventDispatcher->dispatch(new AuthenticationFailureEvent($token, $exception), AuthenticationEvents::AUTHENTICATION_FAILURE); |
| 104 | + } |
| 105 | + |
| 106 | + $exception->setToken($token); |
| 107 | + |
| 108 | + throw $exception; |
| 109 | + } |
| 110 | + |
| 111 | + protected function getGuardKey(string $key): string |
| 112 | + { |
| 113 | + // Guard authenticators in the GuardAuthenticationManager are already indexed |
| 114 | + // by an unique key |
| 115 | + return $key; |
| 116 | + } |
| 117 | +} |
0 commit comments