From 6a84c7583ea45468b7e3d76e39432684697e85bb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 6 Dec 2023 11:15:30 +0200 Subject: [PATCH] jwt: support float and string timestamps --- .../Core/Authentication/Jwt/Token/Payload.php | 30 ++++++++++++++----- .../Core/Authentication/Jwt/TokenTest.php | 14 +++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/application/Espo/Core/Authentication/Jwt/Token/Payload.php b/application/Espo/Core/Authentication/Jwt/Token/Payload.php index 78f241fd35..fd3a6163ac 100644 --- a/application/Espo/Core/Authentication/Jwt/Token/Payload.php +++ b/application/Espo/Core/Authentication/Jwt/Token/Payload.php @@ -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, diff --git a/tests/unit/Espo/Core/Authentication/Jwt/TokenTest.php b/tests/unit/Espo/Core/Authentication/Jwt/TokenTest.php index 64ff68c5b8..c4bbafda1b 100644 --- a/tests/unit/Espo/Core/Authentication/Jwt/TokenTest.php +++ b/tests/unit/Espo/Core/Authentication/Jwt/TokenTest.php @@ -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." .