-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAlgorithmChecker.php
49 lines (41 loc) · 1.17 KB
/
AlgorithmChecker.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
<?php
declare(strict_types=1);
namespace Jose\Component\Checker;
use function in_array;
use function is_string;
/**
* This class is a header parameter checker. When the "alg" header parameter is present, it will check if the value is
* within the allowed ones.
*/
final class AlgorithmChecker implements HeaderChecker
{
private const HEADER_NAME = 'alg';
/**
* @param string[] $supportedAlgorithms
*/
public function __construct(
private readonly array $supportedAlgorithms,
private readonly bool $protectedHeader = false
) {
}
/**
* {@inheritdoc}
*/
public function checkHeader(mixed $value): void
{
if (! is_string($value)) {
throw new InvalidHeaderException('"alg" must be a string.', self::HEADER_NAME, $value);
}
if (! in_array($value, $this->supportedAlgorithms, true)) {
throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value);
}
}
public function supportedHeader(): string
{
return self::HEADER_NAME;
}
public function protectedHeaderOnly(): bool
{
return $this->protectedHeader;
}
}