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

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\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\AuthenticationEvents;
  15. use Symfony\Component\Security\Core\Event\AuthenticationEvent;
  16. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  17. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  19. use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
  20. /**
  21.  * AuthenticationProviderManager uses a list of AuthenticationProviderInterface
  22.  * instances to authenticate a Token.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26.  */
  27. class AuthenticationProviderManager implements AuthenticationManagerInterface
  28. {
  29.     private $providers;
  30.     private $eraseCredentials;
  31.     private $eventDispatcher;
  32.     /**
  33.      * @param iterable|AuthenticationProviderInterface[] $providers        An iterable with AuthenticationProviderInterface instances as values
  34.      * @param bool                                       $eraseCredentials Whether to erase credentials after authentication or not
  35.      *
  36.      * @throws \InvalidArgumentException
  37.      */
  38.     public function __construct(iterable $providersbool $eraseCredentials true)
  39.     {
  40.         if (!$providers) {
  41.             throw new \InvalidArgumentException('You must at least add one authentication provider.');
  42.         }
  43.         $this->providers $providers;
  44.         $this->eraseCredentials $eraseCredentials;
  45.     }
  46.     public function setEventDispatcher(EventDispatcherInterface $dispatcher)
  47.     {
  48.         $this->eventDispatcher $dispatcher;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function authenticate(TokenInterface $token)
  54.     {
  55.         $lastException null;
  56.         $result null;
  57.         foreach ($this->providers as $provider) {
  58.             if (!$provider instanceof AuthenticationProviderInterface) {
  59.                 throw new \InvalidArgumentException(sprintf('Provider "%s" must implement the AuthenticationProviderInterface.', \get_class($provider)));
  60.             }
  61.             if (!$provider->supports($token)) {
  62.                 continue;
  63.             }
  64.             try {
  65.                 $result $provider->authenticate($token);
  66.                 if (null !== $result) {
  67.                     break;
  68.                 }
  69.             } catch (AccountStatusException $e) {
  70.                 $lastException $e;
  71.                 break;
  72.             } catch (AuthenticationException $e) {
  73.                 $lastException $e;
  74.             }
  75.         }
  76.         if (null !== $result) {
  77.             if (true === $this->eraseCredentials) {
  78.                 $result->eraseCredentials();
  79.             }
  80.             if (null !== $this->eventDispatcher) {
  81.                 $this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($result));
  82.             }
  83.             return $result;
  84.         }
  85.         if (null === $lastException) {
  86.             $lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', \get_class($token)));
  87.         }
  88.         if (null !== $this->eventDispatcher) {
  89.             $this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_FAILURE, new AuthenticationFailureEvent($token$lastException));
  90.         }
  91.         $lastException->setToken($token);
  92.         throw $lastException;
  93.     }
  94. }