vendor/symfony/security-http/Authenticator/FormLoginAuthenticator.php line 42

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\HttpKernel\Exception\BadRequestHttpException;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  21. use Symfony\Component\Security\Core\User\UserProviderInterface;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
  24. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  27. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  28. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  29. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  30. use Symfony\Component\Security\Http\HttpUtils;
  31. use Symfony\Component\Security\Http\ParameterBagUtils;
  32. /**
  33.  * @author Wouter de Jong <wouter@wouterj.nl>
  34.  * @author Fabien Potencier <fabien@symfony.com>
  35.  *
  36.  * @final
  37.  */
  38. class FormLoginAuthenticator extends AbstractLoginFormAuthenticator
  39. {
  40.     private HttpUtils $httpUtils;
  41.     private UserProviderInterface $userProvider;
  42.     private AuthenticationSuccessHandlerInterface $successHandler;
  43.     private AuthenticationFailureHandlerInterface $failureHandler;
  44.     private array $options;
  45.     private HttpKernelInterface $httpKernel;
  46.     public function __construct(HttpUtils $httpUtilsUserProviderInterface $userProviderAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandler, array $options)
  47.     {
  48.         $this->httpUtils $httpUtils;
  49.         $this->userProvider $userProvider;
  50.         $this->successHandler $successHandler;
  51.         $this->failureHandler $failureHandler;
  52.         $this->options array_merge([
  53.             'username_parameter' => '_username',
  54.             'password_parameter' => '_password',
  55.             'check_path' => '/login_check',
  56.             'post_only' => true,
  57.             'form_only' => false,
  58.             'enable_csrf' => false,
  59.             'csrf_parameter' => '_csrf_token',
  60.             'csrf_token_id' => 'authenticate',
  61.         ], $options);
  62.     }
  63.     protected function getLoginUrl(Request $request): string
  64.     {
  65.         return $this->httpUtils->generateUri($request$this->options['login_path']);
  66.     }
  67.     public function supports(Request $request): bool
  68.     {
  69.         return ($this->options['post_only'] ? $request->isMethod('POST') : true)
  70.             && $this->httpUtils->checkRequestPath($request$this->options['check_path'])
  71.             && ($this->options['form_only'] ? 'form' === $request->getContentType() : true);
  72.     }
  73.     public function authenticate(Request $request): Passport
  74.     {
  75.         $credentials $this->getCredentials($request);
  76.         $passport = new Passport(
  77.             new UserBadge($credentials['username'], $this->userProvider->loadUserByIdentifier(...)),
  78.             new PasswordCredentials($credentials['password']),
  79.             [new RememberMeBadge()]
  80.         );
  81.         if ($this->options['enable_csrf']) {
  82.             $passport->addBadge(new CsrfTokenBadge($this->options['csrf_token_id'], $credentials['csrf_token']));
  83.         }
  84.         if ($this->userProvider instanceof PasswordUpgraderInterface) {
  85.             $passport->addBadge(new PasswordUpgradeBadge($credentials['password'], $this->userProvider));
  86.         }
  87.         return $passport;
  88.     }
  89.     public function createToken(Passport $passportstring $firewallName): TokenInterface
  90.     {
  91.         return new UsernamePasswordToken($passport->getUser(), $firewallName$passport->getUser()->getRoles());
  92.     }
  93.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  94.     {
  95.         return $this->successHandler->onAuthenticationSuccess($request$token);
  96.     }
  97.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  98.     {
  99.         return $this->failureHandler->onAuthenticationFailure($request$exception);
  100.     }
  101.     private function getCredentials(Request $request): array
  102.     {
  103.         $credentials = [];
  104.         $credentials['csrf_token'] = ParameterBagUtils::getRequestParameterValue($request$this->options['csrf_parameter']);
  105.         if ($this->options['post_only']) {
  106.             $credentials['username'] = ParameterBagUtils::getParameterBagValue($request->request$this->options['username_parameter']);
  107.             $credentials['password'] = ParameterBagUtils::getParameterBagValue($request->request$this->options['password_parameter']) ?? '';
  108.         } else {
  109.             $credentials['username'] = ParameterBagUtils::getRequestParameterValue($request$this->options['username_parameter']);
  110.             $credentials['password'] = ParameterBagUtils::getRequestParameterValue($request$this->options['password_parameter']) ?? '';
  111.         }
  112.         if (!\is_string($credentials['username']) && !$credentials['username'] instanceof \Stringable) {
  113.             throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.'$this->options['username_parameter'], \gettype($credentials['username'])));
  114.         }
  115.         $credentials['username'] = trim($credentials['username']);
  116.         if (\strlen($credentials['username']) > Security::MAX_USERNAME_LENGTH) {
  117.             throw new BadCredentialsException('Invalid username.');
  118.         }
  119.         $request->getSession()->set(Security::LAST_USERNAME$credentials['username']);
  120.         return $credentials;
  121.     }
  122.     public function setHttpKernel(HttpKernelInterface $httpKernel): void
  123.     {
  124.         $this->httpKernel $httpKernel;
  125.     }
  126.     public function start(Request $requestAuthenticationException $authException null): Response
  127.     {
  128.         if (!$this->options['use_forward']) {
  129.             return parent::start($request$authException);
  130.         }
  131.         $subRequest $this->httpUtils->createRequest($request$this->options['login_path']);
  132.         $response $this->httpKernel->handle($subRequestHttpKernelInterface::SUB_REQUEST);
  133.         if (200 === $response->getStatusCode()) {
  134.             $response->setStatusCode(401);
  135.         }
  136.         return $response;
  137.     }
  138. }