-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIssuerChecker.php
58 lines (47 loc) · 1.39 KB
/
IssuerChecker.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
<?php
declare(strict_types=1);
namespace Jose\Component\Checker;
use function in_array;
use function is_string;
/**
* This class is a header parameter and claim checker. When the "iss" header parameter or claim is present, it will
* check if the value is within the allowed ones.
*/
final class IssuerChecker implements ClaimChecker, HeaderChecker
{
private const CLAIM_NAME = 'iss';
public function __construct(
private readonly array $issuers,
private readonly bool $protectedHeader = false
) {
}
public function checkClaim(mixed $value): void
{
$this->checkValue($value, InvalidClaimException::class);
}
public function checkHeader(mixed $value): void
{
$this->checkValue($value, InvalidHeaderException::class);
}
public function supportedClaim(): string
{
return self::CLAIM_NAME;
}
public function supportedHeader(): string
{
return self::CLAIM_NAME;
}
public function protectedHeaderOnly(): bool
{
return $this->protectedHeader;
}
private function checkValue(mixed $value, string $class): void
{
if (! is_string($value)) {
throw new $class('Invalid value.', self::CLAIM_NAME, $value);
}
if (! in_array($value, $this->issuers, true)) {
throw new $class('Unknown issuer.', self::CLAIM_NAME, $value);
}
}
}