vendor/symfony/security-http/EventListener/CsrfTokenClearingLogoutListener.php line 32

  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\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
  13. use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
  14. use Symfony\Component\Security\Http\Event\LogoutEvent;
  15. /**
  16.  * @author Christian Flothmann <christian.flothmann@sensiolabs.de>
  17.  *
  18.  * @final
  19.  */
  20. class CsrfTokenClearingLogoutListener implements EventSubscriberInterface
  21. {
  22.     private ClearableTokenStorageInterface $csrfTokenStorage;
  23.     public function __construct(ClearableTokenStorageInterface $csrfTokenStorage)
  24.     {
  25.         $this->csrfTokenStorage $csrfTokenStorage;
  26.     }
  27.     public function onLogout(LogoutEvent $event): void
  28.     {
  29.         if ($this->csrfTokenStorage instanceof SessionTokenStorage && !$event->getRequest()->hasPreviousSession()) {
  30.             return;
  31.         }
  32.         $this->csrfTokenStorage->clear();
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             LogoutEvent::class => 'onLogout',
  38.         ];
  39.     }
  40. }