From 94144271acb789abfbb3c5af42a17c38a594c090 Mon Sep 17 00:00:00 2001 From: arslan Date: Thu, 2 May 2019 18:18:35 +0500 Subject: [PATCH] Fixed AuthenticaionFailureHandler to utilize messages from custom exceptions --- .../Http/Authentication/AuthenticationFailureHandler.php | 5 ++++- Tests/Functional/GetTokenTest.php | 2 +- .../Authentication/AuthenticationFailureHandlerTest.php | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Security/Http/Authentication/AuthenticationFailureHandler.php b/Security/Http/Authentication/AuthenticationFailureHandler.php index b0d68376..b00e6aa3 100644 --- a/Security/Http/Authentication/AuthenticationFailureHandler.php +++ b/Security/Http/Authentication/AuthenticationFailureHandler.php @@ -36,7 +36,10 @@ public function __construct(EventDispatcherInterface $dispatcher) */ public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { - $event = new AuthenticationFailureEvent($exception, new JWTAuthenticationFailureResponse()); + $event = new AuthenticationFailureEvent( + $exception, + new JWTAuthenticationFailureResponse($exception->getMessage()) + ); if ($this->dispatcher instanceof ContractsEventDispatcherInterface) { $this->dispatcher->dispatch($event, Events::AUTHENTICATION_FAILURE); diff --git a/Tests/Functional/GetTokenTest.php b/Tests/Functional/GetTokenTest.php index 27350460..7d11f8e2 100644 --- a/Tests/Functional/GetTokenTest.php +++ b/Tests/Functional/GetTokenTest.php @@ -62,7 +62,7 @@ public function testGetTokenFromInvalidCredentials() $this->assertArrayHasKey('message', $body, 'The response should have a "message" key containing the failure reason.'); $this->assertArrayHasKey('code', $body, 'The response should have a "code" key containing the response status code.'); - $this->assertSame('Bad credentials', $body['message']); + $this->assertSame('Bad credentials.', $body['message']); $this->assertSame(401, $body['code']); } } diff --git a/Tests/Security/Http/Authentication/AuthenticationFailureHandlerTest.php b/Tests/Security/Http/Authentication/AuthenticationFailureHandlerTest.php index dfd2192f..7d82cd83 100644 --- a/Tests/Security/Http/Authentication/AuthenticationFailureHandlerTest.php +++ b/Tests/Security/Http/Authentication/AuthenticationFailureHandlerTest.php @@ -23,14 +23,16 @@ public function testOnAuthenticationFailure() ->disableOriginalConstructor() ->getMock(); + $authenticationException = $this->getAuthenticationException(); + $handler = new AuthenticationFailureHandler($dispatcher); - $response = $handler->onAuthenticationFailure($this->getRequest(), $this->getAuthenticationException()); + $response = $handler->onAuthenticationFailure($this->getRequest(), $authenticationException); $content = json_decode($response->getContent(), true); $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response); $this->assertEquals(401, $response->getStatusCode()); $this->assertEquals(401, $content['code']); - $this->assertEquals('Bad credentials', $content['message']); + $this->assertEquals($authenticationException->getMessage(), $content['message']); } /**