Skip to content

Commit

Permalink
Authorization is now X-Signature #9696
Browse files Browse the repository at this point in the history
So we can still use `Authorization` header for authentification (eg: via
token bearer), in addition of signature.
  • Loading branch information
PowerKiKi committed Jul 25, 2023
1 parent 27e9dba commit 433ac3e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/Middleware/SignedQueryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Psr\Http\Server\RequestHandlerInterface;

/**
* Validate that the GraphQL query contains a valid signature in the `Authorization` HTTP header.
* Validate that the GraphQL query contains a valid signature in the `X-Signature` HTTP header.
*
* The signature payload is the GraphQL operation (or operations in case of batching). That means that the query itself
* and the variables are signed. But it specifically does **not** include uploaded files.
Expand Down Expand Up @@ -52,23 +52,23 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

private function verify(ServerRequestInterface $request): void
{
$autorization = $request->getHeader('authorization')[0] ?? '';
if (!$autorization) {
$signature = $request->getHeader('X-Signature')[0] ?? '';
if (!$signature) {
if ($this->isAllowedIp($request)) {
return;
}

throw new Exception('Missing `Authorization` HTTP header in signed query', 403);
throw new Exception('Missing `X-Signature` HTTP header in signed query', 403);
}

if (preg_match('~^v1\.(?<timestamp>\d{10})\.(?<hash>[0-9a-f]{64})$~', $autorization, $m)) {
if (preg_match('~^v1\.(?<timestamp>\d{10})\.(?<hash>[0-9a-f]{64})$~', $signature, $m)) {
$timestamp = $m['timestamp'];
$hash = $m['hash'];

$this->verifyTimestamp($timestamp);
$this->verifyHash($request, $timestamp, $hash);
} else {
throw new Exception('Invalid `Authorization` HTTP header in signed query', 403);
throw new Exception('Invalid `X-Signature` HTTP header in signed query', 403);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Middleware/SignedQueryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function process(array $keys, bool $required, string $ip, string $body,
$request = $request->withBody(new CallbackStream(fn () => $body))->withParsedBody($parsedBody);

if ($signature) {
$request = $request->withHeader('Authorization', $signature);
$request = $request->withHeader('X-Signature', $signature);
}

$handler = $this->createMock(RequestHandlerInterface::class);
Expand Down Expand Up @@ -162,15 +162,15 @@ public function dataProviderQuery(): iterable
'{"operationName":"CurrentUser","variables":{},"query":"query CurrentUser { viewer { id }}',
null,
'',
'Missing `Authorization` HTTP header in signed query',
'Missing `X-Signature` HTTP header in signed query',
];

yield 'invalid header' => [
[$key1],
'{"operationName":"CurrentUser","variables":{},"query":"query CurrentUser { viewer { id }}',
null,
'foo',
'Invalid `Authorization` HTTP header in signed query',
'Invalid `X-Signature` HTTP header in signed query',
];

yield 'no graphql operations' => [
Expand Down

0 comments on commit 433ac3e

Please sign in to comment.