| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\JoseVerifier\Internal\Checker; |
| 6 |
|
| 7 |
use Facile\JoseVerifier\Internal\InternalClock; |
| 8 |
use Jose\Component\Checker\ClaimChecker; |
| 9 |
use Jose\Component\Checker\InvalidClaimException; |
| 10 |
use Psr\Clock\ClockInterface; |
| 11 |
|
| 12 |
use function is_int; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class AuthTimeChecker implements ClaimChecker |
| 18 |
{ |
| 19 |
private const CLAIM_NAME = 'auth_time'; |
| 20 |
|
| 21 |
private int $maxAge; |
| 22 |
|
| 23 |
private int $allowedTimeDrift; |
| 24 |
|
| 25 |
private ClockInterface $clock; |
| 26 |
|
| 27 |
public function __construct(int $maxAge, int $allowedTimeDrift = 0, ?ClockInterface $clock = null) |
| 28 |
{ |
| 29 |
$this->maxAge = $maxAge; |
| 30 |
$this->allowedTimeDrift = $allowedTimeDrift; |
| 31 |
$this->clock = $clock ?? new InternalClock(); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function checkClaim(mixed $value): void |
| 38 |
{ |
| 39 |
if (! is_int($value)) { |
| 40 |
throw new InvalidClaimException('"auth_time" must be an integer.', self::CLAIM_NAME, $value); |
| 41 |
} |
| 42 |
|
| 43 |
if ($value + $this->maxAge < $this->clock->now()->getTimestamp() - $this->allowedTimeDrift) { |
| 44 |
throw new InvalidClaimException('Too much time has elapsed since the last End-User authentication.', self::CLAIM_NAME, $value); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function supportedClaim(): string |
| 49 |
{ |
| 50 |
return self::CLAIM_NAME; |
| 51 |
} |
| 52 |
} |