vendor/symfony/security-http/Authenticator/RememberMeAuthenticator.php line 44

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\Http\Authenticator;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Exception\CookieTheftException;
  19. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  20. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  22. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  23. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  24. use Symfony\Component\Security\Http\RememberMe\RememberMeDetails;
  25. use Symfony\Component\Security\Http\RememberMe\RememberMeHandlerInterface;
  26. use Symfony\Component\Security\Http\RememberMe\ResponseListener;
  27. /**
  28.  * The RememberMe *Authenticator* performs remember me authentication.
  29.  *
  30.  * This authenticator is executed whenever a user's session
  31.  * expired and a remember-me cookie was found. This authenticator
  32.  * then "re-authenticates" the user using the information in the
  33.  * cookie.
  34.  *
  35.  * @author Johannes M. Schmitt <[email protected]>
  36.  * @author Wouter de Jong <[email protected]>
  37.  *
  38.  * @final
  39.  */
  40. class RememberMeAuthenticator implements InteractiveAuthenticatorInterface
  41. {
  42.     private $rememberMeHandler;
  43.     private string $secret;
  44.     private $tokenStorage;
  45.     private string $cookieName;
  46.     private $logger;
  47.     public function __construct(RememberMeHandlerInterface $rememberMeHandlerstring $secretTokenStorageInterface $tokenStoragestring $cookieNameLoggerInterface $logger null)
  48.     {
  49.         $this->rememberMeHandler $rememberMeHandler;
  50.         $this->secret $secret;
  51.         $this->tokenStorage $tokenStorage;
  52.         $this->cookieName $cookieName;
  53.         $this->logger $logger;
  54.     }
  55.     public function supports(Request $request): ?bool
  56.     {
  57.         // do not overwrite already stored tokens (i.e. from the session)
  58.         if (null !== $this->tokenStorage->getToken()) {
  59.             return false;
  60.         }
  61.         if (($cookie $request->attributes->get(ResponseListener::COOKIE_ATTR_NAME)) && null === $cookie->getValue()) {
  62.             return false;
  63.         }
  64.         if (!$request->cookies->has($this->cookieName)) {
  65.             return false;
  66.         }
  67.         if (null !== $this->logger) {
  68.             $this->logger->debug('Remember-me cookie detected.');
  69.         }
  70.         // the `null` return value indicates that this authenticator supports lazy firewalls
  71.         return null;
  72.     }
  73.     public function authenticate(Request $request): Passport
  74.     {
  75.         $rawCookie $request->cookies->get($this->cookieName);
  76.         if (!$rawCookie) {
  77.             throw new \LogicException('No remember-me cookie is found.');
  78.         }
  79.         $rememberMeCookie RememberMeDetails::fromRawCookie($rawCookie);
  80.         return new SelfValidatingPassport(new UserBadge($rememberMeCookie->getUserIdentifier(), function () use ($rememberMeCookie) {
  81.             return $this->rememberMeHandler->consumeRememberMeCookie($rememberMeCookie);
  82.         }));
  83.     }
  84.     public function createToken(Passport $passportstring $firewallName): TokenInterface
  85.     {
  86.         return new RememberMeToken($passport->getUser(), $firewallName$this->secret);
  87.     }
  88.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  89.     {
  90.         return null// let the original request continue
  91.     }
  92.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  93.     {
  94.         if (null !== $this->logger) {
  95.             if ($exception instanceof UserNotFoundException) {
  96.                 $this->logger->info('User for remember-me cookie not found.', ['exception' => $exception]);
  97.             } elseif ($exception instanceof UnsupportedUserException) {
  98.                 $this->logger->warning('User class for remember-me cookie not supported.', ['exception' => $exception]);
  99.             } elseif (!$exception instanceof CookieTheftException) {
  100.                 $this->logger->debug('Remember me authentication failed.', ['exception' => $exception]);
  101.             }
  102.         }
  103.         return null;
  104.     }
  105.     public function isInteractive(): bool
  106.     {
  107.         return true;
  108.     }
  109. }