Skip to content

Commit

Permalink
Guard against precision issues on time fractions
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Mar 19, 2021
1 parent 1dc8675 commit 1cdc268
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/functional/HmacTokenTest.php
Expand Up @@ -3,6 +3,7 @@

namespace Lcobucci\JWT\FunctionalTests;

use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Hmac\Sha512;
Expand Down Expand Up @@ -131,4 +132,28 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
self::assertTrue($this->config->validator()->validate($token, $constraint));
self::assertEquals('world', $token->claims()->get('hello'));
}

/**
* @test
* @dataProvider datesWithPotentialRoundingIssues
*/
public function timeFractionsPrecisionsAreRespected(DateTimeImmutable $issuedAt): void
{
$token = $this->config->builder()
->issuedAt($issuedAt)
->getToken($this->config->signer(), $this->config->signingKey());

$parsedToken = $this->config->parser()->parse($token->toString());

self::assertInstanceOf(Token\Plain::class, $parsedToken);
self::assertSame($issuedAt->format('U.u'), $parsedToken->claims()->get('iat')->format('U.u'));
}

public function datesWithPotentialRoundingIssues(): iterable
{
yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.017448')];
yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.023691')];
yield [DateTimeImmutable::createFromFormat('U.u', '1613938511.018045')];
yield [DateTimeImmutable::createFromFormat('U.u', '1616074725.008455')];
}
}

0 comments on commit 1cdc268

Please sign in to comment.