vendor/scheb/two-factor-bundle/Security/Authentication/Provider/AuthenticationProviderDecorator.php line 56

Open in your IDE?
  1. <?php
  2. namespace Scheb\TwoFactorBundle\Security\Authentication\Provider;
  3. use Scheb\TwoFactorBundle\DependencyInjection\Factory\Security\TwoFactorFactory;
  4. use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
  5. use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextFactoryInterface;
  6. use Scheb\TwoFactorBundle\Security\TwoFactor\Handler\AuthenticationHandlerInterface;
  7. use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. class AuthenticationProviderDecorator implements AuthenticationProviderInterface
  13. {
  14.     /**
  15.      * @var AuthenticationProviderInterface
  16.      */
  17.     private $decoratedAuthenticationProvider;
  18.     /**
  19.      * @var AuthenticationHandlerInterface
  20.      */
  21.     private $twoFactorAuthenticationHandler;
  22.     /**
  23.      * @var AuthenticationContextFactoryInterface
  24.      */
  25.     private $authenticationContextFactory;
  26.     /**
  27.      * @var FirewallMap
  28.      */
  29.     private $firewallMap;
  30.     /**
  31.      * @var RequestStack
  32.      */
  33.     private $requestStack;
  34.     public function __construct(
  35.         AuthenticationProviderInterface $decoratedAuthenticationProvider,
  36.         AuthenticationHandlerInterface $twoFactorAuthenticationHandler,
  37.         AuthenticationContextFactoryInterface $authenticationContextFactory,
  38.         FirewallMap $firewallMap,
  39.         RequestStack $requestStack
  40.     ) {
  41.         $this->decoratedAuthenticationProvider $decoratedAuthenticationProvider;
  42.         $this->twoFactorAuthenticationHandler $twoFactorAuthenticationHandler;
  43.         $this->authenticationContextFactory $authenticationContextFactory;
  44.         $this->firewallMap $firewallMap;
  45.         $this->requestStack $requestStack;
  46.     }
  47.     public function supports(TokenInterface $token)
  48.     {
  49.         return $this->decoratedAuthenticationProvider->supports($token);
  50.     }
  51.     public function authenticate(TokenInterface $token)
  52.     {
  53.         $wasAlreadyAuthenticated $token->isAuthenticated();
  54.         $token $this->decoratedAuthenticationProvider->authenticate($token);
  55.         // Only trigger two-factor authentication when the provider was called with an unauthenticated token. When we
  56.         // get an authenticated token passed, we're not doing a login, but the system refreshes the token. Then we don't
  57.         // want to start two-factor authentication or we're ending in an endless loop.
  58.         if ($wasAlreadyAuthenticated) {
  59.             return $token;
  60.         }
  61.         // AnonymousToken and TwoFactorToken can be ignored
  62.         // in case of Guard, it can return null due to having multiple guard authenticators
  63.         if ($token instanceof AnonymousToken || $token instanceof TwoFactorTokenInterface || null === $token) {
  64.             return $token;
  65.         }
  66.         $request $this->requestStack->getMasterRequest();
  67.         $firewallConfig $this->firewallMap->getFirewallConfig($request);
  68.         if (!in_array(TwoFactorFactory::AUTHENTICATION_PROVIDER_KEY$firewallConfig->getListeners())) {
  69.             return $token// This firewall doesn't support two-factor authentication
  70.         }
  71.         $context $this->authenticationContextFactory->create($request$token$firewallConfig->getName());
  72.         return $this->twoFactorAuthenticationHandler->beginTwoFactorAuthentication($context);
  73.     }
  74.     public function __call($method$arguments)
  75.     {
  76.         return ($this->decoratedAuthenticationProvider)->{$method}(...$arguments);
  77.     }
  78. }