vendor/symfony/security/Core/Authentication/Provider/UserAuthenticationProvider.php line 71

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