vendor/symfony/security/Guard/Provider/GuardAuthenticationProvider.php line 102

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Guard\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException;
  15. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  16. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  17. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. use Symfony\Component\Security\Core\User\UserProviderInterface;
  20. use Symfony\Component\Security\Guard\AuthenticatorInterface;
  21. use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
  22. use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
  23. /**
  24.  * Responsible for accepting the PreAuthenticationGuardToken and calling
  25.  * the correct authenticator to retrieve the authenticated token.
  26.  *
  27.  * @author Ryan Weaver <ryan@knpuniversity.com>
  28.  */
  29. class GuardAuthenticationProvider implements AuthenticationProviderInterface
  30. {
  31.     /**
  32.      * @var AuthenticatorInterface[]
  33.      */
  34.     private $guardAuthenticators;
  35.     private $userProvider;
  36.     private $providerKey;
  37.     private $userChecker;
  38.     /**
  39.      * @param iterable|AuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationListener
  40.      * @param UserProviderInterface             $userProvider        The user provider
  41.      * @param string                            $providerKey         The provider (i.e. firewall) key
  42.      * @param UserCheckerInterface              $userChecker
  43.      */
  44.     public function __construct($guardAuthenticatorsUserProviderInterface $userProviderstring $providerKeyUserCheckerInterface $userChecker)
  45.     {
  46.         $this->guardAuthenticators $guardAuthenticators;
  47.         $this->userProvider $userProvider;
  48.         $this->providerKey $providerKey;
  49.         $this->userChecker $userChecker;
  50.     }
  51.     /**
  52.      * Finds the correct authenticator for the token and calls it.
  53.      *
  54.      * @param GuardTokenInterface $token
  55.      *
  56.      * @return TokenInterface
  57.      */
  58.     public function authenticate(TokenInterface $token)
  59.     {
  60.         if (!$token instanceof GuardTokenInterface) {
  61.             throw new \InvalidArgumentException('GuardAuthenticationProvider only supports GuardTokenInterface.');
  62.         }
  63.         if (!$token instanceof PreAuthenticationGuardToken) {
  64.             /*
  65.              * The listener *only* passes PreAuthenticationGuardToken instances.
  66.              * This means that an authenticated token (e.g. PostAuthenticationGuardToken)
  67.              * is being passed here, which happens if that token becomes
  68.              * "not authenticated" (e.g. happens if the user changes between
  69.              * requests). In this case, the user should be logged out, so
  70.              * we will return an AnonymousToken to accomplish that.
  71.              */
  72.             // this should never happen - but technically, the token is
  73.             // authenticated... so it could just be returned
  74.             if ($token->isAuthenticated()) {
  75.                 return $token;
  76.             }
  77.             // this AccountStatusException causes the user to be logged out
  78.             throw new AuthenticationExpiredException();
  79.         }
  80.         $guardAuthenticator $this->findOriginatingAuthenticator($token);
  81.         if (null === $guardAuthenticator) {
  82.             throw new AuthenticationException(sprintf('Token with provider key "%s" did not originate from any of the guard authenticators of provider "%s".'$token->getGuardProviderKey(), $this->providerKey));
  83.         }
  84.         return $this->authenticateViaGuard($guardAuthenticator$token);
  85.     }
  86.     private function authenticateViaGuard($guardAuthenticatorPreAuthenticationGuardToken $token)
  87.     {
  88.         // get the user from the GuardAuthenticator
  89.         $user $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
  90.         if (null === $user) {
  91.             throw new UsernameNotFoundException(sprintf('Null returned from %s::getUser()', \get_class($guardAuthenticator)));
  92.         }
  93.         if (!$user instanceof UserInterface) {
  94.             throw new \UnexpectedValueException(sprintf('The %s::getUser() method must return a UserInterface. You returned %s.', \get_class($guardAuthenticator), \is_object($user) ? \get_class($user) : \gettype($user)));
  95.         }
  96.         $this->userChecker->checkPreAuth($user);
  97.         if (true !== $guardAuthenticator->checkCredentials($token->getCredentials(), $user)) {
  98.             throw new BadCredentialsException(sprintf('Authentication failed because %s::checkCredentials() did not return true.', \get_class($guardAuthenticator)));
  99.         }
  100.         $this->userChecker->checkPostAuth($user);
  101.         // turn the UserInterface into a TokenInterface
  102.         $authenticatedToken $guardAuthenticator->createAuthenticatedToken($user$this->providerKey);
  103.         if (!$authenticatedToken instanceof TokenInterface) {
  104.             throw new \UnexpectedValueException(sprintf('The %s::createAuthenticatedToken() method must return a TokenInterface. You returned %s.', \get_class($guardAuthenticator), \is_object($authenticatedToken) ? \get_class($authenticatedToken) : \gettype($authenticatedToken)));
  105.         }
  106.         return $authenticatedToken;
  107.     }
  108.     private function findOriginatingAuthenticator(PreAuthenticationGuardToken $token)
  109.     {
  110.         // find the *one* GuardAuthenticator that this token originated from
  111.         foreach ($this->guardAuthenticators as $key => $guardAuthenticator) {
  112.             // get a key that's unique to *this* guard authenticator
  113.             // this MUST be the same as GuardAuthenticationListener
  114.             $uniqueGuardKey $this->providerKey.'_'.$key;
  115.             if ($uniqueGuardKey === $token->getGuardProviderKey()) {
  116.                 return $guardAuthenticator;
  117.             }
  118.         }
  119.         // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
  120.         // instances that will be checked if you have multiple firewalls.
  121.         return null;
  122.     }
  123.     public function supports(TokenInterface $token)
  124.     {
  125.         if ($token instanceof PreAuthenticationGuardToken) {
  126.             return null !== $this->findOriginatingAuthenticator($token);
  127.         }
  128.         return $token instanceof GuardTokenInterface;
  129.     }
  130. }