GuardAuthenticationProvider.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Guard\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationExpiredException;
  16. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  17. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  18. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Security\Core\User\UserProviderInterface;
  21. use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
  22. use Symfony\Component\Security\Guard\Token\GuardTokenInterface;
  23. use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
  24. /**
  25. * Responsible for accepting the PreAuthenticationGuardToken and calling
  26. * the correct authenticator to retrieve the authenticated token.
  27. *
  28. * @author Ryan Weaver <ryan@knpuniversity.com>
  29. */
  30. class GuardAuthenticationProvider implements AuthenticationProviderInterface
  31. {
  32. /**
  33. * @var GuardAuthenticatorInterface[]
  34. */
  35. private $guardAuthenticators;
  36. private $userProvider;
  37. private $providerKey;
  38. private $userChecker;
  39. /**
  40. * @param GuardAuthenticatorInterface[] $guardAuthenticators The authenticators, with keys that match what's passed to GuardAuthenticationListener
  41. * @param UserProviderInterface $userProvider The user provider
  42. * @param string $providerKey The provider (i.e. firewall) key
  43. * @param UserCheckerInterface $userChecker
  44. */
  45. public function __construct(array $guardAuthenticators, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker)
  46. {
  47. $this->guardAuthenticators = $guardAuthenticators;
  48. $this->userProvider = $userProvider;
  49. $this->providerKey = $providerKey;
  50. $this->userChecker = $userChecker;
  51. }
  52. /**
  53. * Finds the correct authenticator for the token and calls it.
  54. *
  55. * @param GuardTokenInterface $token
  56. *
  57. * @return TokenInterface
  58. */
  59. public function authenticate(TokenInterface $token)
  60. {
  61. if (!$token instanceof GuardTokenInterface) {
  62. throw new \InvalidArgumentException('GuardAuthenticationProvider only supports GuardTokenInterface.');
  63. }
  64. if (!$token instanceof PreAuthenticationGuardToken) {
  65. /*
  66. * The listener *only* passes PreAuthenticationGuardToken instances.
  67. * This means that an authenticated token (e.g. PostAuthenticationGuardToken)
  68. * is being passed here, which happens if that token becomes
  69. * "not authenticated" (e.g. happens if the user changes between
  70. * requests). In this case, the user should be logged out, so
  71. * we will return an AnonymousToken to accomplish that.
  72. */
  73. // this should never happen - but technically, the token is
  74. // authenticated... so it could just be returned
  75. if ($token->isAuthenticated()) {
  76. return $token;
  77. }
  78. // this AccountStatusException causes the user to be logged out
  79. throw new AuthenticationExpiredException();
  80. }
  81. $guardAuthenticator = $this->findOriginatingAuthenticator($token);
  82. if (null === $guardAuthenticator) {
  83. throw new AuthenticationException(sprintf('Token with provider key "%s" did not originate from any of the guard authenticators of provider "%s".', $token->getGuardProviderKey(), $this->providerKey));
  84. }
  85. return $this->authenticateViaGuard($guardAuthenticator, $token);
  86. }
  87. private function authenticateViaGuard(GuardAuthenticatorInterface $guardAuthenticator, PreAuthenticationGuardToken $token)
  88. {
  89. // get the user from the GuardAuthenticator
  90. $user = $guardAuthenticator->getUser($token->getCredentials(), $this->userProvider);
  91. if (null === $user) {
  92. throw new UsernameNotFoundException(sprintf('Null returned from %s::getUser()', \get_class($guardAuthenticator)));
  93. }
  94. if (!$user instanceof UserInterface) {
  95. throw new \UnexpectedValueException(sprintf('The %s::getUser() method must return a UserInterface. You returned %s.', \get_class($guardAuthenticator), \is_object($user) ? \get_class($user) : \gettype($user)));
  96. }
  97. $this->userChecker->checkPreAuth($user);
  98. if (true !== $guardAuthenticator->checkCredentials($token->getCredentials(), $user)) {
  99. throw new BadCredentialsException(sprintf('Authentication failed because %s::checkCredentials() did not return true.', \get_class($guardAuthenticator)));
  100. }
  101. $this->userChecker->checkPostAuth($user);
  102. // turn the UserInterface into a TokenInterface
  103. $authenticatedToken = $guardAuthenticator->createAuthenticatedToken($user, $this->providerKey);
  104. if (!$authenticatedToken instanceof TokenInterface) {
  105. throw new \UnexpectedValueException(sprintf('The %s::createAuthenticatedToken() method must return a TokenInterface. You returned %s.', \get_class($guardAuthenticator), \is_object($authenticatedToken) ? \get_class($authenticatedToken) : \gettype($authenticatedToken)));
  106. }
  107. return $authenticatedToken;
  108. }
  109. private function findOriginatingAuthenticator(PreAuthenticationGuardToken $token)
  110. {
  111. // find the *one* GuardAuthenticator that this token originated from
  112. foreach ($this->guardAuthenticators as $key => $guardAuthenticator) {
  113. // get a key that's unique to *this* guard authenticator
  114. // this MUST be the same as GuardAuthenticationListener
  115. $uniqueGuardKey = $this->providerKey.'_'.$key;
  116. if ($uniqueGuardKey === $token->getGuardProviderKey()) {
  117. return $guardAuthenticator;
  118. }
  119. }
  120. // no matching authenticator found - but there will be multiple GuardAuthenticationProvider
  121. // instances that will be checked if you have multiple firewalls.
  122. return null;
  123. }
  124. public function supports(TokenInterface $token)
  125. {
  126. if ($token instanceof PreAuthenticationGuardToken) {
  127. return null !== $this->findOriginatingAuthenticator($token);
  128. }
  129. return $token instanceof GuardTokenInterface;
  130. }
  131. }