| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Checker; |
| 6 |
|
| 7 |
use Psr\Clock\ClockInterface; |
| 8 |
use function is_float; |
| 9 |
use function is_int; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
final class IssuedAtChecker implements ClaimChecker, HeaderChecker |
| 15 |
{ |
| 16 |
private const NAME = 'iat'; |
| 17 |
|
| 18 |
private readonly ClockInterface $clock; |
| 19 |
|
| 20 |
public function __construct( |
| 21 |
private readonly int $allowedTimeDrift = 0, |
| 22 |
private readonly bool $protectedHeaderOnly = false, |
| 23 |
?ClockInterface $clock = null, |
| 24 |
) { |
| 25 |
if ($clock === null) { |
| 26 |
trigger_deprecation( |
| 27 |
'web-token/jwt-library', |
| 28 |
'3.3.0', |
| 29 |
'The parameter "$clock" will become mandatory in 4.0.0. Please set a valid PSR Clock implementation instead of "null".' |
| 30 |
); |
| 31 |
$clock = new InternalClock(); |
| 32 |
} |
| 33 |
$this->clock = $clock; |
| 34 |
} |
| 35 |
|
| 36 |
public function checkClaim(mixed $value): void |
| 37 |
{ |
| 38 |
if (! is_float($value) && ! is_int($value)) { |
| 39 |
throw new InvalidClaimException('"iat" must be an integer.', self::NAME, $value); |
| 40 |
} |
| 41 |
|
| 42 |
$now = $this->clock->now() |
| 43 |
->getTimestamp(); |
| 44 |
if ($now < $value - $this->allowedTimeDrift) { |
| 45 |
throw new InvalidClaimException('The JWT is issued in the future.', self::NAME, $value); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function supportedClaim(): string |
| 50 |
{ |
| 51 |
return self::NAME; |
| 52 |
} |
| 53 |
|
| 54 |
public function checkHeader(mixed $value): void |
| 55 |
{ |
| 56 |
if (! is_float($value) && ! is_int($value)) { |
| 57 |
throw new InvalidHeaderException('The header "iat" must be an integer.', self::NAME, $value); |
| 58 |
} |
| 59 |
|
| 60 |
$now = $this->clock->now() |
| 61 |
->getTimestamp(); |
| 62 |
if ($now < $value - $this->allowedTimeDrift) { |
| 63 |
throw new InvalidHeaderException('The JWT is issued in the future.', self::NAME, $value); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function supportedHeader(): string |
| 68 |
{ |
| 69 |
return self::NAME; |
| 70 |
} |
| 71 |
|
| 72 |
public function protectedHeaderOnly(): bool |
| 73 |
{ |
| 74 |
return $this->protectedHeaderOnly; |
| 75 |
} |
| 76 |
} |