vendor/symfony/security-http/Firewall/SimplePreAuthenticationListener.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\Http\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  18. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
  19. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  21. use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  24. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  25. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  27. use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
  28. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  29. use Symfony\Component\Security\Http\SecurityEvents;
  30. use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
  31. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use Guard instead.'SimplePreAuthenticationListener::class), E_USER_DEPRECATED);
  32. /**
  33.  * SimplePreAuthenticationListener implements simple proxying to an authenticator.
  34.  *
  35.  * @author Jordi Boggiano <j.boggiano@seld.be>
  36.  *
  37.  * @deprecated since Symfony 4.2, use Guard instead.
  38.  */
  39. class SimplePreAuthenticationListener implements ListenerInterface
  40. {
  41.     use LegacyListenerTrait;
  42.     private $tokenStorage;
  43.     private $authenticationManager;
  44.     private $providerKey;
  45.     private $simpleAuthenticator;
  46.     private $logger;
  47.     private $dispatcher;
  48.     private $sessionStrategy;
  49.     private $trustResolver;
  50.     public function __construct(TokenStorageInterface $tokenStorageAuthenticationManagerInterface $authenticationManagerstring $providerKeySimplePreAuthenticatorInterface $simpleAuthenticatorLoggerInterface $logger nullEventDispatcherInterface $dispatcher nullAuthenticationTrustResolverInterface $trustResolver null)
  51.     {
  52.         if (empty($providerKey)) {
  53.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  54.         }
  55.         $this->tokenStorage $tokenStorage;
  56.         $this->authenticationManager $authenticationManager;
  57.         $this->providerKey $providerKey;
  58.         $this->simpleAuthenticator $simpleAuthenticator;
  59.         $this->logger $logger;
  60.         if (null !== $dispatcher && class_exists(LegacyEventDispatcherProxy::class)) {
  61.             $this->dispatcher LegacyEventDispatcherProxy::decorate($dispatcher);
  62.         } else {
  63.             $this->dispatcher $dispatcher;
  64.         }
  65.         $this->trustResolver $trustResolver ?: new AuthenticationTrustResolver(AnonymousToken::class, RememberMeToken::class);
  66.     }
  67.     /**
  68.      * Call this method if your authentication token is stored to a session.
  69.      *
  70.      * @final
  71.      */
  72.     public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
  73.     {
  74.         $this->sessionStrategy $sessionStrategy;
  75.     }
  76.     /**
  77.      * Handles basic authentication.
  78.      */
  79.     public function __invoke(RequestEvent $event)
  80.     {
  81.         $request $event->getRequest();
  82.         if (null !== $this->logger) {
  83.             $this->logger->info('Attempting SimplePreAuthentication.', ['key' => $this->providerKey'authenticator' => \get_class($this->simpleAuthenticator)]);
  84.         }
  85.         if ((null !== $token $this->tokenStorage->getToken()) && !$this->trustResolver->isAnonymous($token)) {
  86.             return;
  87.         }
  88.         try {
  89.             $token $this->simpleAuthenticator->createToken($request$this->providerKey);
  90.             // allow null to be returned to skip authentication
  91.             if (null === $token) {
  92.                 return;
  93.             }
  94.             $token $this->authenticationManager->authenticate($token);
  95.             $this->migrateSession($request$token);
  96.             $this->tokenStorage->setToken($token);
  97.             if (null !== $this->dispatcher) {
  98.                 $loginEvent = new InteractiveLoginEvent($request$token);
  99.                 $this->dispatcher->dispatch($loginEventSecurityEvents::INTERACTIVE_LOGIN);
  100.             }
  101.         } catch (AuthenticationException $e) {
  102.             $this->tokenStorage->setToken(null);
  103.             if (null !== $this->logger) {
  104.                 $this->logger->info('SimplePreAuthentication request failed.', ['exception' => $e'authenticator' => \get_class($this->simpleAuthenticator)]);
  105.             }
  106.             if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  107.                 $response $this->simpleAuthenticator->onAuthenticationFailure($request$e);
  108.                 if ($response instanceof Response) {
  109.                     $event->setResponse($response);
  110.                 } elseif (null !== $response) {
  111.                     throw new \UnexpectedValueException(sprintf('The %s::onAuthenticationFailure method must return null or a Response object', \get_class($this->simpleAuthenticator)));
  112.                 }
  113.             }
  114.             return;
  115.         }
  116.         if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  117.             $response $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
  118.             if ($response instanceof Response) {
  119.                 $event->setResponse($response);
  120.             } elseif (null !== $response) {
  121.                 throw new \UnexpectedValueException(sprintf('The %s::onAuthenticationSuccess method must return null or a Response object', \get_class($this->simpleAuthenticator)));
  122.             }
  123.         }
  124.     }
  125.     private function migrateSession(Request $requestTokenInterface $token)
  126.     {
  127.         if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
  128.             return;
  129.         }
  130.         $this->sessionStrategy->onAuthentication($request$token);
  131.     }
  132. }