vendor/symfony/security-core/Authentication/AuthenticationProviderManager.php line 85

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Authentication;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  13. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\AuthenticationEvents;
  16. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  17. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  18. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  19. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  20. use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
  21. // Help opcache.preload discover always-needed symbols
  22. class_exists(AuthenticationEvents::class);
  23. class_exists(AuthenticationFailureEvent::class);
  24. class_exists(AuthenticationSuccessEvent::class);
  25. /**
  26.  * AuthenticationProviderManager uses a list of AuthenticationProviderInterface
  27.  * instances to authenticate a Token.
  28.  *
  29.  * @author Fabien Potencier <fabien@symfony.com>
  30.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31.  */
  32. class AuthenticationProviderManager implements AuthenticationManagerInterface
  33. {
  34.     private $providers;
  35.     private $eraseCredentials;
  36.     private $eventDispatcher;
  37.     /**
  38.      * @param iterable|AuthenticationProviderInterface[] $providers        An iterable with AuthenticationProviderInterface instances as values
  39.      * @param bool                                       $eraseCredentials Whether to erase credentials after authentication or not
  40.      *
  41.      * @throws \InvalidArgumentException
  42.      */
  43.     public function __construct(iterable $providersbool $eraseCredentials true)
  44.     {
  45.         if (!$providers) {
  46.             throw new \InvalidArgumentException('You must at least add one authentication provider.');
  47.         }
  48.         $this->providers $providers;
  49.         $this->eraseCredentials $eraseCredentials;
  50.     }
  51.     /**
  52.      * @final since Symfony 4.3, the type-hint will be updated to the interface from symfony/contracts in 5.0
  53.      */
  54.     public function setEventDispatcher(EventDispatcherInterface $dispatcher)
  55.     {
  56.         $this->eventDispatcher LegacyEventDispatcherProxy::decorate($dispatcher);
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function authenticate(TokenInterface $token)
  62.     {
  63.         $lastException null;
  64.         $result null;
  65.         foreach ($this->providers as $provider) {
  66.             if (!$provider instanceof AuthenticationProviderInterface) {
  67.                 throw new \InvalidArgumentException(sprintf('Provider "%s" must implement the AuthenticationProviderInterface.', \get_class($provider)));
  68.             }
  69.             if (!$provider->supports($token)) {
  70.                 continue;
  71.             }
  72.             try {
  73.                 $result $provider->authenticate($token);
  74.                 if (null !== $result) {
  75.                     break;
  76.                 }
  77.             } catch (AccountStatusException $e) {
  78.                 $lastException $e;
  79.                 break;
  80.             } catch (AuthenticationException $e) {
  81.                 $lastException $e;
  82.             }
  83.         }
  84.         if (null !== $result) {
  85.             if (true === $this->eraseCredentials) {
  86.                 $result->eraseCredentials();
  87.             }
  88.             if (null !== $this->eventDispatcher) {
  89.                 $this->eventDispatcher->dispatch(new AuthenticationSuccessEvent($result), AuthenticationEvents::AUTHENTICATION_SUCCESS);
  90.             }
  91.             return $result;
  92.         }
  93.         if (null === $lastException) {
  94.             $lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', \get_class($token)));
  95.         }
  96.         if (null !== $this->eventDispatcher) {
  97.             $this->eventDispatcher->dispatch(new AuthenticationFailureEvent($token$lastException), AuthenticationEvents::AUTHENTICATION_FAILURE);
  98.         }
  99.         $lastException->setToken($token);
  100.         throw $lastException;
  101.     }
  102. }