vendor/symfony/security-core/Authentication/Provider/UserAuthenticationProvider.php line 26

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\Core\Authentication\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  17. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  18. use Symfony\Component\Security\Core\Exception\CustomUserMessageAccountStatusException;
  19. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  20. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  21. use Symfony\Component\Security\Core\User\UserInterface;
  22. trigger_deprecation('symfony/security-core''5.3''The "%s" class is deprecated, use the new authenticator system instead.'UserAuthenticationProvider::class);
  23. /**
  24.  * UserProviderInterface retrieves users for UsernamePasswordToken tokens.
  25.  *
  26.  * @author Fabien Potencier <fabien@symfony.com>
  27.  *
  28.  * @deprecated since Symfony 5.3, use the new authenticator system instead
  29.  */
  30. abstract class UserAuthenticationProvider implements AuthenticationProviderInterface
  31. {
  32.     private $hideUserNotFoundExceptions;
  33.     private $userChecker;
  34.     private $providerKey;
  35.     /**
  36.      * @throws \InvalidArgumentException
  37.      */
  38.     public function __construct(UserCheckerInterface $userCheckerstring $providerKeybool $hideUserNotFoundExceptions true)
  39.     {
  40.         if (empty($providerKey)) {
  41.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  42.         }
  43.         $this->userChecker $userChecker;
  44.         $this->providerKey $providerKey;
  45.         $this->hideUserNotFoundExceptions $hideUserNotFoundExceptions;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function authenticate(TokenInterface $token)
  51.     {
  52.         if (!$this->supports($token)) {
  53.             throw new AuthenticationException('The token is not supported by this authentication provider.');
  54.         }
  55.         $username method_exists($token'getUserIdentifier') ? $token->getUserIdentifier() : $token->getUsername();
  56.         if ('' === $username || null === $username) {
  57.             $username AuthenticationProviderInterface::USERNAME_NONE_PROVIDED;
  58.         }
  59.         try {
  60.             $user $this->retrieveUser($username$token);
  61.         } catch (UserNotFoundException $e) {
  62.             if ($this->hideUserNotFoundExceptions) {
  63.                 throw new BadCredentialsException('Bad credentials.'0$e);
  64.             }
  65.             $e->setUserIdentifier($username);
  66.             throw $e;
  67.         }
  68.         if (!$user instanceof UserInterface) {
  69.             throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
  70.         }
  71.         try {
  72.             $this->userChecker->checkPreAuth($user);
  73.             $this->checkAuthentication($user$token);
  74.             $this->userChecker->checkPostAuth($user);
  75.         } catch (AccountStatusException|BadCredentialsException $e) {
  76.             if ($this->hideUserNotFoundExceptions && !$e instanceof CustomUserMessageAccountStatusException) {
  77.                 throw new BadCredentialsException('Bad credentials.'0$e);
  78.             }
  79.             throw $e;
  80.         }
  81.         if ($token instanceof SwitchUserToken) {
  82.             $authenticatedToken = new SwitchUserToken($user$token->getCredentials(), $this->providerKey$user->getRoles(), $token->getOriginalToken());
  83.         } else {
  84.             $authenticatedToken = new UsernamePasswordToken($user$token->getCredentials(), $this->providerKey$user->getRoles());
  85.         }
  86.         $authenticatedToken->setAttributes($token->getAttributes());
  87.         return $authenticatedToken;
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function supports(TokenInterface $token)
  93.     {
  94.         return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getFirewallName();
  95.     }
  96.     /**
  97.      * Retrieves the user from an implementation-specific location.
  98.      *
  99.      * @return UserInterface
  100.      *
  101.      * @throws AuthenticationException if the credentials could not be validated
  102.      */
  103.     abstract protected function retrieveUser(string $usernameUsernamePasswordToken $token);
  104.     /**
  105.      * Does additional checks on the user and token (like validating the
  106.      * credentials).
  107.      *
  108.      * @throws AuthenticationException if the credentials could not be validated
  109.      */
  110.     abstract protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token);
  111. }