Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Add request object to callback for errors #141

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$decoded = $this->decodeToken($token);
} catch (RuntimeException | DomainException $exception) {
$response = (new ResponseFactory)->createResponse(401);
return $this->processError($response, [
return $this->processError($request, $response, [
"message" => $exception->getMessage()
]);
}
Expand All @@ -154,7 +154,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

/* Modify $request before calling next middleware. */
if (is_callable($this->options["before"])) {
$response = (new ResponseFactory)->createResponse(200);
$beforeRequest = $this->options["before"]($request, $params);
if ($beforeRequest instanceof ServerRequestInterface) {
$request = $beforeRequest;
Expand Down Expand Up @@ -219,10 +218,13 @@ private function shouldAuthenticate(ServerRequestInterface $request): bool
/**
* Call the error handler if it exists.
*/
private function processError(ResponseInterface $response, array $arguments): ResponseInterface
{
private function processError(
ServerRequestInterface $request,
ResponseInterface $response,
array $arguments
): ResponseInterface {
if (is_callable($this->options["error"])) {
$handlerResponse = $this->options["error"]($response, $arguments);
$handlerResponse = $this->options["error"]($request, $response, $arguments);
if ($handlerResponse instanceof ResponseInterface) {
return $handlerResponse;
}
Expand Down
12 changes: 10 additions & 2 deletions tests/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,11 @@ public function testShouldCallError()
$collection = new MiddlewareCollection([
new JwtAuthentication([
"secret" => "supersecretkeyyoushouldnotcommit",
"error" => function (ResponseInterface $response, $arguments) use (&$dummy) {
"error" => function (
ServerRequestInterface $request,
ResponseInterface $response,
$arguments
) use (&$dummy) {
$dummy = true;
}
])
Expand Down Expand Up @@ -625,7 +629,11 @@ public function testShouldCallErrorAndModifyBody()
$collection = new MiddlewareCollection([
new JwtAuthentication([
"secret" => "supersecretkeyyoushouldnotcommittogithub",
"error" => function (ResponseInterface $response, $arguments) use (&$dummy) {
"error" => function (
ServerRequestInterface $request,
ResponseInterface $response,
$arguments
) use (&$dummy) {
$dummy = true;
$response->getBody()->write("Error");
return $response;
Expand Down