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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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 <[email protected]>
  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.             $roles $user->getRoles();
  83.             $roles[] = 'ROLE_PREVIOUS_ADMIN';
  84.             $authenticatedToken = new SwitchUserToken($user$token->getCredentials(), $this->providerKey$roles$token->getOriginalToken());
  85.         } else {
  86.             $authenticatedToken = new UsernamePasswordToken($user$token->getCredentials(), $this->providerKey$user->getRoles());
  87.         }
  88.         $authenticatedToken->setAttributes($token->getAttributes());
  89.         return $authenticatedToken;
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     public function supports(TokenInterface $token)
  95.     {
  96.         return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getFirewallName();
  97.     }
  98.     /**
  99.      * Retrieves the user from an implementation-specific location.
  100.      *
  101.      * @return UserInterface
  102.      *
  103.      * @throws AuthenticationException if the credentials could not be validated
  104.      */
  105.     abstract protected function retrieveUser(string $usernameUsernamePasswordToken $token);
  106.     /**
  107.      * Does additional checks on the user and token (like validating the
  108.      * credentials).
  109.      *
  110.      * @throws AuthenticationException if the credentials could not be validated
  111.      */
  112.     abstract protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token);
  113. }