vendor/symfony/symfony/src/Symfony/Component/Security/Guard/GuardAuthenticatorHandler.php line 78

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;
  11. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  19. use Symfony\Component\Security\Http\SecurityEvents;
  20. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  22. /**
  23.  * A utility class that does much of the *work* during the guard authentication process.
  24.  *
  25.  * By having the logic here instead of the listener, more of the process
  26.  * can be called directly (e.g. for manual authentication) or overridden.
  27.  *
  28.  * @author Ryan Weaver <ryan@knpuniversity.com>
  29.  *
  30.  * @final
  31.  */
  32. class GuardAuthenticatorHandler
  33. {
  34.     private $tokenStorage;
  35.     private $dispatcher;
  36.     private $sessionStrategy;
  37.     private $statelessProviderKeys;
  38.     /**
  39.      * @param array $statelessProviderKeys An array of provider/firewall keys that are "stateless" and so do not need the session migrated on success
  40.      */
  41.     public function __construct(TokenStorageInterface $tokenStorageEventDispatcherInterface $eventDispatcher null, array $statelessProviderKeys = [])
  42.     {
  43.         $this->tokenStorage $tokenStorage;
  44.         if (null !== $eventDispatcher && class_exists(LegacyEventDispatcherProxy::class)) {
  45.             $this->dispatcher LegacyEventDispatcherProxy::decorate($eventDispatcher);
  46.         } else {
  47.             $this->dispatcher $eventDispatcher;
  48.         }
  49.         $this->statelessProviderKeys $statelessProviderKeys;
  50.     }
  51.     /**
  52.      * Authenticates the given token in the system.
  53.      */
  54.     public function authenticateWithToken(TokenInterface $tokenRequest $requeststring $providerKey null)
  55.     {
  56.         $this->migrateSession($request$token$providerKey);
  57.         $this->tokenStorage->setToken($token);
  58.         if (null !== $this->dispatcher) {
  59.             $loginEvent = new InteractiveLoginEvent($request$token);
  60.             $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  61.         }
  62.     }
  63.     /**
  64.      * Returns the "on success" response for the given GuardAuthenticator.
  65.      */
  66.     public function handleAuthenticationSuccess(TokenInterface $tokenRequest $requestAuthenticatorInterface $guardAuthenticatorstring $providerKey): ?Response
  67.     {
  68.         $response $guardAuthenticator->onAuthenticationSuccess($request$token$providerKey);
  69.         // check that it's a Response or null
  70.         if ($response instanceof Response || null === $response) {
  71.             return $response;
  72.         }
  73.         throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationSuccess()" method must return null or a Response object. You returned "%s".', \get_class($guardAuthenticator), \is_object($response) ? \get_class($response) : \gettype($response)));
  74.     }
  75.     /**
  76.      * Convenience method for authenticating the user and returning the
  77.      * Response *if any* for success.
  78.      */
  79.     public function authenticateUserAndHandleSuccess(UserInterface $userRequest $requestAuthenticatorInterface $authenticatorstring $providerKey): ?Response
  80.     {
  81.         // create an authenticated token for the User
  82.         $token $authenticator->createAuthenticatedToken($user$providerKey);
  83.         // authenticate this in the system
  84.         $this->authenticateWithToken($token$request$providerKey);
  85.         // return the success metric
  86.         return $this->handleAuthenticationSuccess($token$request$authenticator$providerKey);
  87.     }
  88.     /**
  89.      * Handles an authentication failure and returns the Response for the
  90.      * GuardAuthenticator.
  91.      */
  92.     public function handleAuthenticationFailure(AuthenticationException $authenticationExceptionRequest $requestAuthenticatorInterface $guardAuthenticatorstring $providerKey): ?Response
  93.     {
  94.         $response $guardAuthenticator->onAuthenticationFailure($request$authenticationException);
  95.         if ($response instanceof Response || null === $response) {
  96.             // returning null is ok, it means they want the request to continue
  97.             return $response;
  98.         }
  99.         throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationFailure()" method must return null or a Response object. You returned "%s".', \get_class($guardAuthenticator), \is_object($response) ? \get_class($response) : \gettype($response)));
  100.     }
  101.     /**
  102.      * Call this method if your authentication token is stored to a session.
  103.      *
  104.      * @final
  105.      */
  106.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  107.     {
  108.         $this->sessionStrategy $sessionStrategy;
  109.     }
  110.     private function migrateSession(Request $requestTokenInterface $token, ?string $providerKey)
  111.     {
  112.         if (\in_array($providerKey$this->statelessProviderKeystrue) || !$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  113.             return;
  114.         }
  115.         $this->sessionStrategy->onAuthentication($request$token);
  116.     }
  117. }