DefaultAuthenticationFailureHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 $httpKernel, HttpUtils $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 An array of options
  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 $request, AuthenticationException $exception)
  64. {
  65. if ($failureUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['failure_path_parameter'])) {
  66. $this->options['failure_path'] = $failureUrl;
  67. }
  68. if (null === $this->options['failure_path']) {
  69. $this->options['failure_path'] = $this->options['login_path'];
  70. }
  71. if ($this->options['failure_forward']) {
  72. if (null !== $this->logger) {
  73. $this->logger->debug('Authentication failure, forward triggered.', ['failure_path' => $this->options['failure_path']]);
  74. }
  75. $subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
  76. $subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
  77. return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  78. }
  79. if (null !== $this->logger) {
  80. $this->logger->debug('Authentication failure, redirect triggered.', ['failure_path' => $this->options['failure_path']]);
  81. }
  82. $request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
  83. return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
  84. }
  85. }