From 4503ac8e9f761820ae4319f86e8cf0ec81c7483a Mon Sep 17 00:00:00 2001 From: Vincent Chalamon Date: Wed, 10 Oct 2018 14:43:36 +0200 Subject: [PATCH] Convert InsufficientAuthenticationException to HttpException --- .../Security/Http/Firewall/ExceptionListener.php | 3 ++- .../Http/Tests/Firewall/ExceptionListenerTest.php | 15 ++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php b/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php index c7a3d8cd95..6c1a170db7 100644 --- a/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php +++ b/src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; @@ -171,7 +172,7 @@ class ExceptionListener private function startAuthentication(Request $request, AuthenticationException $authException) { if (null === $this->authenticationEntryPoint) { - throw $authException; + throw new HttpException(Response::HTTP_UNAUTHORIZED, $authException->getMessage(), $authException, array(), $authException->getCode()); } if (null !== $this->logger) { diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php index 271988c13a..4b0f358cda 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php @@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -30,7 +31,7 @@ class ExceptionListenerTest extends TestCase /** * @dataProvider getAuthenticationExceptionProvider */ - public function testAuthenticationExceptionWithoutEntryPoint(\Exception $exception, \Exception $eventException = null) + public function testAuthenticationExceptionWithoutEntryPoint(\Exception $exception, \Exception $eventException) { $event = $this->createEvent($exception); @@ -38,7 +39,7 @@ class ExceptionListenerTest extends TestCase $listener->onKernelException($event); $this->assertNull($event->getResponse()); - $this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()); + $this->assertEquals($eventException, $event->getException()); } /** @@ -58,11 +59,11 @@ class ExceptionListenerTest extends TestCase public function getAuthenticationExceptionProvider() { return array( - array(new AuthenticationException()), - array(new \LogicException('random', 0, $e = new AuthenticationException()), $e), - array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AuthenticationException())), $e), - array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AccessDeniedException())), $e), - array(new AuthenticationException('random', 0, new \LogicException())), + array($e = new AuthenticationException(), new HttpException(Response::HTTP_UNAUTHORIZED, '', $e, array(), 0)), + array(new \LogicException('random', 0, $e = new AuthenticationException()), new HttpException(Response::HTTP_UNAUTHORIZED, '', $e, array(), 0)), + array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AuthenticationException())), new HttpException(Response::HTTP_UNAUTHORIZED, 'embed', $e, array(), 0)), + array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AccessDeniedException())), new HttpException(Response::HTTP_UNAUTHORIZED, 'embed', $e, array(), 0)), + array($e = new AuthenticationException('random', 0, new \LogicException()), new HttpException(Response::HTTP_UNAUTHORIZED, 'random', $e, array(), 0)), ); }