-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IssuedAtChecker.php
60 lines (50 loc) · 1.57 KB
/
IssuedAtChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Jose\Component\Checker;
use function is_float;
use function is_int;
/**
* This class is a claim checker. When the "iat" is present, it will compare the value with the current timestamp.
*/
final class IssuedAtChecker implements ClaimChecker, HeaderChecker
{
private const NAME = 'iat';
public function __construct(
private readonly int $allowedTimeDrift = 0,
private readonly bool $protectedHeaderOnly = false
) {
}
/**
* {@inheritdoc}
*/
public function checkClaim(mixed $value): void
{
if (! is_float($value) && ! is_int($value)) {
throw new InvalidClaimException('"iat" must be an integer.', self::NAME, $value);
}
if (time() < $value - $this->allowedTimeDrift) {
throw new InvalidClaimException('The JWT is issued in the future.', self::NAME, $value);
}
}
public function supportedClaim(): string
{
return self::NAME;
}
public function checkHeader(mixed $value): void
{
if (! is_float($value) && ! is_int($value)) {
throw new InvalidHeaderException('The header "iat" must be an integer.', self::NAME, $value);
}
if (time() < $value - $this->allowedTimeDrift) {
throw new InvalidHeaderException('The JWT is issued in the future.', self::NAME, $value);
}
}
public function supportedHeader(): string
{
return self::NAME;
}
public function protectedHeaderOnly(): bool
{
return $this->protectedHeaderOnly;
}
}