vendor/scheb/2fa-bundle/Security/TwoFactor/Event/TwoFactorFormListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\TwoFactor\Event;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Scheb\TwoFactorBundle\Security\TwoFactor\TwoFactorFirewallConfig;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  11. /**
  12.  * @final
  13.  */
  14. class TwoFactorFormListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var TwoFactorFirewallConfig
  18.      */
  19.     private $twoFactorFirewallConfig;
  20.     /**
  21.      * @var TokenStorageInterface
  22.      */
  23.     private $tokenStorage;
  24.     /**
  25.      * @var EventDispatcherInterface
  26.      */
  27.     private $eventDispatcher;
  28.     public function __construct(
  29.         TwoFactorFirewallConfig $twoFactorFirewallConfig,
  30.         TokenStorageInterface $tokenStorage,
  31.         EventDispatcherInterface $eventDispatcher
  32.     ) {
  33.         $this->twoFactorFirewallConfig $twoFactorFirewallConfig;
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->eventDispatcher $eventDispatcher;
  36.     }
  37.     public function onKernelRequest(RequestEvent $requestEvent): void
  38.     {
  39.         $request $requestEvent->getRequest();
  40.         if (!$request->hasSession()) {
  41.             return;
  42.         }
  43.         $token $this->tokenStorage->getToken();
  44.         if (!($token instanceof TwoFactorTokenInterface)) {
  45.             return;
  46.         }
  47.         if ($this->twoFactorFirewallConfig->isAuthFormRequest($request)) {
  48.             $event = new TwoFactorAuthenticationEvent($request$token);
  49.             $this->eventDispatcher->dispatch($eventTwoFactorAuthenticationEvents::FORM);
  50.             return;
  51.         }
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             KernelEvents::REQUEST => 'onKernelRequest',
  57.         ];
  58.     }
  59. }