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

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