* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Security\Http\Firewall; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface; use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\Security\Core\Exception\AccountStatusException; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; use Symfony\Component\Security\Core\Exception\LogoutException; use Symfony\Component\Security\Http\Util\TargetPathTrait; use Symfony\Component\Security\Http\HttpUtils; use Symfony\Component\HttpFoundation\Request; use Psr\Log\LoggerInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * ExceptionListener catches authentication exception and converts them to * Response instances. * * @author Fabien Potencier */ class ExceptionListener { use TargetPathTrait; private $tokenStorage; private $providerKey; private $accessDeniedHandler; private $authenticationEntryPoint; private $authenticationTrustResolver; private $errorPage; private $logger; private $httpUtils; private $stateless; public function __construct(TokenStorageInterface $tokenStorage, AuthenticationTrustResolverInterface $trustResolver, HttpUtils $httpUtils, $providerKey, AuthenticationEntryPointInterface $authenticationEntryPoint = null, $errorPage = null, AccessDeniedHandlerInterface $accessDeniedHandler = null, LoggerInterface $logger = null, $stateless = false) { $this->tokenStorage = $tokenStorage; $this->accessDeniedHandler = $accessDeniedHandler; $this->httpUtils = $httpUtils; $this->providerKey = $providerKey; $this->authenticationEntryPoint = $authenticationEntryPoint; $this->authenticationTrustResolver = $trustResolver; $this->errorPage = $errorPage; $this->logger = $logger; $this->stateless = $stateless; } /** * Registers a onKernelException listener to take care of security exceptions. */ public function register(EventDispatcherInterface $dispatcher) { $dispatcher->addListener(KernelEvents::EXCEPTION, array($this, 'onKernelException'), 1); } /** * Unregisters the dispatcher. */ public function unregister(EventDispatcherInterface $dispatcher) { $dispatcher->removeListener(KernelEvents::EXCEPTION, array($this, 'onKernelException')); } /** * Handles security related exceptions. */ public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); do { if ($exception instanceof AuthenticationException) { return $this->handleAuthenticationException($event, $exception); } elseif ($exception instanceof AccessDeniedException) { return $this->handleAccessDeniedException($event, $exception); } elseif ($exception instanceof LogoutException) { return $this->handleLogoutException($exception); } } while (null !== $exception = $exception->getPrevious()); } private function handleAuthenticationException(GetResponseForExceptionEvent $event, AuthenticationException $exception) { if (null !== $this->logger) { $this->logger->info('An AuthenticationException was thrown; redirecting to authentication entry point.', array('exception' => $exception)); } try { $event->setResponse($this->startAuthentication($event->getRequest(), $exception)); $event->allowCustomResponseCode(); } catch (\Exception $e) { $event->setException($e); } } private function handleAccessDeniedException(GetResponseForExceptionEvent $event, AccessDeniedException $exception) { $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception)); $token = $this->tokenStorage->getToken(); if (!$this->authenticationTrustResolver->isFullFledged($token)) { if (null !== $this->logger) { $this->logger->debug('Access denied, the user is not fully authenticated; redirecting to authentication entry point.', array('exception' => $exception)); } try { $insufficientAuthenticationException = new InsufficientAuthenticationException('Full authentication is required to access this resource.', 0, $exception); $insufficientAuthenticationException->setToken($token); $event->setResponse($this->startAuthentication($event->getRequest(), $insufficientAuthenticationException)); } catch (\Exception $e) { $event->setException($e); } return; } if (null !== $this->logger) { $this->logger->debug('Access denied, the user is neither anonymous, nor remember-me.', array('exception' => $exception)); } try { if (null !== $this->accessDeniedHandler) { $response = $this->accessDeniedHandler->handle($event->getRequest(), $exception); if ($response instanceof Response) { $event->setResponse($response); } } elseif (null !== $this->errorPage) { $subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage); $subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception); $event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true)); $event->allowCustomResponseCode(); } } catch (\Exception $e) { if (null !== $this->logger) { $this->logger->error('An exception was thrown when handling an AccessDeniedException.', array('exception' => $e)); } $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e)); } } private function handleLogoutException(LogoutException $exception) { if (null !== $this->logger) { $this->logger->info('A LogoutException was thrown.', array('exception' => $exception)); } } /** * @return Response * * @throws AuthenticationException */ private function startAuthentication(Request $request, AuthenticationException $authException) { if (null === $this->authenticationEntryPoint) { throw $authException; } if (null !== $this->logger) { $this->logger->debug('Calling Authentication entry point.'); } if (!$this->stateless) { $this->setTargetPath($request); } if ($authException instanceof AccountStatusException) { // remove the security token to prevent infinite redirect loops $this->tokenStorage->setToken(null); if (null !== $this->logger) { $this->logger->info('The security token was removed due to an AccountStatusException.', array('exception' => $authException)); } } $response = $this->authenticationEntryPoint->start($request, $authException); if (!$response instanceof Response) { $given = is_object($response) ? get_class($response) : gettype($response); throw new \LogicException(sprintf('The %s::start() method must return a Response object (%s returned)', get_class($this->authenticationEntryPoint), $given)); } return $response; } protected function setTargetPath(Request $request) { // session isn't required when using HTTP basic authentication mechanism for example if ($request->hasSession() && $request->isMethodSafe(false) && !$request->isXmlHttpRequest()) { $this->saveTargetPath($request->getSession(), $this->providerKey, $request->getUri()); } } } __halt_compiler();----SIGNATURE:----jK1pkNpQ2qdPjh+ZZkipX6s7ncLFrqXn2+D57dFAhaQWmEVE9zY/Skc3SXh2oAcxxpN7VW6S9Z0ks86esf8Pa0z06nQDRwotvLnwN38gTwmK/cYLIT6yFw2uqBaYq2GVONewemJrQhP/XckSB9KTQ90NZmxPcG4HGujb+++tP7HoI9Q1UcguOm25lpUwNugAi3pmjLVCmtncYF5PnI7YhCtY8dbbaHasy5UltwTJjMgOGFU8jObaO1b2k8LNmdUFl/yzIdORMDjrdqlzk9qEUBsqY+6iBUv7IFmAwbi0OjLf8aVeBrgHKQLFljo1KW58QOAlUvV83PSY6X8L1F0eXAy4+olYf4Z9SVOqOTz5BBEWYJR1wg6oweFVa/dSsxp3kecaiOOYNM6IC1e1F92vEOOw1kC38kVrKTsXP6m5LL1jaybN8yScUunHKqYdF2U8Sg1I1KNrL68Awx0xTEDvmEXggXBq/yA3U5P7v7xhPCMda9F4jY8kxyFT1yJy8cbQs60kESOv2QL9R6n/M4fx1l2ZbQtNd3Zt1NCf8Q4/WNOiiVUkVpfHaSdIVzLanokXw6ashy7iXekOxWFn3uWquJgGLmFImRu3xectNQcRJLFL6+5irKgaElvgMe5xv1M9VU40ETrNh765s5leOj5t6HoIeTF4yYDMYnLo0yC9om0=----ATTACHMENT:----Mjk3MjgzMjIzMDMzNjU4MCA5MzM0NTA2NDc5NDk0MjcgMzA1MTIyMDc4MDQ4ODc4MA==