Convert InsufficientAuthenticationException to HttpException

This commit is contained in:
Vincent Chalamon 2018-10-10 14:43:36 +02:00
parent c57e4e1abc
commit 4503ac8e9f
No known key found for this signature in database
GPG Key ID: 18A95D0A095ABAC5
2 changed files with 10 additions and 8 deletions

View File

@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
@ -171,7 +172,7 @@ class ExceptionListener
private function startAuthentication(Request $request, AuthenticationException $authException) private function startAuthentication(Request $request, AuthenticationException $authException)
{ {
if (null === $this->authenticationEntryPoint) { if (null === $this->authenticationEntryPoint) {
throw $authException; throw new HttpException(Response::HTTP_UNAUTHORIZED, $authException->getMessage(), $authException, array(), $authException->getCode());
} }
if (null !== $this->logger) { if (null !== $this->logger) {

View File

@ -15,6 +15,7 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
@ -30,7 +31,7 @@ class ExceptionListenerTest extends TestCase
/** /**
* @dataProvider getAuthenticationExceptionProvider * @dataProvider getAuthenticationExceptionProvider
*/ */
public function testAuthenticationExceptionWithoutEntryPoint(\Exception $exception, \Exception $eventException = null) public function testAuthenticationExceptionWithoutEntryPoint(\Exception $exception, \Exception $eventException)
{ {
$event = $this->createEvent($exception); $event = $this->createEvent($exception);
@ -38,7 +39,7 @@ class ExceptionListenerTest extends TestCase
$listener->onKernelException($event); $listener->onKernelException($event);
$this->assertNull($event->getResponse()); $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() public function getAuthenticationExceptionProvider()
{ {
return array( return array(
array(new AuthenticationException()), array($e = new AuthenticationException(), new HttpException(Response::HTTP_UNAUTHORIZED, '', $e, array(), 0)),
array(new \LogicException('random', 0, $e = new AuthenticationException()), $e), 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())), $e), 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())), $e), array(new \LogicException('random', 0, $e = new AuthenticationException('embed', 0, new AccessDeniedException())), new HttpException(Response::HTTP_UNAUTHORIZED, 'embed', $e, array(), 0)),
array(new AuthenticationException('random', 0, new \LogicException())), array($e = new AuthenticationException('random', 0, new \LogicException()), new HttpException(Response::HTTP_UNAUTHORIZED, 'random', $e, array(), 0)),
); );
} }