jwt: support float and string timestamps

This commit is contained in:
Yuri Kuznetsov
2023-12-06 11:15:30 +02:00
parent ccee4776a8
commit 6a84c7583e
2 changed files with 37 additions and 7 deletions
@@ -179,23 +179,23 @@ class Payload
throw new RuntimeException("Bad `aud`.");
}
if ($exp !== null && !is_int($exp)) {
throw new RuntimeException("No or bad `exp`.");
if ($exp !== null && !is_numeric($exp)) {
throw new RuntimeException("Bad `exp`.");
}
if ($iat !== null && !is_int($iat)) {
throw new RuntimeException("No or bad `iat`.");
if ($iat !== null && !is_numeric($iat)) {
throw new RuntimeException("Bad `iat`.");
}
if ($nbf !== null && !is_int($nbf)) {
throw new RuntimeException("No or bad `nbf`.");
if ($nbf !== null && !is_numeric($nbf)) {
throw new RuntimeException("Bad `nbf`.");
}
if ($nonce !== null && !is_string($nonce)) {
throw new RuntimeException("Bad `nonce`.");
}
if ($authTime !== null && !is_int($authTime)) {
if ($authTime !== null && !is_numeric($authTime)) {
throw new RuntimeException("Bad `auth_time`.");
}
@@ -203,6 +203,22 @@ class Payload
throw new RuntimeException("Bad `sid`.");
}
if ($exp !== null) {
$exp = (int) $exp;
}
if ($iat !== null) {
$iat = (int) $iat;
}
if ($nbf !== null) {
$nbf = (int) $nbf;
}
if ($authTime !== null) {
$authTime = (int) $authTime;
}
return new self(
$sub,
$iss,
@@ -51,6 +51,20 @@ class TokenTest extends TestCase
$this->assertEquals('John Doe', $token->getPayload()->get('name'));
}
public function testToken2(): void
{
$raw = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0Ij" .
"oxNTE2MjM5MDIyLCJleHAiOjE1MTYyMzgwMjIsIm5iZiI6MTUxNjIzODAyMywiYXV0aF90aW1lIjoiMTUxNjIzODAyNCJ9." .
"7Z45aUyLR9o8lUaIvxkO7SzOhTaXgfXf_rEFYnvTPL8";
$token = Token::create($raw);
$this->assertEquals(1516238022, $token->getPayload()->getExp());
$this->assertEquals(1516239022, $token->getPayload()->getIat());
$this->assertEquals(1516238023, $token->getPayload()->getNbf());
$this->assertEquals(1516238024, $token->getPayload()->getAuthTime());
}
public function testVerifySignatureHS256(): void
{
$raw = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9." .