vendor/symfony/security-guard/Authenticator/GuardBridgeAuthenticator.php line 35

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