| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Checker; |
| 6 |
|
| 7 |
use function in_array; |
| 8 |
use function is_string; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
final class AlgorithmChecker implements HeaderChecker |
| 15 |
{ |
| 16 |
private const HEADER_NAME = 'alg'; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public function __construct( |
| 22 |
private readonly array $supportedAlgorithms, |
| 23 |
private readonly bool $protectedHeader = false |
| 24 |
) { |
| 25 |
} |
| 26 |
|
| 27 |
public function checkHeader(mixed $value): void |
| 28 |
{ |
| 29 |
if (! is_string($value)) { |
| 30 |
throw new InvalidHeaderException('"alg" must be a string.', self::HEADER_NAME, $value); |
| 31 |
} |
| 32 |
if (! in_array($value, $this->supportedAlgorithms, true)) { |
| 33 |
throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function supportedHeader(): string |
| 38 |
{ |
| 39 |
return self::HEADER_NAME; |
| 40 |
} |
| 41 |
|
| 42 |
public function protectedHeaderOnly(): bool |
| 43 |
{ |
| 44 |
return $this->protectedHeader; |
| 45 |
} |
| 46 |
} |