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