vendor/scheb/2fa-bundle/Security/Http/Firewall/TwoFactorAccessListener.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Scheb\TwoFactorBundle\Security\Http\Firewall;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Scheb\TwoFactorBundle\Security\Authorization\TwoFactorAccessDecider;
  6. use Scheb\TwoFactorBundle\Security\TwoFactor\TwoFactorFirewallConfig;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  11. use Symfony\Component\Security\Http\Firewall\AbstractListener;
  12. use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface;
  13. /**
  14.  * Handles access control in the "2fa in progress" phase.
  15.  *
  16.  * @final
  17.  */
  18. class TwoFactorAccessListener extends AbstractListener implements FirewallListenerInterface
  19. {
  20.     public function __construct(
  21.         private TwoFactorFirewallConfig $twoFactorFirewallConfig,
  22.         private TokenStorageInterface $tokenStorage,
  23.         private TwoFactorAccessDecider $twoFactorAccessDecider,
  24.     ) {
  25.     }
  26.     public function supports(Request $request): ?bool
  27.     {
  28.         // When the path is explicitly configured for anonymous access, no need to check access (important for lazy
  29.         // firewalls, to prevent the response cache control to be flagged "private")
  30.         return !$this->twoFactorAccessDecider->isPubliclyAccessible($request);
  31.     }
  32.     public function authenticate(RequestEvent $event): void
  33.     {
  34.         // When the firewall is lazy, the token is not initialized in the "supports" stage, so this check does only work
  35.         // within the "authenticate" stage.
  36.         $token $this->tokenStorage->getToken();
  37.         if (!($token instanceof TwoFactorTokenInterface)) {
  38.             // No need to check for firewall name here, the listener is bound to the firewall context
  39.             return;
  40.         }
  41.         $request $event->getRequest();
  42.         if ($this->twoFactorFirewallConfig->isCheckPathRequest($request)) {
  43.             return;
  44.         }
  45.         if ($this->twoFactorFirewallConfig->isAuthFormRequest($request)) {
  46.             return;
  47.         }
  48.         if (!$this->twoFactorAccessDecider->isAccessible($request$token)) {
  49.             $exception = new AccessDeniedException('User is in a two-factor authentication process.');
  50.             $exception->setSubject($request);
  51.             throw $exception;
  52.         }
  53.     }
  54.     public static function getPriority(): int
  55.     {
  56.         // When the class is injected via FirewallListenerFactoryInterface
  57.         // Inject before Symfony's AccessListener (-255) and after the LogoutListener (-127)
  58.         return -191;
  59.     }
  60. }