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

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