vendor/scheb/two-factor-bundle/Security/TwoFactor/EventListener/RequestListener.php line 66

Open in your IDE?
  1. <?php
  2. namespace Scheb\TwoFactorBundle\Security\TwoFactor\EventListener;
  3. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationHandlerInterface;
  6. use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextFactoryInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. class RequestListener
  9. {
  10.     /**
  11.      * @var AuthenticationContextFactoryInterface
  12.      */
  13.     private $authenticationContextFactory;
  14.     /**
  15.      * @var AuthenticationHandlerInterface
  16.      */
  17.     private $authHandler;
  18.     /**
  19.      * @var TokenStorageInterface
  20.      */
  21.     private $tokenStorage;
  22.     /**
  23.      * @var array
  24.      */
  25.     private $supportedTokens;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $excludePattern;
  30.     /**
  31.      * Construct a listener for login events.
  32.      *
  33.      * @param AuthenticationContextFactoryInterface $authenticationContextFactory
  34.      * @param AuthenticationHandlerInterface        $authHandler
  35.      * @param TokenStorageInterface                 $tokenStorage
  36.      * @param array                                 $supportedTokens
  37.      * @param string                                $excludePattern
  38.      */
  39.     public function __construct(
  40.         AuthenticationContextFactoryInterface $authenticationContextFactory,
  41.         AuthenticationHandlerInterface $authHandler,
  42.         TokenStorageInterface $tokenStorage,
  43.         array $supportedTokens,
  44.         $excludePattern
  45.     ) {
  46.         $this->authenticationContextFactory $authenticationContextFactory;
  47.         $this->authHandler $authHandler;
  48.         $this->tokenStorage $tokenStorage;
  49.         $this->supportedTokens $supportedTokens;
  50.         $this->excludePattern $excludePattern;
  51.     }
  52.     /**
  53.      * Listen for request events.
  54.      *
  55.      * @param GetResponseEvent $event
  56.      */
  57.     public function onCoreRequest(GetResponseEvent $event)
  58.     {
  59.         $request $event->getRequest();
  60.         // Exclude path
  61.         if ($this->excludePattern !== null && preg_match('#'.$this->excludePattern.'#'$request->getPathInfo())) {
  62.             return;
  63.         }
  64.         // Check if security token is supported
  65.         $token $this->tokenStorage->getToken();
  66.         if (!$this->isTokenSupported($token)) {
  67.             return;
  68.         }
  69.         // Forward to two-factor provider
  70.         // Providers can create a response object
  71.         $context $this->authenticationContextFactory->create($request$token);
  72.         $response $this->authHandler->requestAuthenticationCode($context);
  73.         // Set the response (if there is one)
  74.         if ($response instanceof Response) {
  75.             $event->setResponse($response);
  76.         }
  77.     }
  78.     /**
  79.      * Check if the token class is supported.
  80.      *
  81.      * @param mixed $token
  82.      *
  83.      * @return bool
  84.      */
  85.     private function isTokenSupported($token)
  86.     {
  87.         if (null === $token) {
  88.             return false;
  89.         }
  90.         $class get_class($token);
  91.         return in_array($class$this->supportedTokens);
  92.     }
  93. }