Skip to content

Commit f174826

Browse files
authored
feat: store timestamp in ExpiredException (#604)
1 parent 4dbfac0 commit f174826

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/ExpiredException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class ExpiredException extends \UnexpectedValueException implements JWTException
66
{
77
private object $payload;
88

9+
private ?int $timestamp = null;
10+
911
public function setPayload(object $payload): void
1012
{
1113
$this->payload = $payload;
@@ -15,4 +17,14 @@ public function getPayload(): object
1517
{
1618
return $this->payload;
1719
}
20+
21+
public function setTimestamp(int $timestamp): void
22+
{
23+
$this->timestamp = $timestamp;
24+
}
25+
26+
public function getTimestamp(): ?int
27+
{
28+
return $this->timestamp;
29+
}
1830
}

src/JWT.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public static function decode(
185185
if (isset($payload->exp) && ($timestamp - static::$leeway) >= $payload->exp) {
186186
$ex = new ExpiredException('Expired token');
187187
$ex->setPayload($payload);
188+
$ex->setTimestamp($timestamp);
188189
throw $ex;
189190
}
190191

tests/JWTTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ public function testExpiredExceptionPayload()
130130
}
131131
}
132132

133+
/**
134+
* @runInSeparateProcess
135+
*/
136+
public function testExpiredExceptionTimestamp()
137+
{
138+
$this->expectException(ExpiredException::class);
139+
140+
JWT::$timestamp = 98765;
141+
$payload = [
142+
'message' => 'abc',
143+
'exp' => 1234,
144+
];
145+
$encoded = JWT::encode($payload, 'my_key', 'HS256');
146+
147+
try {
148+
JWT::decode($encoded, new Key('my_key', 'HS256'));
149+
} catch (ExpiredException $e) {
150+
$exTimestamp = $e->getTimestamp();
151+
$this->assertSame(98765, $exTimestamp);
152+
throw $e;
153+
}
154+
}
155+
133156
public function testBeforeValidExceptionPayload()
134157
{
135158
$this->expectException(BeforeValidException::class);

0 commit comments

Comments
 (0)