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

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.     public function __construct(
  17.         private TwoFactorFirewallConfig $twoFactorFirewallConfig,
  18.         private TokenStorageInterface $tokenStorage,
  19.         private EventDispatcherInterface $eventDispatcher,
  20.     ) {
  21.     }
  22.     public function onKernelRequest(RequestEvent $requestEvent): void
  23.     {
  24.         $request $requestEvent->getRequest();
  25.         if (!$request->hasSession()) {
  26.             return;
  27.         }
  28.         $token $this->tokenStorage->getToken();
  29.         if (!($token instanceof TwoFactorTokenInterface)) {
  30.             return;
  31.         }
  32.         if ($this->twoFactorFirewallConfig->isAuthFormRequest($request)) {
  33.             $event = new TwoFactorAuthenticationEvent($request$token);
  34.             $this->eventDispatcher->dispatch($eventTwoFactorAuthenticationEvents::FORM);
  35.             return;
  36.         }
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [KernelEvents::REQUEST => 'onKernelRequest'];
  44.     }
  45. }