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

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\AuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  16. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  17. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  18. use Symfony\Component\Security\Core\Role\SwitchUserRole;
  19. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. /**
  22.  * UserProviderInterface retrieves users for UsernamePasswordToken tokens.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. abstract class UserAuthenticationProvider implements AuthenticationProviderInterface
  27. {
  28.     private $hideUserNotFoundExceptions;
  29.     private $userChecker;
  30.     private $providerKey;
  31.     /**
  32.      * @throws \InvalidArgumentException
  33.      */
  34.     public function __construct(UserCheckerInterface $userCheckerstring $providerKeybool $hideUserNotFoundExceptions true)
  35.     {
  36.         if (empty($providerKey)) {
  37.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  38.         }
  39.         $this->userChecker $userChecker;
  40.         $this->providerKey $providerKey;
  41.         $this->hideUserNotFoundExceptions $hideUserNotFoundExceptions;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function authenticate(TokenInterface $token)
  47.     {
  48.         if (!$this->supports($token)) {
  49.             throw new AuthenticationException('The token is not supported by this authentication provider.');
  50.         }
  51.         $username $token->getUsername();
  52.         if ('' === $username || null === $username) {
  53.             $username AuthenticationProviderInterface::USERNAME_NONE_PROVIDED;
  54.         }
  55.         try {
  56.             $user $this->retrieveUser($username$token);
  57.         } catch (UsernameNotFoundException $e) {
  58.             if ($this->hideUserNotFoundExceptions) {
  59.                 throw new BadCredentialsException('Bad credentials.'0$e);
  60.             }
  61.             $e->setUsername($username);
  62.             throw $e;
  63.         }
  64.         if (!$user instanceof UserInterface) {
  65.             throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
  66.         }
  67.         try {
  68.             $this->userChecker->checkPreAuth($user);
  69.             $this->checkAuthentication($user$token);
  70.             $this->userChecker->checkPostAuth($user);
  71.         } catch (BadCredentialsException $e) {
  72.             if ($this->hideUserNotFoundExceptions) {
  73.                 throw new BadCredentialsException('Bad credentials.'0$e);
  74.             }
  75.             throw $e;
  76.         }
  77.         if ($token instanceof SwitchUserToken) {
  78.             $authenticatedToken = new SwitchUserToken($user$token->getCredentials(), $this->providerKey$this->getRoles($user$token), $token->getOriginalToken());
  79.         } else {
  80.             $authenticatedToken = new UsernamePasswordToken($user$token->getCredentials(), $this->providerKey$this->getRoles($user$token));
  81.         }
  82.         $authenticatedToken->setAttributes($token->getAttributes());
  83.         return $authenticatedToken;
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function supports(TokenInterface $token)
  89.     {
  90.         return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
  91.     }
  92.     /**
  93.      * Retrieves roles from user and appends SwitchUserRole if original token contained one.
  94.      */
  95.     private function getRoles(UserInterface $userTokenInterface $token): array
  96.     {
  97.         $roles $user->getRoles();
  98.         foreach ($token->getRoles(false) as $role) {
  99.             if ($role instanceof SwitchUserRole) {
  100.                 $roles[] = $role;
  101.                 break;
  102.             }
  103.         }
  104.         return $roles;
  105.     }
  106.     /**
  107.      * Retrieves the user from an implementation-specific location.
  108.      *
  109.      * @param string $username The username to retrieve
  110.      *
  111.      * @return UserInterface The user
  112.      *
  113.      * @throws AuthenticationException if the credentials could not be validated
  114.      */
  115.     abstract protected function retrieveUser($usernameUsernamePasswordToken $token);
  116.     /**
  117.      * Does additional checks on the user and token (like validating the
  118.      * credentials).
  119.      *
  120.      * @throws AuthenticationException if the credentials could not be validated
  121.      */
  122.     abstract protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token);
  123. }