From f727fb319f97cd8f89a317cdf25ecdc44805a141 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Sun, 10 Mar 2019 08:22:24 +0200 Subject: [PATCH] Pass request uri as an argument to error handler (see #96) (#155) * Pass request uri as an argument to error handler 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. --- CHANGELOG.md | 11 ++++++++++- src/JwtAuthentication.php | 3 ++- tests/JwtAuthenticationTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53a43f1..63d7cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,16 @@ All notable changes to this project will be documented in this file, in reverse "error" => [SomeErrorHandler::class, "error"] ]); ``` - +### Added +- The `error` handler now receives the request uri in the `$arguments` array. This is a workaround for [#96](https://github.com/tuupola/slim-jwt-auth/issues/96) which will be fixed in `4.x`. + ```php + $middleware = new JwtAuthentication([ + "secret" => "supersecretkeyyoushouldnotcommit", + "error" => function ($response, $arguments) { + print_r(arguments["uri"]); + } + ]); + ``` ## [3.2.0](https://github.com/tuupola/slim-jwt-auth/compare/3.1.1...3.2.0) - 2019-01-26 diff --git a/src/JwtAuthentication.php b/src/JwtAuthentication.php index 9b4796f..2c59560 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 8204a0b..bf7aa41 100644 --- a/tests/JwtAuthenticationTest.php +++ b/tests/JwtAuthenticationTest.php @@ -1012,4 +1012,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); + } }