Skip to content

Commit 0a53cf2

Browse files
chore: better BeforeValidException message for decode (#526)
1 parent 299105a commit 0a53cf2

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Diff for: src/JWT.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static function decode(
154154
// token can actually be used. If it's not yet that time, abort.
155155
if (isset($payload->nbf) && floor($payload->nbf) > ($timestamp + static::$leeway)) {
156156
throw new BeforeValidException(
157-
'Cannot handle token prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
157+
'Cannot handle token with nbf prior to ' . \date(DateTime::ISO8601, (int) $payload->nbf)
158158
);
159159
}
160160

@@ -163,7 +163,7 @@ public static function decode(
163163
// correctly used the nbf claim).
164164
if (!isset($payload->nbf) && isset($payload->iat) && floor($payload->iat) > ($timestamp + static::$leeway)) {
165165
throw new BeforeValidException(
166-
'Cannot handle token prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
166+
'Cannot handle token with iat prior to ' . \date(DateTime::ISO8601, (int) $payload->iat)
167167
);
168168
}
169169

Diff for: tests/JWTTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public function testInvalidTokenWithNbfLeeway()
147147
];
148148
$encoded = JWT::encode($payload, 'my_key', 'HS256');
149149
$this->expectException(BeforeValidException::class);
150+
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
150151
JWT::decode($encoded, new Key('my_key', 'HS256'));
151152
}
152153

@@ -176,6 +177,7 @@ public function testValidTokenWithNbfMicrotime()
176177
public function testInvalidTokenWithNbfMicrotime()
177178
{
178179
$this->expectException(BeforeValidException::class);
180+
$this->expectExceptionMessage('Cannot handle token with nbf prior to');
179181
$payload = [
180182
'message' => 'abc',
181183
'nbf' => microtime(true) + 20, // use microtime in the future
@@ -211,6 +213,7 @@ public function testInvalidTokenWithIatLeeway()
211213
];
212214
$encoded = JWT::encode($payload, 'my_key', 'HS256');
213215
$this->expectException(BeforeValidException::class);
216+
$this->expectExceptionMessage('Cannot handle token with iat prior to');
214217
JWT::decode($encoded, new Key('my_key', 'HS256'));
215218
}
216219

@@ -228,6 +231,7 @@ public function testValidTokenWithIatMicrotime()
228231
public function testInvalidTokenWithIatMicrotime()
229232
{
230233
$this->expectException(BeforeValidException::class);
234+
$this->expectExceptionMessage('Cannot handle token with iat prior to');
231235
$payload = [
232236
'message' => 'abc',
233237
'iat' => microtime(true) + 20, // use microtime in the future

0 commit comments

Comments
 (0)