Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing session cookie (split cookie) #958

Merged
merged 1 commit into from
Dec 8, 2021
Merged
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
2 changes: 1 addition & 1 deletion DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function load(array $configs, ContainerBuilder $container)
$container
->setDefinition($id = "lexik_jwt_authentication.cookie_provider.$name", new ChildDefinition('lexik_jwt_authentication.cookie_provider'))
->replaceArgument(0, $name)
->replaceArgument(1, $attributes['lifetime'] ?: ($config['token_ttl'] ?: 0))
->replaceArgument(1, $attributes['lifetime'] ?? ($config['token_ttl'] ?: 0))
->replaceArgument(2, $attributes['samesite'])
->replaceArgument(3, $attributes['path'])
->replaceArgument(4, $attributes['domain'])
Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/1-configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ set_cookies:
### Automatically generating split cookies
You are also able to automatically generate split cookies. Benefits of this approach are in [this post](https://medium.com/lightrail/getting-token-authentication-right-in-a-stateless-single-page-application-57d0c6474e3).

Set the signature cookie (jwt_s) lifetime to 0 to create session cookies.

Keep in mind, that SameSite attribute is **not supported** in [some browsers](https://caniuse.com/#feat=same-site-cookie-attribute)

```
Expand All @@ -138,7 +140,7 @@ set_cookies:
- payload

jwt_s:
lifetime: null
lifetime: 0
samesite: strict
path: /
domain: null
Expand Down
8 changes: 6 additions & 2 deletions Security/Http/Cookie/JWTCookieProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,21 @@ public function createCookie(string $jwt, ?string $name = null, $expiresAt = nul
throw new \LogicException(sprintf('The cookie name must be provided, either pass it as 2nd argument of %s or set a default name via the constructor.', __METHOD__));
}

if (!$expiresAt && !$this->defaultLifetime) {
if (!$expiresAt && null === $this->defaultLifetime) {
throw new \LogicException(sprintf('The cookie expiration time must be provided, either pass it as 3rd argument of %s or set a default lifetime via the constructor.', __METHOD__));
}

$jwtParts = new JWTSplitter($jwt);
$jwt = $jwtParts->getParts($split ?: $this->defaultSplit);

if (null === $expiresAt) {
$expiresAt = 0 === $this->defaultLifetime ? 0 : (time() + $this->defaultLifetime);
}

return new Cookie(
$name ?: $this->defaultName,
$jwt,
null === $expiresAt ? (time() + $this->defaultLifetime) : $expiresAt,
$expiresAt,
$path ?: $this->defaultPath,
$domain ?: $this->defaultDomain,
$secure ?: $this->defaultSecure,
Expand Down
38 changes: 38 additions & 0 deletions Tests/Security/Http/Cookie/JWTCookieProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\Tests\Security\Http\Cookie;

use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider;
use PHPUnit\Framework\TestCase;

/**
* JWTCookieProviderTest.
*/
class JWTCookieProviderTest extends TestCase
{
public function testCreateCookieWithExpiration()
{
$expiresAt = time() + 3600;
$cookieProvider = new JWTCookieProvider("default_name");
$cookie = $cookieProvider->createCookie("header.payload.signature", "name", $expiresAt);

$this->assertEquals($expiresAt, $cookie->getExpiresTime());
}

public function testCreateCookieWithLifetime()
{
$lifetime = 3600;
$cookieProvider = new JWTCookieProvider("default_name", $lifetime);
$cookie = $cookieProvider->createCookie("header.payload.signature");

$this->assertEquals(time() + $lifetime, $cookie->getExpiresTime());
}

public function testCreateSessionCookie()
{
$cookieProvider = new JWTCookieProvider("default_name", 0);
$cookie = $cookieProvider->createCookie("header.payload.signature");

$this->assertEquals(0, $cookie->getExpiresTime());
}
}