Debian Codegraph @

lib/3rdParty/composer/facile-it/php-jose-verifier/src/Internal/Checker/ContentEncryptionAlgorithmChecker.php

raw symbols SCIP
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 * This class is a header parameter checker.
12 * When the "enc" header parameter is present, it will check if the value is within the allowed ones.
13 *
14 * @internal
15 */
16 final class ContentEncryptionAlgorithmChecker implements HeaderChecker
17 {
18 private const HEADER_NAME = 'enc';
19
20 private bool $protectedHeader;
21
22 /** @var string[] */
23 private array $supportedAlgorithms;
24
25 /**
26 * @param string[] $supportedAlgorithms
27 */
28 public function __construct(array $supportedAlgorithms, bool $protectedHeader = false)
29 {
30 $this->supportedAlgorithms = $supportedAlgorithms;
31 $this->protectedHeader = $protectedHeader;
32 }
33
34 /**
35 * @param mixed $value
36 *
37 * @throws InvalidHeaderException if the header is invalid
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 }