Skip to content

Commit

Permalink
allow tokens via header
Browse files Browse the repository at this point in the history
  • Loading branch information
blizzz committed Jan 21, 2024
1 parent 9c75e38 commit 5d515aa
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$value = $body[$this->getTokenValueKey()] ?? null;
}

if ($name === null && $value === null) {
// DELETE request may not have a request body. Supply token by headers
$name = $request->getHeader($this->getTokenNameKey())[0] ?? null;
$value = $request->getHeader($this->getTokenValueKey())[0] ?? null;
}

if (in_array($request->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
$isValid = $this->validateToken((string) $name, (string) $value);
if ($isValid && !$this->persistentTokenMode) {
Expand Down
56 changes: 56 additions & 0 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public function testSetFailureHandler()
->willReturn([])
->shouldBeCalledOnce();

$requestProphecy
->getHeader(Argument::type('string'))
->willReturn([])
->shouldBeCalledTimes(2);

$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);

$mw->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
Expand Down Expand Up @@ -174,6 +179,11 @@ public function testDefaultFailureHandler()
->willReturn([])
->shouldBeCalledOnce();

$requestProphecy
->getHeader(Argument::type('string'))
->willReturn([])
->shouldBeCalledTimes(2);

$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);

$response = $mw->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
Expand Down Expand Up @@ -476,6 +486,7 @@ public function testProcessAppendsNewTokensWhenPersistentTokenModeIsOff()

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy->getParsedBody()->willReturn(null)->shouldBeCalledOnce();
$requestProphecy->getHeader(Argument::type('string'))->willReturn([])->shouldBeCalledTimes(2);
$requestProphecy
->getMethod()
->willReturn('GET')
Expand Down Expand Up @@ -508,6 +519,7 @@ public function testProcessAppendsNewTokensWhenPersistentTokenModeIsOn()

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy->getParsedBody()->willReturn(null)->shouldBeCalledOnce();
$requestProphecy->getHeader(Argument::type('string'))->willReturn([])->shouldBeCalledTimes(2);
$requestProphecy
->getMethod()
->willReturn('GET')
Expand Down Expand Up @@ -556,4 +568,48 @@ public function testCanGetLastKeyPairFromIterator()
$unmaskedToken = $unmaskTokenMethod->invoke($mw, $keyPair['test_value']);
$this->assertEquals('value2', $unmaskedToken);
}

public function testTokenFromHeaderOnDelete()
{
$storage = [
'test_name' => 'test_value123',
];

$responseProphecy = $this->prophesize(ResponseInterface::class)
->willImplement(ResponseInterface::class);

$requestHandlerProphecy = $this->prophesize(RequestHandlerInterface::class);
$requestHandlerProphecy
->handle(Argument::type(ServerRequestInterface::class))
->willReturn($responseProphecy->reveal())
->shouldBeCalledOnce();

$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);

$mw = new Guard($responseFactoryProphecy->reveal(), 'test', $storage);

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy
->getMethod()
->willReturn('DELETE')
->shouldBeCalledOnce();
$requestProphecy
->withAttribute(Argument::type('string'), Argument::type('string'))
->willReturn($requestProphecy->reveal())
->shouldBeCalledTimes(2);
$requestProphecy
->getParsedBody()
->willReturn([])
->shouldBeCalledOnce();
$requestProphecy
->getHeader('test_name')
->willReturn(['test_name'])
->shouldBeCalledOnce();
$requestProphecy
->getHeader('test_value')
->willReturn([$this->maskToken($mw, 'test_value123')])
->shouldBeCalledOnce();

$mw->process($requestProphecy->reveal(), $requestHandlerProphecy->reveal());
}
}

0 comments on commit 5d515aa

Please sign in to comment.