1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Easy; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Checker\HeaderChecker; |
17
|
|
|
use Jose\Component\Checker\InvalidHeaderException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class is a header parameter checker. |
21
|
|
|
* When the "enc" header parameter is present, it will check if the value is within the allowed ones. |
22
|
|
|
*/ |
23
|
|
|
final class ContentEncryptionAlgorithmChecker implements HeaderChecker |
24
|
|
|
{ |
25
|
|
|
private const HEADER_NAME = 'enc'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $protectedHeader = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] |
34
|
|
|
*/ |
35
|
|
|
private $supportedAlgorithms; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string[] $supportedAlgorithms |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $supportedAlgorithms, bool $protectedHeader = false) |
41
|
|
|
{ |
42
|
|
|
$this->supportedAlgorithms = $supportedAlgorithms; |
43
|
|
|
$this->protectedHeader = $protectedHeader; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
* |
49
|
|
|
* @throws InvalidHeaderException if the header is invalid |
50
|
|
|
*/ |
51
|
|
|
public function checkHeader($value): void |
52
|
|
|
{ |
53
|
|
|
if (!\is_string($value)) { |
54
|
|
|
throw new InvalidHeaderException('"enc" must be a string.', self::HEADER_NAME, $value); |
55
|
|
|
} |
56
|
|
|
if (!\in_array($value, $this->supportedAlgorithms, true)) { |
57
|
|
|
throw new InvalidHeaderException('Unsupported algorithm.', self::HEADER_NAME, $value); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function supportedHeader(): string |
62
|
|
|
{ |
63
|
|
|
return self::HEADER_NAME; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function protectedHeaderOnly(): bool |
67
|
|
|
{ |
68
|
|
|
return $this->protectedHeader; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|