/vendor/symfony/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

https://github.com/israelnoguera/parejas · PHP · 177 lines · 124 code · 26 blank · 27 comment · 16 complexity · ca424a0cb6f286b013b4dae8ca16d15d MD5 · raw file

  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\Firewall;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
  13. use Symfony\Component\Security\Core\SecurityContextInterface;
  14. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  15. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  16. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  19. use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException;
  20. use Symfony\Component\Security\Http\HttpUtils;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  23. use Symfony\Component\HttpKernel\HttpKernelInterface;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  26. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  27. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  28. /**
  29. * ExceptionListener catches authentication exception and converts them to
  30. * Response instances.
  31. *
  32. * @author Fabien Potencier <fabien@symfony.com>
  33. */
  34. class ExceptionListener
  35. {
  36. private $context;
  37. private $accessDeniedHandler;
  38. private $authenticationEntryPoint;
  39. private $authenticationTrustResolver;
  40. private $errorPage;
  41. private $logger;
  42. private $httpUtils;
  43. public function __construct(SecurityContextInterface $context, AuthenticationTrustResolverInterface $trustResolver, HttpUtils $httpUtils, AuthenticationEntryPointInterface $authenticationEntryPoint = null, $errorPage = null, AccessDeniedHandlerInterface $accessDeniedHandler = null, LoggerInterface $logger = null)
  44. {
  45. $this->context = $context;
  46. $this->accessDeniedHandler = $accessDeniedHandler;
  47. $this->httpUtils = $httpUtils;
  48. $this->authenticationEntryPoint = $authenticationEntryPoint;
  49. $this->authenticationTrustResolver = $trustResolver;
  50. $this->errorPage = $errorPage;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * Registers a onKernelException listener to take care of security exceptions.
  55. *
  56. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  57. */
  58. public function register(EventDispatcherInterface $dispatcher)
  59. {
  60. $dispatcher->addListener(KernelEvents::EXCEPTION, array($this, 'onKernelException'));
  61. }
  62. /**
  63. * Handles security related exceptions.
  64. *
  65. * @param GetResponseForExceptionEvent $event An GetResponseForExceptionEvent instance
  66. */
  67. public function onKernelException(GetResponseForExceptionEvent $event)
  68. {
  69. $exception = $event->getException();
  70. $request = $event->getRequest();
  71. // determine the actual cause for the exception
  72. while (null !== $previous = $exception->getPrevious()) {
  73. $exception = $previous;
  74. }
  75. if ($exception instanceof AuthenticationException) {
  76. if (null !== $this->logger) {
  77. $this->logger->info(sprintf('Authentication exception occurred; redirecting to authentication entry point (%s)', $exception->getMessage()));
  78. }
  79. try {
  80. $response = $this->startAuthentication($request, $exception);
  81. } catch (\Exception $e) {
  82. $event->setException($e);
  83. return;
  84. }
  85. } elseif ($exception instanceof AccessDeniedException) {
  86. $token = $this->context->getToken();
  87. if (!$this->authenticationTrustResolver->isFullFledged($token)) {
  88. if (null !== $this->logger) {
  89. $this->logger->debug('Access denied (user is not fully authenticated); redirecting to authentication entry point');
  90. }
  91. try {
  92. $response = $this->startAuthentication($request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception));
  93. } catch (\Exception $e) {
  94. $event->setException($e);
  95. return;
  96. }
  97. } else {
  98. if (null !== $this->logger) {
  99. $this->logger->debug('Access is denied (and user is neither anonymous, nor remember-me)');
  100. }
  101. try {
  102. if (null !== $this->accessDeniedHandler) {
  103. $response = $this->accessDeniedHandler->handle($request, $exception);
  104. if (!$response instanceof Response) {
  105. return;
  106. }
  107. } elseif (null !== $this->errorPage) {
  108. $subRequest = $this->httpUtils->createRequest($request, $this->errorPage);
  109. $subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);
  110. $response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
  111. $response->setStatusCode(403);
  112. } else {
  113. $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
  114. return;
  115. }
  116. } catch (\Exception $e) {
  117. if (null !== $this->logger) {
  118. $this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()));
  119. }
  120. $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
  121. return;
  122. }
  123. }
  124. } else {
  125. return;
  126. }
  127. $event->setResponse($response);
  128. }
  129. private function startAuthentication(Request $request, AuthenticationException $authException)
  130. {
  131. if (null === $this->authenticationEntryPoint) {
  132. throw $authException;
  133. }
  134. if (null !== $this->logger) {
  135. $this->logger->debug('Calling Authentication entry point');
  136. }
  137. $this->setTargetPath($request);
  138. if ($authException instanceof AccountStatusException) {
  139. // remove the security token to prevent infinite redirect loops
  140. $this->context->setToken(null);
  141. }
  142. return $this->authenticationEntryPoint->start($request, $authException);
  143. }
  144. protected function setTargetPath(Request $request)
  145. {
  146. // session isn't required when using http basic authentication mechanism for example
  147. if ($request->hasSession()) {
  148. $request->getSession()->set('_security.target_path', $request->getUri());
  149. }
  150. }
  151. }