vendor/symfony/security-http/Firewall/LogoutListener.php line 37

  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 Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Exception\LogoutException;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Http\Event\LogoutEvent;
  19. use Symfony\Component\Security\Http\HttpUtils;
  20. use Symfony\Component\Security\Http\ParameterBagUtils;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * LogoutListener logout users.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  *
  27.  * @final
  28.  */
  29. class LogoutListener extends AbstractListener
  30. {
  31.     private TokenStorageInterface $tokenStorage;
  32.     private array $options;
  33.     private HttpUtils $httpUtils;
  34.     private ?CsrfTokenManagerInterface $csrfTokenManager;
  35.     private EventDispatcherInterface $eventDispatcher;
  36.     /**
  37.      * @param array $options An array of options to process a logout attempt
  38.      */
  39.     public function __construct(TokenStorageInterface $tokenStorageHttpUtils $httpUtilsEventDispatcherInterface $eventDispatcher, array $options = [], ?CsrfTokenManagerInterface $csrfTokenManager null)
  40.     {
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->httpUtils $httpUtils;
  43.         $this->options array_merge([
  44.             'csrf_parameter' => '_csrf_token',
  45.             'csrf_token_id' => 'logout',
  46.             'logout_path' => '/logout',
  47.         ], $options);
  48.         $this->csrfTokenManager $csrfTokenManager;
  49.         $this->eventDispatcher $eventDispatcher;
  50.     }
  51.     public function supports(Request $request): ?bool
  52.     {
  53.         return $this->requiresLogout($request);
  54.     }
  55.     /**
  56.      * Performs the logout if requested.
  57.      *
  58.      * If a CsrfTokenManagerInterface instance is available, it will be used to
  59.      * validate the request.
  60.      *
  61.      * @throws LogoutException   if the CSRF token is invalid
  62.      * @throws \RuntimeException if the LogoutEvent listener does not set a response
  63.      */
  64.     public function authenticate(RequestEvent $event): void
  65.     {
  66.         $request $event->getRequest();
  67.         if (null !== $this->csrfTokenManager) {
  68.             $csrfToken ParameterBagUtils::getRequestParameterValue($request$this->options['csrf_parameter']);
  69.             if (!\is_string($csrfToken) || false === $this->csrfTokenManager->isTokenValid(new CsrfToken($this->options['csrf_token_id'], $csrfToken))) {
  70.                 throw new LogoutException('Invalid CSRF token.');
  71.             }
  72.         }
  73.         $logoutEvent = new LogoutEvent($request$this->tokenStorage->getToken());
  74.         $this->eventDispatcher->dispatch($logoutEvent);
  75.         if (!$response $logoutEvent->getResponse()) {
  76.             throw new \RuntimeException('No logout listener set the Response, make sure at least the DefaultLogoutListener is registered.');
  77.         }
  78.         $this->tokenStorage->setToken(null);
  79.         $event->setResponse($response);
  80.     }
  81.     /**
  82.      * Whether this request is asking for logout.
  83.      *
  84.      * The default implementation only processed requests to a specific path,
  85.      * but a subclass could change this to logout requests where
  86.      * certain parameters is present.
  87.      */
  88.     protected function requiresLogout(Request $request): bool
  89.     {
  90.         return isset($this->options['logout_path']) && $this->httpUtils->checkRequestPath($request$this->options['logout_path']);
  91.     }
  92.     public static function getPriority(): int
  93.     {
  94.         return -127;
  95.     }
  96. }