Last active
March 10, 2021 04:50
-
-
Save chalasr/7fe121992bd125f2313619aff447df5f to your computer and use it in GitHub Desktop.
Integer values for date claims using lcobucci/jwt v4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\JWT\Encoding; | |
use Lcobucci\JWT\ClaimsFormatter; | |
use Lcobucci\JWT\Token\RegisteredClaims; | |
class UnixTimestampDateConversion implements ClaimsFormatter | |
{ | |
public function formatClaims(array $claims): array | |
{ | |
foreach (RegisteredClaims::DATE_CLAIMS as $claim) { | |
if (! array_key_exists($claim, $claims)) { | |
continue; | |
} | |
$claims[$claim] = $this->convertDate($claims[$claim]); | |
} | |
return $claims; | |
} | |
private function convertDate(\DateTimeImmutable $date): int | |
{ | |
return $date->getTimestamp(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\JWT; | |
use App\JWT\Encoding\UnixTimestampDateConversion; | |
use Lcobucci\JWT\Builder; | |
use Lcobucci\JWT\Configuration; | |
use Lcobucci\JWT\Encoding\JoseEncoder; | |
$builder = $config->builder(new UnixTimestampDateConversion()); | |
$builder->expiresAt(new \DateTimeImmutable('+1hour')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment