Skip to content

Commit

Permalink
Pass request uri as an argument to error handler (see #96) (#155)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tuupola committed Mar 10, 2019
1 parent 76c7487 commit f727fb3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]);
}

Expand Down
29 changes: 29 additions & 0 deletions tests/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit f727fb3

Please sign in to comment.