vendor/symfony/security-http/Firewall/RememberMeListener.php line 35

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\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Event\RequestEvent;
  15. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  19. use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
  20. use Symfony\Component\Security\Http\SecurityEvents;
  21. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
  22. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  23. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  24. /**
  25.  * RememberMeListener implements authentication capabilities via a cookie.
  26.  *
  27.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  28.  *
  29.  * @final since Symfony 4.3
  30.  */
  31. class RememberMeListener extends AbstractListener implements ListenerInterface
  32. {
  33.     use LegacyListenerTrait;
  34.     private $tokenStorage;
  35.     private $rememberMeServices;
  36.     private $authenticationManager;
  37.     private $logger;
  38.     private $dispatcher;
  39.     private $catchExceptions true;
  40.     private $sessionStrategy;
  41.     public function __construct(TokenStorageInterface $tokenStorageRememberMeServicesInterface $rememberMeServicesAuthenticationManagerInterface $authenticationManagerLoggerInterface $logger nullEventDispatcherInterface $dispatcher nullbool $catchExceptions trueSessionAuthenticationStrategyInterface $sessionStrategy null)
  42.     {
  43.         $this->tokenStorage $tokenStorage;
  44.         $this->rememberMeServices $rememberMeServices;
  45.         $this->authenticationManager $authenticationManager;
  46.         $this->logger $logger;
  47.         if (null !== $dispatcher && class_exists(LegacyEventDispatcherProxy::class)) {
  48.             $this->dispatcher LegacyEventDispatcherProxy::decorate($dispatcher);
  49.         } else {
  50.             $this->dispatcher $dispatcher;
  51.         }
  52.         $this->catchExceptions $catchExceptions;
  53.         $this->sessionStrategy null === $sessionStrategy ? new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE) : $sessionStrategy;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function supports(Request $request): ?bool
  59.     {
  60.         return null// always run authenticate() lazily with lazy firewalls
  61.     }
  62.     /**
  63.      * Handles remember-me cookie based authentication.
  64.      */
  65.     public function authenticate(RequestEvent $event)
  66.     {
  67.         if (null !== $this->tokenStorage->getToken()) {
  68.             return;
  69.         }
  70.         $request $event->getRequest();
  71.         try {
  72.             if (null === $token $this->rememberMeServices->autoLogin($request)) {
  73.                 return;
  74.             }
  75.         } catch (AuthenticationException $e) {
  76.             if (null !== $this->logger) {
  77.                 $this->logger->warning(
  78.                     'The token storage was not populated with remember-me token as the'
  79.                    .' RememberMeServices was not able to create a token from the remember'
  80.                    .' me information.', ['exception' => $e]
  81.                 );
  82.             }
  83.             $this->rememberMeServices->loginFail($request);
  84.             if (!$this->catchExceptions) {
  85.                 throw $e;
  86.             }
  87.             return;
  88.         }
  89.         try {
  90.             $token $this->authenticationManager->authenticate($token);
  91.             if ($request->hasSession() && $request->getSession()->isStarted()) {
  92.                 $this->sessionStrategy->onAuthentication($request$token);
  93.             }
  94.             $this->tokenStorage->setToken($token);
  95.             if (null !== $this->dispatcher) {
  96.                 $loginEvent = new InteractiveLoginEvent($request$token);
  97.                 $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  98.             }
  99.             if (null !== $this->logger) {
  100.                 $this->logger->debug('Populated the token storage with a remember-me token.');
  101.             }
  102.         } catch (AuthenticationException $e) {
  103.             if (null !== $this->logger) {
  104.                 $this->logger->warning(
  105.                     'The token storage was not populated with remember-me token as the'
  106.                    .' AuthenticationManager rejected the AuthenticationToken returned'
  107.                    .' by the RememberMeServices.', ['exception' => $e]
  108.                 );
  109.             }
  110.             $this->rememberMeServices->loginFail($request$e);
  111.             if (!$this->catchExceptions) {
  112.                 throw $e;
  113.             }
  114.         }
  115.     }
  116. }