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 484430f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 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,36 @@ 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'));
}

/** @return iterable<DateTimeImmutable[]> */
public function datesWithPotentialRoundingIssues(): iterable
{
$create = static function (string $timeFraction): DateTimeImmutable {
$date = DateTimeImmutable::createFromFormat('U.u', $timeFraction);
assert($date instanceof DateTimeImmutable);

return $date;
};

yield [$create('1613938511.017448')];
yield [$create('1613938511.023691')];
yield [$create('1613938511.018045')];
yield [$create('1616074725.008455')];
}
}

0 comments on commit 484430f

Please sign in to comment.