Skip to content

Commit

Permalink
Fix usage of non JSON numeric values for time fractions
Browse files Browse the repository at this point in the history
The RFC-7519 states that the `NumericDate` type is:

> JSON numeric value representing the number of seconds from
> 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring
> leap seconds.

Then also mentions that time fractions (as covered by RFC-3339) are supported:

> Seconds Since the Epoch", in which each day is accounted for by
> exactly 86400 seconds, other than that non-integer values can be
> represented.

While adding support for time fractions we've interpreted the
"non-integer" really as any "non-integer" value, and used strings to
guard against precision issues.

That causes issues, since a string isn't a "JSON numeric value"
according to the JSON specs.

We observed that the 6-digit precision is not lost when doing JSON
encode/decode operations, this applies that technique to make sure we
comply to the specs and have "rounding issues" when dealing with floats.
  • Loading branch information
yassinrais authored and lcobucci committed Mar 19, 2021
1 parent ad4729f commit 9a961f4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/Encoding/MicrosecondBasedDateConversion.php
Expand Up @@ -25,16 +25,13 @@ public function formatClaims(array $claims): array
return $claims;
}

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

if ((int) $microseconds === 0) {
return (int) $seconds;
if ($date->format('u') === '000000') {
return (int) $date->format('U');
}

return $seconds . '.' . $microseconds;
return (float) $date->format('U.u');
}
}
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]);
}
}
6 changes: 3 additions & 3 deletions test/unit/Encoding/MicrosecondBasedDateConversionTest.php
Expand Up @@ -36,8 +36,8 @@ public function dateClaimsHaveMicrosecondsOrSeconds(): void
$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.000123, $formatted[RegisteredClaims::NOT_BEFORE]);
self::assertSame(1487285080.123456, $formatted[RegisteredClaims::EXPIRATION_TIME]);
self::assertSame('test', $formatted['testing']); // this should remain untouched
}

Expand All @@ -62,7 +62,7 @@ public function notAllDateClaimsNeedToBeConfigured(): void
$formatted = $formatter->formatClaims($claims);

self::assertSame(1487285080, $formatted[RegisteredClaims::ISSUED_AT]);
self::assertSame('1487285080.123456', $formatted[RegisteredClaims::EXPIRATION_TIME]);
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 9a961f4

Please sign in to comment.