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 <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\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 <schmittjoh@gmail.com>
  36.  * @author Wouter de Jong <wouter@wouterj.nl>
  37.  *
  38.  * @final
  39.  */
  40. class RememberMeAuthenticator implements InteractiveAuthenticatorInterface
  41. {
  42.     private RememberMeHandlerInterface $rememberMeHandler;
  43.     private string $secret;
  44.     private TokenStorageInterface $tokenStorage;
  45.     private string $cookieName;
  46.     private ?LoggerInterface $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.         $this->logger?->debug('Remember-me cookie detected.');
  68.         // the `null` return value indicates that this authenticator supports lazy firewalls
  69.         return null;
  70.     }
  71.     public function authenticate(Request $request): Passport
  72.     {
  73.         $rawCookie $request->cookies->get($this->cookieName);
  74.         if (!$rawCookie) {
  75.             throw new \LogicException('No remember-me cookie is found.');
  76.         }
  77.         $rememberMeCookie RememberMeDetails::fromRawCookie($rawCookie);
  78.         return new SelfValidatingPassport(new UserBadge($rememberMeCookie->getUserIdentifier(), function () use ($rememberMeCookie) {
  79.             return $this->rememberMeHandler->consumeRememberMeCookie($rememberMeCookie);
  80.         }));
  81.     }
  82.     public function createToken(Passport $passportstring $firewallName): TokenInterface
  83.     {
  84.         return new RememberMeToken($passport->getUser(), $firewallName$this->secret);
  85.     }
  86.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  87.     {
  88.         return null// let the original request continue
  89.     }
  90.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  91.     {
  92.         if (null !== $this->logger) {
  93.             if ($exception instanceof UserNotFoundException) {
  94.                 $this->logger->info('User for remember-me cookie not found.', ['exception' => $exception]);
  95.             } elseif ($exception instanceof UnsupportedUserException) {
  96.                 $this->logger->warning('User class for remember-me cookie not supported.', ['exception' => $exception]);
  97.             } elseif (!$exception instanceof CookieTheftException) {
  98.                 $this->logger->debug('Remember me authentication failed.', ['exception' => $exception]);
  99.             }
  100.         }
  101.         return null;
  102.     }
  103.     public function isInteractive(): bool
  104.     {
  105.         return true;
  106.     }
  107. }