-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
set_cookie
option to store JWT in secure cookies
- Loading branch information
Showing
8 changed files
with
159 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="lexik_jwt_authentication.cookie_provider" class="Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider"> | ||
<argument>null</argument> | ||
<argument>null</argument> | ||
</service> | ||
<service id="Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie\JWTCookieProvider" alias="lexik_jwt_authentication.cookie_provider" /> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Cookie; | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
/** | ||
* Creates secure JWT cookies. | ||
*/ | ||
final class JWTCookieProvider | ||
{ | ||
private $defaultCookieName; | ||
private $defaultCookieLifetime; | ||
|
||
/** | ||
* @param string|null $defaultCookieName | ||
* @param int|null $defaultCookieLifetime | ||
*/ | ||
public function __construct($defaultCookieName = null, $defaultCookieLifetime = null) | ||
{ | ||
$this->defaultCookieName = $defaultCookieName; | ||
$this->defaultCookieLifetime = $defaultCookieLifetime; | ||
} | ||
|
||
/** | ||
* @param string $jwt | ||
* @param string|null $name The cookie name. If null, the default will be used. | ||
* @param int|string|\DateTimeInterface|null $expiresAt The cookie expiration datetime. If null, the default one will be used | ||
* @param null $domain | ||
* @param null $path | ||
* | ||
* @return Cookie | ||
*/ | ||
public function createCookie($jwt, $name = null, $expiresAt = null, $sameSite = Cookie::SAMESITE_LAX, $domain = null, $path = '/') | ||
{ | ||
if (!$name && !$this->defaultCookieName) { | ||
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->defaultCookieLifetime) { | ||
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__)); | ||
} | ||
|
||
return new Cookie( | ||
$name ?: $this->defaultCookieName, | ||
$jwt, | ||
null === $expiresAt ? (time() + $this->defaultCookieLifetime) : $expiresAt, | ||
$path, | ||
$domain, | ||
true, | ||
true, | ||
false, | ||
$sameSite | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters