You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using JWTAuthenticationBundle in combination with GesdinetJWTRefreshTokenBundle. I have an event listener created, which in my case checks if the user is an admin. If so, I set him a longer token expiration time (also refresh token).
I tried to change the expiration time of the cookies, but only the time in the token changes. When setting in cookie lifetime: null, the value of the ttl parameter is taken and the listener does not modify the cookie time value.
My current solution is to use the lexik_jwt_authentication.on_jwt_created event listener described in the documentation, where I modify the token time stored in the cookie. Then using lexik_jwt_authentication.on_authentication_success, which only accesses the getResponse method, I set a new cookie, having previously deleted the ones created by the bundle.
Summary: event listener changes the validity time of the token, but does not change the validity time of cookies.
// src/EventListener/JWTCreatedListener.php
//This code works correctly, the validity time encoded in the token changes as expected. But not in the cookie
<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
class JWTCreatedListener
{
public function onJWTCreated(JWTCreatedEvent $event)
{
$user = $event->getUser();
$payload = $event->getData();
// If admin change token tll to value of JWT_TOKEN_TTL_ADMIN env variable
if (in_array('ROLE_ADMIN', $user->getRoles())) {
$expiration = new \DateTime('+' . $_ENV['JWT_TOKEN_TTL_ADMIN'] . ' seconds');
$payload['exp'] = $expiration->getTimestamp();
}
$event->setData($payload);
}
}
// src/EventListener/JWTAuthenticationSuccessListener.php
// Very ugly temporary code ;), but working properly. Written quickly as an example of a workaround
<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\HttpFoundation\Cookie;
class JWTAuthenticationSuccessListener
{
public function onJWTRefreshCreated(AuthenticationSuccessEvent $event)
{
$user = $event->getUser();
$payload = $event->getData();
$cookiesAdminNamesWithEnv = [
'jwt_hp' => [
'user' => 'JWT_TOKEN_TTL',
'admin' => 'JWT_TOKEN_TTL_ADMIN'
],
'jwt_s' => [
'user' => 'JWT_TOKEN_TTL',
'admin' => 'JWT_TOKEN_TTL_ADMIN'
],
'refresh_token' => [
'user' => 'JWT_REFRESH_TTL',
'admin' => 'JWT_REFRESH_TTL_ADMiN'
],
];
if (in_array('ROLE_ADMIN', $user->getRoles())) {
$cookies = $event->getResponse()->headers->getCookies();
foreach ($cookies as $cookie) {
$cookieName = $cookie->getName();
if(array_key_exists($cookieName, $cookiesAdminNamesWithEnv)) {
$newCookie = new Cookie(
$cookieName,
$cookie->getValue(),
$cookie->getExpiresTime() - (int) $_ENV[$cookiesAdminNamesWithEnv[$cookieName]['user']] + (int) $_ENV[$cookiesAdminNamesWithEnv[$cookieName]['admin']],
$cookie->getPath(),
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttpOnly(),
$cookie->isRaw(),
$cookie->getSameSite(),
$cookie->isPartitioned(),
);
$event->getResponse()->headers->removeCookie($cookieName);
$event->getResponse()->headers->setCookie($newCookie);
}
}
}
$event->setData($payload);
}
}
The text was updated successfully, but these errors were encountered:
Hi,
I am using JWTAuthenticationBundle in combination with GesdinetJWTRefreshTokenBundle. I have an event listener created, which in my case checks if the user is an admin. If so, I set him a longer token expiration time (also refresh token).
I tried to change the expiration time of the cookies, but only the time in the token changes. When setting in cookie lifetime: null, the value of the ttl parameter is taken and the listener does not modify the cookie time value.
My current solution is to use the lexik_jwt_authentication.on_jwt_created event listener described in the documentation, where I modify the token time stored in the cookie. Then using lexik_jwt_authentication.on_authentication_success, which only accesses the getResponse method, I set a new cookie, having previously deleted the ones created by the bundle.
Summary: event listener changes the validity time of the token, but does not change the validity time of cookies.
My files:
The text was updated successfully, but these errors were encountered: