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

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