vendor/symfony/security-http/Authenticator/LoginLinkAuthenticator.php line 32

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\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\Http\Authentication\AuthenticationFailureHandlerInterface;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  19. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  21. use Symfony\Component\Security\Http\HttpUtils;
  22. use Symfony\Component\Security\Http\LoginLink\Exception\InvalidLoginLinkAuthenticationException;
  23. use Symfony\Component\Security\Http\LoginLink\Exception\InvalidLoginLinkExceptionInterface;
  24. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  25. /**
  26.  * @author Ryan Weaver <ryan@symfonycasts.com>
  27.  */
  28. final class LoginLinkAuthenticator extends AbstractAuthenticator implements InteractiveAuthenticatorInterface
  29. {
  30.     private $loginLinkHandler;
  31.     private $httpUtils;
  32.     private $successHandler;
  33.     private $failureHandler;
  34.     private $options;
  35.     public function __construct(LoginLinkHandlerInterface $loginLinkHandlerHttpUtils $httpUtilsAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $options)
  36.     {
  37.         $this->loginLinkHandler $loginLinkHandler;
  38.         $this->httpUtils $httpUtils;
  39.         $this->successHandler $successHandler;
  40.         $this->failureHandler $failureHandler;
  41.         $this->options $options + ['check_post_only' => false];
  42.     }
  43.     public function supports(Request $request): ?bool
  44.     {
  45.         return ($this->options['check_post_only'] ? $request->isMethod('POST') : true)
  46.             && $this->httpUtils->checkRequestPath($request$this->options['check_route']);
  47.     }
  48.     public function authenticate(Request $request): PassportInterface
  49.     {
  50.         $username $request->get('user');
  51.         if (!$username) {
  52.             throw new InvalidLoginLinkAuthenticationException('Missing user from link.');
  53.         }
  54.         return new SelfValidatingPassport(
  55.             new UserBadge($username, function () use ($request) {
  56.                 try {
  57.                     $user $this->loginLinkHandler->consumeLoginLink($request);
  58.                 } catch (InvalidLoginLinkExceptionInterface $e) {
  59.                     throw new InvalidLoginLinkAuthenticationException('Login link could not be validated.'0$e);
  60.                 }
  61.                 return $user;
  62.             }),
  63.             [new RememberMeBadge()]
  64.         );
  65.     }
  66.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  67.     {
  68.         return $this->successHandler->onAuthenticationSuccess($request$token);
  69.     }
  70.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  71.     {
  72.         return $this->failureHandler->onAuthenticationFailure($request$exception);
  73.     }
  74.     public function isInteractive(): bool
  75.     {
  76.         return true;
  77.     }
  78. }