Skip to content

Commit 484430f

Browse files
committed
Guard against precision issues on time fractions
1 parent 1dc8675 commit 484430f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/functional/HmacTokenTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Lcobucci\JWT\FunctionalTests;
55

6+
use DateTimeImmutable;
67
use Lcobucci\JWT\Configuration;
78
use Lcobucci\JWT\Signer\Hmac\Sha256;
89
use Lcobucci\JWT\Signer\Hmac\Sha512;
@@ -131,4 +132,36 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
131132
self::assertTrue($this->config->validator()->validate($token, $constraint));
132133
self::assertEquals('world', $token->claims()->get('hello'));
133134
}
135+
136+
/**
137+
* @test
138+
* @dataProvider datesWithPotentialRoundingIssues
139+
*/
140+
public function timeFractionsPrecisionsAreRespected(DateTimeImmutable $issuedAt): void
141+
{
142+
$token = $this->config->builder()
143+
->issuedAt($issuedAt)
144+
->getToken($this->config->signer(), $this->config->signingKey());
145+
146+
$parsedToken = $this->config->parser()->parse($token->toString());
147+
148+
self::assertInstanceOf(Token\Plain::class, $parsedToken);
149+
self::assertSame($issuedAt->format('U.u'), $parsedToken->claims()->get('iat')->format('U.u'));
150+
}
151+
152+
/** @return iterable<DateTimeImmutable[]> */
153+
public function datesWithPotentialRoundingIssues(): iterable
154+
{
155+
$create = static function (string $timeFraction): DateTimeImmutable {
156+
$date = DateTimeImmutable::createFromFormat('U.u', $timeFraction);
157+
assert($date instanceof DateTimeImmutable);
158+
159+
return $date;
160+
};
161+
162+
yield [$create('1613938511.017448')];
163+
yield [$create('1613938511.023691')];
164+
yield [$create('1613938511.018045')];
165+
yield [$create('1616074725.008455')];
166+
}
134167
}

0 commit comments

Comments
 (0)