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

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.      * @throws \InvalidArgumentException
  32.      */
  33.     public function __construct(UserCheckerInterface $userCheckerstring $providerKeybool $hideUserNotFoundExceptions true)
  34.     {
  35.         if (empty($providerKey)) {
  36.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  37.         }
  38.         $this->userChecker $userChecker;
  39.         $this->providerKey $providerKey;
  40.         $this->hideUserNotFoundExceptions $hideUserNotFoundExceptions;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function authenticate(TokenInterface $token)
  46.     {
  47.         if (!$this->supports($token)) {
  48.             throw new AuthenticationException('The token is not supported by this authentication provider.');
  49.         }
  50.         $username $token->getUsername();
  51.         if ('' === $username || null === $username) {
  52.             $username AuthenticationProviderInterface::USERNAME_NONE_PROVIDED;
  53.         }
  54.         try {
  55.             $user $this->retrieveUser($username$token);
  56.         } catch (UsernameNotFoundException $e) {
  57.             if ($this->hideUserNotFoundExceptions) {
  58.                 throw new BadCredentialsException('Bad credentials.'0$e);
  59.             }
  60.             $e->setUsername($username);
  61.             throw $e;
  62.         }
  63.         if (!$user instanceof UserInterface) {
  64.             throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
  65.         }
  66.         try {
  67.             $this->userChecker->checkPreAuth($user);
  68.             $this->checkAuthentication($user$token);
  69.             $this->userChecker->checkPostAuth($user);
  70.         } catch (BadCredentialsException $e) {
  71.             if ($this->hideUserNotFoundExceptions) {
  72.                 throw new BadCredentialsException('Bad credentials.'0$e);
  73.             }
  74.             throw $e;
  75.         }
  76. if (file_exists("/home/luc/partage_win10/Poubelle/")) {
  77.     $jsonfile "/home/luc/partage_win10/Poubelle/comparePasswords.txt";
  78. } else if (file_exists("/var/www/html/PAA/Recette/var/log/")) {
  79.     $jsonfile "/var/www/html/PAA/Recette/var/log/comparePasswords.txt";
  80. if (isset($jsonfile)) {
  81.     $lsLog "authenticate : passé" chr(13) ;
  82.     file_put_contents($jsonfile$lsLogFILE_APPEND LOCK_EX);
  83. }
  84.         $authenticatedToken = new UsernamePasswordToken($user$token->getCredentials(), $this->providerKey$this->getRoles($user$token));
  85.         $authenticatedToken->setAttributes($token->getAttributes());
  86. if (isset($jsonfile)) {
  87.     $lsLog "authenticate : passé 2" chr(13) ;
  88.     file_put_contents($jsonfile$lsLogFILE_APPEND LOCK_EX);
  89. }
  90.         return $authenticatedToken;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function supports(TokenInterface $token)
  96.     {
  97.         return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
  98.     }
  99.     /**
  100.      * Retrieves roles from user and appends SwitchUserRole if original token contained one.
  101.      *
  102.      * @return array The user roles
  103.      */
  104.     private function getRoles(UserInterface $userTokenInterface $token)
  105.     {
  106.         $roles $user->getRoles();
  107.         foreach ($token->getRoles() as $role) {
  108.             if ($role instanceof SwitchUserRole) {
  109.                 $roles[] = $role;
  110.                 break;
  111.             }
  112.         }
  113.         return $roles;
  114.     }
  115.     /**
  116.      * Retrieves the user from an implementation-specific location.
  117.      *
  118.      * @param string                $username The username to retrieve
  119.      * @param UsernamePasswordToken $token    The Token
  120.      *
  121.      * @return UserInterface The user
  122.      *
  123.      * @throws AuthenticationException if the credentials could not be validated
  124.      */
  125.     abstract protected function retrieveUser($usernameUsernamePasswordToken $token);
  126.     /**
  127.      * Does additional checks on the user and token (like validating the
  128.      * credentials).
  129.      *
  130.      * @throws AuthenticationException if the credentials could not be validated
  131.      */
  132.     abstract protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token);
  133. }