GuardBridgeAuthenticator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Authenticator;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  16. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Core\User\UserProviderInterface;
  19. use Symfony\Component\Security\Guard\AuthenticatorInterface as GuardAuthenticatorInterface;
  20. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  21. use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface;
  22. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  23. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  27. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  28. use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface;
  29. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  30. /**
  31. * This authenticator is used to bridge Guard authenticators with
  32. * the Symfony Authenticator system.
  33. *
  34. * @author Wouter de Jong <wouter@wouterj.nl>
  35. *
  36. * @internal
  37. */
  38. class GuardBridgeAuthenticator implements InteractiveAuthenticatorInterface, AuthenticationEntryPointInterface
  39. {
  40. private $guard;
  41. private $userProvider;
  42. public function __construct(GuardAuthenticatorInterface $guard, UserProviderInterface $userProvider)
  43. {
  44. $this->guard = $guard;
  45. $this->userProvider = $userProvider;
  46. }
  47. public function start(Request $request, AuthenticationException $authException = null)
  48. {
  49. return $this->guard->start($request, $authException);
  50. }
  51. public function supports(Request $request): ?bool
  52. {
  53. return $this->guard->supports($request);
  54. }
  55. public function authenticate(Request $request): PassportInterface
  56. {
  57. $credentials = $this->guard->getCredentials($request);
  58. if (null === $credentials) {
  59. throw new \UnexpectedValueException(sprintf('The return value of "%1$s::getCredentials()" must not be null. Return false from "%1$s::supports()" instead.', get_debug_type($this->guard)));
  60. }
  61. // get the user from the GuardAuthenticator
  62. if (class_exists(UserBadge::class)) {
  63. $user = new UserBadge('guard_authenticator_'.md5(serialize($credentials)), function () use ($credentials) { return $this->getUser($credentials); });
  64. } else {
  65. // BC with symfony/security-http:5.1
  66. $user = $this->getUser($credentials);
  67. }
  68. $passport = new Passport($user, new CustomCredentials([$this->guard, 'checkCredentials'], $credentials));
  69. if ($this->userProvider instanceof PasswordUpgraderInterface && $this->guard instanceof PasswordAuthenticatedInterface && (null !== $password = $this->guard->getPassword($credentials))) {
  70. $passport->addBadge(new PasswordUpgradeBadge($password, $this->userProvider));
  71. }
  72. if ($this->guard->supportsRememberMe()) {
  73. $passport->addBadge(new RememberMeBadge());
  74. }
  75. return $passport;
  76. }
  77. private function getUser($credentials): UserInterface
  78. {
  79. $user = $this->guard->getUser($credentials, $this->userProvider);
  80. if (null === $user) {
  81. throw new UsernameNotFoundException(sprintf('Null returned from "%s::getUser()".', get_debug_type($this->guard)));
  82. }
  83. if (!$user instanceof UserInterface) {
  84. throw new \UnexpectedValueException(sprintf('The "%s::getUser()" method must return a UserInterface. You returned "%s".', get_debug_type($this->guard), get_debug_type($user)));
  85. }
  86. return $user;
  87. }
  88. public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
  89. {
  90. if (!$passport instanceof UserPassportInterface) {
  91. throw new \LogicException(sprintf('"%s" does not support non-user passports.', __CLASS__));
  92. }
  93. return $this->guard->createAuthenticatedToken($passport->getUser(), $firewallName);
  94. }
  95. public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
  96. {
  97. return $this->guard->onAuthenticationSuccess($request, $token, $firewallName);
  98. }
  99. public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
  100. {
  101. return $this->guard->onAuthenticationFailure($request, $exception);
  102. }
  103. public function isInteractive(): bool
  104. {
  105. // the GuardAuthenticationHandler always dispatches the InteractiveLoginEvent
  106. return true;
  107. }
  108. }