vendor/scheb/two-factor-bundle/Security/TwoFactor/EventListener/InteractiveLoginListener.php line 56

Open in your IDE?
  1. <?php
  2. namespace Scheb\TwoFactorBundle\Security\TwoFactor\EventListener;
  3. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  4. use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationHandlerInterface;
  5. use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextFactoryInterface;
  6. class InteractiveLoginListener
  7. {
  8.     /**
  9.      * @var AuthenticationContextFactoryInterface
  10.      */
  11.     private $authenticationContextFactory;
  12.     /**
  13.      * @var AuthenticationHandlerInterface
  14.      */
  15.     private $authHandler;
  16.     /**
  17.      * @var array
  18.      */
  19.     private $supportedTokens;
  20.     /**
  21.      * @var array
  22.      */
  23.     private $ipWhitelist;
  24.     /**
  25.      * Construct a listener for login events.
  26.      *
  27.      * @param AuthenticationContextFactoryInterface $authenticationContextFactory
  28.      * @param AuthenticationHandlerInterface $authHandler
  29.      * @param array $supportedTokens
  30.      * @param array $ipWhitelist
  31.      */
  32.     public function __construct(
  33.         AuthenticationContextFactoryInterface $authenticationContextFactory,
  34.         AuthenticationHandlerInterface $authHandler,
  35.         array $supportedTokens,
  36.         array $ipWhitelist
  37.     ) {
  38.         $this->authenticationContextFactory $authenticationContextFactory;
  39.         $this->authHandler $authHandler;
  40.         $this->supportedTokens $supportedTokens;
  41.         $this->ipWhitelist $ipWhitelist;
  42.     }
  43.     /**
  44.      * Listen for successful login events.
  45.      *
  46.      * @param InteractiveLoginEvent $event
  47.      */
  48.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  49.     {
  50.         $request $event->getRequest();
  51.         // Skip two-factor authentication for whitelisted IPs
  52.         if (in_array($request->getClientIp(), $this->ipWhitelist)) {
  53.             return;
  54.         }
  55.         // Check if security token is supported
  56.         $token $event->getAuthenticationToken();
  57.         if (!$this->isTokenSupported($token)) {
  58.             return;
  59.         }
  60.         // Forward to two-factor providers
  61.         // They decide if they will do two-factor authentication
  62.         $context $this->authenticationContextFactory->create($request$token);
  63.         $this->authHandler->beginAuthentication($context);
  64.     }
  65.     /**
  66.      * Check if the token class is supported.
  67.      *
  68.      * @param mixed $token
  69.      *
  70.      * @return bool
  71.      */
  72.     private function isTokenSupported($token)
  73.     {
  74.         if (null === $token) {
  75.             return false;
  76.         }
  77.         $class get_class($token);
  78.         return in_array($class$this->supportedTokens);
  79.     }
  80. }