|
3 | 3 |
|
4 | 4 | namespace Lcobucci\JWT\FunctionalTests; |
5 | 5 |
|
| 6 | +use DateTimeImmutable; |
6 | 7 | use Lcobucci\JWT\Configuration; |
7 | 8 | use Lcobucci\JWT\Signer\Hmac\Sha256; |
8 | 9 | use Lcobucci\JWT\Signer\Hmac\Sha512; |
@@ -131,4 +132,36 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void |
131 | 132 | self::assertTrue($this->config->validator()->validate($token, $constraint)); |
132 | 133 | self::assertEquals('world', $token->claims()->get('hello')); |
133 | 134 | } |
| 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 | + } |
134 | 167 | } |
0 commit comments