Skip to content

Commit

Permalink
Encode timestamps as float instead of string values
Browse files Browse the repository at this point in the history
Some JWT parsers like auth0/node-jsonwebtoken and jwt.io complain about timestamps formatted as a string.
  • Loading branch information
Stephen Beirlaen authored and StephenBeirlaen committed Dec 23, 2020
1 parent 43cb7a7 commit 18788f1
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/Encoding/MicrosecondBasedDateConversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Lcobucci\JWT\Token\RegisteredClaims;

use function array_key_exists;
use function floatval;

final class MicrosecondBasedDateConversion implements ClaimsFormatter
{
Expand All @@ -25,16 +26,11 @@ public function formatClaims(array $claims): array
return $claims;
}

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

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

return $seconds . '.' . $microseconds;
return floatval($formattedTimestamp);
}
}

0 comments on commit 18788f1

Please sign in to comment.