Skip to content

Commit

Permalink
Merge pull request #560 from Slamdunk/jwt_5
Browse files Browse the repository at this point in the history
Add support for lcobucci/jwt:v5
  • Loading branch information
Ocramius authored Feb 26, 2023
2 parents 77351b9 + 8041220 commit 480cf58
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": "~8.1.0 || ~8.2.0",
"ext-json": "*",
"dflydev/fig-cookies": "^3.0.0",
"lcobucci/jwt": "^4.3.0",
"lcobucci/jwt": "^4.3.0 || ^5.0.0",
"lcobucci/clock": "^3.0.0",
"psr/http-message": "^1.0.1",
"psr/http-server-handler": "^1.0.1",
Expand Down
57 changes: 29 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Storageless/Http/SessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ private function parseToken(Request $request): UnencryptedToken|null
return null;
}

$cookie = $cookies[$cookieName];
if ($cookie === '') {
return null;
}

try {
$token = $this->config->parser()->parse($cookies[$cookieName]);
$token = $this->config->parser()->parse($cookie);
} catch (InvalidArgumentException) {
return null;
}
Expand Down
17 changes: 16 additions & 1 deletion test/StoragelessTest/Http/SessionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function testInjectsSessionInResponseCookies(callable $middlewareFactory)
$token = $this->getCookie($response)->getValue();

self::assertIsString($token);
self::assertTrue($token !== '');
$parsedToken = (new Parser(new JoseEncoder()))->parse($token);
self::assertInstanceOf(Plain::class, $parsedToken);
self::assertEquals(['foo' => 'bar'], $parsedToken->claims()->get('session-data'));
Expand Down Expand Up @@ -328,6 +329,20 @@ public function testWillIgnoreSignedTokensWithoutIssuedAt(callable $middlewareFa
$this->ensureSameResponse($middleware, $unsignedToken, $this->emptyValidationMiddleware());
}

/**
* @param callable(): SessionMiddleware $middlewareFactory
*
* @dataProvider validMiddlewaresProvider
*/
public function testWillIgnoreRequestsWithEmptyStringCookie(callable $middlewareFactory): void
{
$middleware = $middlewareFactory();
$expiredToken = (new ServerRequest())
->withCookieParams([SessionMiddleware::DEFAULT_COOKIE => '']);

$this->ensureSameResponse($middleware, $expiredToken, $this->emptyValidationMiddleware());
}

public function testWillRefreshTokenWithIssuedAtExactlyAtTokenRefreshTimeThreshold(): void
{
// forcing ourselves to think of time as a mutable value:
Expand Down Expand Up @@ -367,7 +382,7 @@ public function testWillRefreshTokenWithIssuedAtExactlyAtTokenRefreshTimeThresho
->getValue();

self::assertIsString($tokenString);

self::assertTrue($tokenString !== '');
$token = (new Parser(new JoseEncoder()))->parse($tokenString);
self::assertInstanceOf(Plain::class, $token);
self::assertEquals($now, $token->claims()->get(RegisteredClaims::ISSUED_AT), 'Token was refreshed');
Expand Down

0 comments on commit 480cf58

Please sign in to comment.