vendor/symfony/security-http/Authentication/DefaultAuthenticationFailureHandler.php line 94

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\Authentication;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\HttpKernelInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Core\Security;
  17. use Symfony\Component\Security\Http\HttpUtils;
  18. use Symfony\Component\Security\Http\ParameterBagUtils;
  19. /**
  20.  * Class with the default authentication failure handling logic.
  21.  *
  22.  * Can be optionally be extended from by the developer to alter the behavior
  23.  * while keeping the default behavior.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  27.  * @author Alexander <iam.asm89@gmail.com>
  28.  */
  29. class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
  30. {
  31.     protected $httpKernel;
  32.     protected $httpUtils;
  33.     protected $logger;
  34.     protected $options;
  35.     protected $defaultOptions = [
  36.         'failure_path' => null,
  37.         'failure_forward' => false,
  38.         'login_path' => '/login',
  39.         'failure_path_parameter' => '_failure_path',
  40.     ];
  41.     public function __construct(HttpKernelInterface $httpKernelHttpUtils $httpUtils, array $options = [], LoggerInterface $logger null)
  42.     {
  43.         $this->httpKernel $httpKernel;
  44.         $this->httpUtils $httpUtils;
  45.         $this->logger $logger;
  46.         $this->setOptions($options);
  47.     }
  48.     /**
  49.      * Gets the options.
  50.      */
  51.     public function getOptions(): array
  52.     {
  53.         return $this->options;
  54.     }
  55.     public function setOptions(array $options)
  56.     {
  57.         $this->options array_merge($this->defaultOptions$options);
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  63.     {
  64.         $options $this->options;
  65.         $failureUrl ParameterBagUtils::getRequestParameterValue($request$options['failure_path_parameter']);
  66.         if (\is_string($failureUrl) && (str_starts_with($failureUrl'/') || str_starts_with($failureUrl'http'))) {
  67.             $options['failure_path'] = $failureUrl;
  68.         } elseif ($this->logger && $failureUrl) {
  69.             $this->logger->debug(sprintf('Ignoring query parameter "%s": not a valid URL.'$options['failure_path_parameter']));
  70.         }
  71.         $options['failure_path'] ??= $options['login_path'];
  72.         if ($options['failure_forward']) {
  73.             $this->logger?->debug('Authentication failure, forward triggered.', ['failure_path' => $options['failure_path']]);
  74.             $subRequest $this->httpUtils->createRequest($request$options['failure_path']);
  75.             $subRequest->attributes->set(Security::AUTHENTICATION_ERROR$exception);
  76.             return $this->httpKernel->handle($subRequestHttpKernelInterface::SUB_REQUEST);
  77.         }
  78.         $this->logger?->debug('Authentication failure, redirect triggered.', ['failure_path' => $options['failure_path']]);
  79.         $request->getSession()->set(Security::AUTHENTICATION_ERROR$exception);
  80.         return $this->httpUtils->createRedirectResponse($request$options['failure_path']);
  81.     }
  82. }