Skip to content

Commit ea21cc7

Browse files
committed
Fix issue with token generation via DateTimeImmutable instead of int (dependency with lcobucci/jwt ^5.0)
1 parent 02ea57f commit ea21cc7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Subscriber/AdditionalAccessTokenClaimsAndHeaderSubscriber.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
66
use Lexik\Bundle\JWTAuthenticationBundle\Events;
7+
use Psr\Clock\ClockInterface;
78
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
use Symfony\Component\Clock\NativeClock;
810

911
final class AdditionalAccessTokenClaimsAndHeaderSubscriber implements EventSubscriberInterface
1012
{
11-
/**
12-
* @var int|null
13-
*/
14-
private $ttl;
13+
private ?int $ttl;
14+
private ?ClockInterface $clock;
1515

16-
public function __construct(?int $ttl)
16+
public function __construct(?int $ttl, ?ClockInterface $clock = null)
1717
{
1818
$this->ttl = $ttl;
19+
20+
if (null === $clock) {
21+
$this->clock = new NativeClock(new \DateTimeZone('UTC'));
22+
}
1923
}
2024

2125
public static function getSubscribedEvents(): array
@@ -29,14 +33,16 @@ public static function getSubscribedEvents(): array
2933

3034
public function addClaims(JWTCreatedEvent $event): void
3135
{
36+
$now = $this->clock->now();
37+
3238
$claims = [
3339
'jti' => uniqid('', true),
34-
'iat' => time(),
35-
'nbf' => time(),
40+
'iat' => $now,
41+
'nbf' => $now,
3642
];
3743
$data = $event->getData();
3844
if (!array_key_exists('exp', $data) && $this->ttl > 0) {
39-
$claims['exp'] = time() + $this->ttl;
45+
$claims['exp'] = $now->modify(sprintf('+%d second',$this->ttl));
4046
}
4147
$event->setData(array_merge($claims, $data));
4248
}

0 commit comments

Comments
 (0)