Skip to content

Commit ad4729f

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

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Lcobucci\JWT\FunctionalTests;
5+
6+
use DateTimeImmutable;
7+
use Lcobucci\JWT\Configuration;
8+
use Lcobucci\JWT\Token\Plain;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @covers \Lcobucci\JWT\Configuration
13+
* @covers \Lcobucci\JWT\Encoding\JoseEncoder
14+
* @covers \Lcobucci\JWT\Encoding\ChainedFormatter
15+
* @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion
16+
* @covers \Lcobucci\JWT\Encoding\UnifyAudience
17+
* @covers \Lcobucci\JWT\Token\Builder
18+
* @covers \Lcobucci\JWT\Token\Parser
19+
* @covers \Lcobucci\JWT\Token\Plain
20+
* @covers \Lcobucci\JWT\Token\DataSet
21+
* @covers \Lcobucci\JWT\Token\Signature
22+
* @covers \Lcobucci\JWT\Signer\Key\InMemory
23+
* @covers \Lcobucci\JWT\Signer\None
24+
* @covers \Lcobucci\JWT\Validation\Validator
25+
* @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated
26+
* @covers \Lcobucci\JWT\Validation\Constraint\SignedWith
27+
*/
28+
final class TimeFractionPrecisionTest extends TestCase
29+
{
30+
/**
31+
* @test
32+
* @dataProvider datesWithPotentialRoundingIssues
33+
*/
34+
public function timeFractionsPrecisionsAreRespected(string $timeFraction): void
35+
{
36+
$config = Configuration::forUnsecuredSigner();
37+
38+
$issuedAt = DateTimeImmutable::createFromFormat('U.u', $timeFraction);
39+
40+
self::assertInstanceOf(DateTimeImmutable::class, $issuedAt);
41+
self::assertSame($timeFraction, $issuedAt->format('U.u'));
42+
43+
$token = $config->builder()
44+
->issuedAt($issuedAt)
45+
->getToken($config->signer(), $config->signingKey());
46+
47+
$parsedToken = $config->parser()->parse($token->toString());
48+
49+
self::assertInstanceOf(Plain::class, $parsedToken);
50+
self::assertSame($timeFraction, $parsedToken->claims()->get('iat')->format('U.u'));
51+
}
52+
53+
/** @return iterable<string[]> */
54+
public function datesWithPotentialRoundingIssues(): iterable
55+
{
56+
yield ['1613938511.017448'];
57+
yield ['1613938511.023691'];
58+
yield ['1613938511.018045'];
59+
yield ['1616074725.008455'];
60+
}
61+
}

0 commit comments

Comments
 (0)