AuthenticationProviderManager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\AuthenticationEvents;
  14. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  15. use Symfony\Component\Security\Core\Event\AuthenticationSuccessEvent;
  16. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Exception\ProviderNotFoundException;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. // Help opcache.preload discover always-needed symbols
  21. class_exists(AuthenticationEvents::class);
  22. class_exists(AuthenticationFailureEvent::class);
  23. class_exists(AuthenticationSuccessEvent::class);
  24. /**
  25. * AuthenticationProviderManager uses a list of AuthenticationProviderInterface
  26. * instances to authenticate a Token.
  27. *
  28. * @author Fabien Potencier <fabien@symfony.com>
  29. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  30. */
  31. class AuthenticationProviderManager implements AuthenticationManagerInterface
  32. {
  33. private $providers;
  34. private $eraseCredentials;
  35. private $eventDispatcher;
  36. /**
  37. * @param iterable|AuthenticationProviderInterface[] $providers An iterable with AuthenticationProviderInterface instances as values
  38. * @param bool $eraseCredentials Whether to erase credentials after authentication or not
  39. *
  40. * @throws \InvalidArgumentException
  41. */
  42. public function __construct(iterable $providers, bool $eraseCredentials = true)
  43. {
  44. if (!$providers) {
  45. throw new \InvalidArgumentException('You must at least add one authentication provider.');
  46. }
  47. $this->providers = $providers;
  48. $this->eraseCredentials = $eraseCredentials;
  49. }
  50. public function setEventDispatcher(EventDispatcherInterface $dispatcher)
  51. {
  52. $this->eventDispatcher = $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_debug_type($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. }