Skip to content

Commit

Permalink
Appropriate HTTP status code #9696
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerKiKi committed Jul 24, 2023
1 parent 30ab2e5 commit 5311a4c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Middleware/SignedQueryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function verify(ServerRequestInterface $request): void
{
$autorization = $request->getHeader('authorization')[0] ?? '';
if (!$autorization) {
throw new Exception('Missing `Authorization` HTTP header in signed query');
throw new Exception('Missing `Authorization` HTTP header in signed query', 403);
}

if (preg_match('~^v1\.(?<timestamp>\d{10})\.(?<hash>[0-9a-f]{64})$~', $autorization, $m)) {
Expand All @@ -60,7 +60,7 @@ private function verify(ServerRequestInterface $request): void
$this->verifyTimestamp($timestamp);
$this->verifyHash($request, $timestamp, $hash);
} else {
throw new Exception('Invalid `Authorization` HTTP header in signed query');
throw new Exception('Invalid `Authorization` HTTP header in signed query', 403);
}
}

Expand All @@ -71,7 +71,7 @@ private function verifyTimestamp(string $timestamp): void
$past = $now - $leeway;
$future = $now + $leeway;
if ($timestamp < $past || $timestamp > $future) {
throw new Exception('Signed query is expired');
throw new Exception('Signed query is expired', 403);
}
}

Expand All @@ -87,7 +87,7 @@ private function verifyHash(ServerRequestInterface $request, string $timestamp,
}
}

throw new Exception('Invalid signed query');
throw new Exception('Invalid signed query', 403);
}

private function getOperations(ServerRequestInterface $request): mixed
Expand All @@ -105,6 +105,6 @@ private function getOperations(ServerRequestInterface $request): mixed
}
}

throw new Exception('Could not find GraphQL operations in request');
throw new Exception('Could not find GraphQL operations in request', 403);
}
}
2 changes: 2 additions & 0 deletions tests/Middleware/SignedQueryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function testNonRequiredSignedQuery(array $keys, string $body, null|array
public function testThrowIfNoKeys(): void
{
$this->expectExceptionMessage('Signed queries are required, but no keys are configured');
$this->expectExceptionCode(0);
new SignedQueryMiddleware([]);
}

Expand All @@ -64,6 +65,7 @@ private function process(array $keys, bool $required, string $body, null|array $

if ($expectExceptionMessage) {
$this->expectExceptionMessage($expectExceptionMessage);
$this->expectExceptionCode(403);
}

$middleware->process($request, $handler);
Expand Down

0 comments on commit 5311a4c

Please sign in to comment.