Skip to content

Commit

Permalink
🖋 The issue about timestamp float precsion fixed using a json_encode …
Browse files Browse the repository at this point in the history
…instead of (string)
  • Loading branch information
yassinrais authored and lcobucci committed Mar 19, 2021
1 parent 484430f commit ba4aa2d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 25 deletions.
16 changes: 1 addition & 15 deletions src/Encoding/MicrosecondBasedDateConversion.php
Expand Up @@ -3,7 +3,6 @@

namespace Lcobucci\JWT\Encoding;

use DateTimeImmutable;
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Token\RegisteredClaims;

Expand All @@ -19,22 +18,9 @@ public function formatClaims(array $claims): array
continue;
}

$claims[$claim] = $this->convertDate($claims[$claim]);
$claims[$claim] = (float) $claims[$claim]->format('U.u');
}

return $claims;
}

/** @return int|string */
private function convertDate(DateTimeImmutable $date)
{
$seconds = $date->format('U');
$microseconds = $date->format('u');

if ((int) $microseconds === 0) {
return (int) $seconds;
}

return $seconds . '.' . $microseconds;
}
}
8 changes: 7 additions & 1 deletion src/Token/Parser.php
Expand Up @@ -12,8 +12,12 @@
use function count;
use function explode;
use function is_array;
use function is_string;
use function json_encode;
use function strpos;

use const JSON_THROW_ON_ERROR;

final class Parser implements ParserInterface
{
private Decoder $decoder;
Expand Down Expand Up @@ -105,7 +109,9 @@ private function parseClaims(string $data): array
continue;
}

$claims[$claim] = $this->convertDate((string) $claims[$claim]);
$date = $claims[$claim];

$claims[$claim] = $this->convertDate(is_string($date) ? $date : json_encode($date, JSON_THROW_ON_ERROR));
}

return $claims;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Encoding/ChainedFormatterTest.php
Expand Up @@ -34,6 +34,6 @@ public function formatClaimsShouldApplyAllConfiguredFormatters(): void
$formatted = $formatter->formatClaims($claims);

self::assertSame('test', $formatted[RegisteredClaims::AUDIENCE]);
self::assertSame('1487285080.123456', $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame(1487285080.123456, $formatted[RegisteredClaims::EXPIRATION_TIME]);
}
}
12 changes: 5 additions & 7 deletions test/unit/Encoding/MicrosecondBasedDateConversionTest.php
Expand Up @@ -14,7 +14,6 @@ final class MicrosecondBasedDateConversionTest extends TestCase
* @test
*
* @covers ::formatClaims
* @covers ::convertDate
*/
public function dateClaimsHaveMicrosecondsOrSeconds(): void
{
Expand All @@ -35,17 +34,16 @@ public function dateClaimsHaveMicrosecondsOrSeconds(): void
$formatter = new MicrosecondBasedDateConversion();
$formatted = $formatter->formatClaims($claims);

self::assertSame(1487285080, $formatted[RegisteredClaims::ISSUED_AT]);
self::assertSame('1487285080.000123', $formatted[RegisteredClaims::NOT_BEFORE]);
self::assertSame('1487285080.123456', $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame(1487285080.0, $formatted[RegisteredClaims::ISSUED_AT]);
self::assertSame(1487285080.000123, $formatted[RegisteredClaims::NOT_BEFORE]);
self::assertSame(1487285080.123456, $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame('test', $formatted['testing']); // this should remain untouched
}

/**
* @test
*
* @covers ::formatClaims
* @covers ::convertDate
*/
public function notAllDateClaimsNeedToBeConfigured(): void
{
Expand All @@ -61,8 +59,8 @@ public function notAllDateClaimsNeedToBeConfigured(): void
$formatter = new MicrosecondBasedDateConversion();
$formatted = $formatter->formatClaims($claims);

self::assertSame(1487285080, $formatted[RegisteredClaims::ISSUED_AT]);
self::assertSame('1487285080.123456', $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame(1487285080.0, $formatted[RegisteredClaims::ISSUED_AT]);
self::assertSame(1487285080.123456, $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame('test', $formatted['testing']); // this should remain untouched
}
}
2 changes: 1 addition & 1 deletion test/unit/Token/ParserTest.php
Expand Up @@ -453,7 +453,7 @@ public function parseMustConvertDateClaimsToObjects(): void
{
$data = [
RegisteredClaims::ISSUED_AT => 1486930663,
RegisteredClaims::EXPIRATION_TIME => '1486930757.023055',
RegisteredClaims::EXPIRATION_TIME => 1486930757.023055,
];

$this->decoder->expects(self::exactly(2))
Expand Down

0 comments on commit ba4aa2d

Please sign in to comment.