vendor/symfony/security-guard/Provider/GuardAuthenticationProvider.php line 32

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\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException;
  17. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  18. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  19. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  20. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  21. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  22. use Symfony\Component\Security\Core\User\UserInterface;
  23. use Symfony\Component\Security\Core\User\UserProviderInterface;
  24. use Symfony\Component\Security\Guard\AuthenticatorInterface;
  25. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  26. use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
  27. use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
  28. trigger_deprecation('symfony/security-guard''5.3''The "%s" class is deprecated, use the new authenticator system instead.'GuardAuthenticationProvider::class);
  29. /**
  30.  * Responsible for accepting the PreAuthenticationGuardToken and calling
  31.  * the correct authenticator to retrieve the authenticated token.
  32.  *
  33.  * @author Ryan Weaver <ryan@knpuniversity.com>
  34.  *
  35.  * @deprecated since Symfony 5.3, use the new authenticator system instead
  36.  */
  37. class GuardAuthenticationProvider implements AuthenticationProviderInterface
  38. {
  39.     /**
  40.      * @var AuthenticatorInterface[]
  41.      */
  42.     private $guardAuthenticators;
  43.     private $userProvider;
  44.     private $providerKey;
  45.     private $userChecker;
  46.     private $passwordHasher;
  47.     /**
  48.      * @param iterable|AuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationListener
  49.      * @param string                            $providerKey         The provider (i.e. firewall) key
  50.      * @param UserPasswordHasherInterface       $passwordHasher
  51.      */
  52.     public function __construct(iterable $guardAuthenticatorsUserProviderInterface $userProviderstring $providerKeyUserCheckerInterface $userChecker$passwordHasher null)
  53.     {
  54.         $this->guardAuthenticators $guardAuthenticators;
  55.         $this->userProvider $userProvider;
  56.         $this->providerKey $providerKey;
  57.         $this->userChecker $userChecker;
  58.         $this->passwordHasher $passwordHasher;
  59.         if ($passwordHasher instanceof UserPasswordEncoderInterface) {
  60.             trigger_deprecation('symfony/security-core''5.3'sprintf('Passing a "%s" instance to the "%s" constructor is deprecated, use "%s" instead.'UserPasswordEncoderInterface::class, __CLASS__UserPasswordHasherInterface::class));
  61.         }
  62.     }
  63.     /**
  64.      * Finds the correct authenticator for the token and calls it.
  65.      *
  66.      * @param GuardTokenInterface $token
  67.      *
  68.      * @return TokenInterface
  69.      */
  70.     public function authenticate(TokenInterface $token)
  71.     {
  72.         if (!$token instanceof GuardTokenInterface) {
  73.             throw new \InvalidArgumentException('GuardAuthenticationProvider only supports GuardTokenInterface.');
  74.         }
  75.         if (!$token instanceof PreAuthenticationGuardToken) {
  76.             /*
  77.              * The listener *only* passes PreAuthenticationGuardToken instances.
  78.              * This means that an authenticated token (e.g. PostAuthenticationGuardToken)
  79.              * is being passed here, which happens if that token becomes
  80.              * "not authenticated" (e.g. happens if the user changes between
  81.              * requests). In this case, the user should be logged out, so
  82.              * we will return an AnonymousToken to accomplish that.
  83.              */
  84.             // this should never happen - but technically, the token is
  85.             // authenticated... so it could just be returned
  86.             if ($token->isAuthenticated()) {
  87.                 return $token;
  88.             }
  89.             // this causes the user to be logged out
  90.             throw new AuthenticationExpiredException();
  91.         }
  92.         $guardAuthenticator $this->findOriginatingAuthenticator($token);
  93.         if (null === $guardAuthenticator) {
  94.             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));
  95.         }
  96.         return $this->authenticateViaGuard($guardAuthenticator$token);
  97.     }
  98.     private function authenticateViaGuard(AuthenticatorInterface $guardAuthenticatorPreAuthenticationGuardToken $token): GuardTokenInterface
  99.     {
  100.         // get the user from the GuardAuthenticator
  101.         $user $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
  102.         if (null === $user) {
  103.             $e = new UserNotFoundException(sprintf('Null returned from "%s::getUser()".'get_debug_type($guardAuthenticator)));
  104.             // @deprecated since Symfony 5.3, change to $token->getUserIdentifier() in 6.0
  105.             $e->setUserIdentifier(method_exists($token'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername());
  106.             throw $e;
  107.         }
  108.         if (!$user instanceof UserInterface) {
  109.             throw new \UnexpectedValueException(sprintf('The "%s::getUser()" method must return a UserInterface. You returned "%s".'get_debug_type($guardAuthenticator), get_debug_type($user)));
  110.         }
  111.         if ($guardAuthenticator instanceof PasswordAuthenticatedInterface && !$user instanceof PasswordAuthenticatedUserInterface) {
  112.             trigger_deprecation('symfony/security-guard''5.3''Not implementing the "%s" interface in class "%s" while using password-based guard authenticators is deprecated.'PasswordAuthenticatedUserInterface::class, get_debug_type($user));
  113.         }
  114.         $this->userChecker->checkPreAuth($user);
  115.         if (true !== $checkCredentialsResult $guardAuthenticator->checkCredentials($token->getCredentials(), $user)) {
  116.             if (false !== $checkCredentialsResult) {
  117.                 throw new \TypeError(sprintf('"%s::checkCredentials()" must return a boolean value.'get_debug_type($guardAuthenticator)));
  118.             }
  119.             throw new BadCredentialsException(sprintf('Authentication failed because "%s::checkCredentials()" did not return true.'get_debug_type($guardAuthenticator)));
  120.         }
  121.         if ($this->userProvider instanceof PasswordUpgraderInterface && $guardAuthenticator instanceof PasswordAuthenticatedInterface && null !== $this->passwordHasher && (null !== $password $guardAuthenticator->getPassword($token->getCredentials())) && $this->passwordHasher->needsRehash($user)) {
  122.             if ($this->passwordHasher instanceof UserPasswordEncoderInterface) {
  123.                 // @deprecated since Symfony 5.3
  124.                 $this->userProvider->upgradePassword($user$this->passwordHasher->encodePassword($user$password));
  125.             } else {
  126.                 $this->userProvider->upgradePassword($user$this->passwordHasher->hashPassword($user$password));
  127.             }
  128.         }
  129.         $this->userChecker->checkPostAuth($user);
  130.         // turn the UserInterface into a TokenInterface
  131.         $authenticatedToken $guardAuthenticator->createAuthenticatedToken($user$this->providerKey);
  132.         if (!$authenticatedToken instanceof TokenInterface) {
  133.             throw new \UnexpectedValueException(sprintf('The "%s::createAuthenticatedToken()" method must return a TokenInterface. You returned "%s".'get_debug_type($guardAuthenticator), get_debug_type($authenticatedToken)));
  134.         }
  135.         return $authenticatedToken;
  136.     }
  137.     private function findOriginatingAuthenticator(PreAuthenticationGuardToken $token): ?AuthenticatorInterface
  138.     {
  139.         // find the *one* GuardAuthenticator that this token originated from
  140.         foreach ($this->guardAuthenticators as $key => $guardAuthenticator) {
  141.             // get a key that's unique to *this* guard authenticator
  142.             // this MUST be the same as GuardAuthenticationListener
  143.             $uniqueGuardKey $this->providerKey.'_'.$key;
  144.             if ($uniqueGuardKey === $token->getGuardProviderKey()) {
  145.                 return $guardAuthenticator;
  146.             }
  147.         }
  148.         // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
  149.         // instances that will be checked if you have multiple firewalls.
  150.         return null;
  151.     }
  152.     public function supports(TokenInterface $token)
  153.     {
  154.         if ($token instanceof PreAuthenticationGuardToken) {
  155.             return null !== $this->findOriginatingAuthenticator($token);
  156.         }
  157.         return $token instanceof GuardTokenInterface;
  158.     }
  159. }