From e87879d99d9175aeaef9a6df138662d06c2767cd Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sun, 24 Feb 2019 17:40:08 +0200 Subject: [PATCH] Pass request uri as an argument to error handler (see #96) This is a workaround because changing the error handler signature would break BC. Removing the request in 3.x was an oversight on my part and it will most likely be put back in 4.x. --- src/JwtAuthentication.php | 3 ++- tests/JwtAuthenticationTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/JwtAuthentication.php b/src/JwtAuthentication.php index 8998942..41234d2 100644 --- a/src/JwtAuthentication.php +++ b/src/JwtAuthentication.php @@ -141,7 +141,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface } catch (RuntimeException | DomainException $exception) { $response = (new ResponseFactory)->createResponse(401); return $this->processError($response, [ - "message" => $exception->getMessage() + "message" => $exception->getMessage(), + "uri" => (string)$request->getUri() ]); } diff --git a/tests/JwtAuthenticationTest.php b/tests/JwtAuthenticationTest.php index 4d0ece8..0057193 100644 --- a/tests/JwtAuthenticationTest.php +++ b/tests/JwtAuthenticationTest.php @@ -852,4 +852,33 @@ public function testShouldHandlePsr7() $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals("Success", $response->getBody()); } + + public function testShouldHaveUriInErrorHandlerIssue96() + { + $request = (new ServerRequestFactory) + ->createServerRequest("GET", "https://example.com/api/foo?bar=pop"); + + $dummy = null; + + $default = function (ServerRequestInterface $request) { + $response = (new ResponseFactory)->createResponse(); + $response->getBody()->write("Success"); + return $response; + }; + + $collection = new MiddlewareCollection([ + new JwtAuthentication([ + "secret" => "supersecretkeyyoushouldnotcommit", + "error" => function (ResponseInterface $response, $arguments) use (&$dummy) { + $dummy = $arguments["uri"]; + } + ]) + ]); + + $response = $collection->dispatch($request, $default); + + $this->assertEquals(401, $response->getStatusCode()); + $this->assertEquals("", $response->getBody()); + $this->assertEquals("https://example.com/api/foo?bar=pop", $dummy); + } }