1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWE\EncryptionAlgorithm; |
4
|
|
|
|
5
|
|
|
use JWX\JWA\JWA; |
6
|
|
|
use JWX\JWE\ContentEncryptionAlgorithm; |
7
|
|
|
use JWX\JWE\EncryptionAlgorithm\A128CBCHS256Algorithm; |
8
|
|
|
use JWX\JWE\EncryptionAlgorithm\A128GCMAlgorithm; |
9
|
|
|
use JWX\JWE\EncryptionAlgorithm\A192CBCHS384Algorithm; |
10
|
|
|
use JWX\JWE\EncryptionAlgorithm\A192GCMAlgorithm; |
11
|
|
|
use JWX\JWE\EncryptionAlgorithm\A256CBCHS512Algorithm; |
12
|
|
|
use JWX\JWE\EncryptionAlgorithm\A256GCMAlgorithm; |
13
|
|
|
use JWX\JWT\Header\Header; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Factory class to construct content encryption algorithm instances. |
18
|
|
|
*/ |
19
|
|
|
abstract class EncryptionAlgorithmFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Mapping from algorithm name to class name. |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
const MAP_ALGO_TO_CLASS = array( |
29
|
|
|
/* @formatter:off */ |
30
|
|
|
JWA::ALGO_A128CBC_HS256 => A128CBCHS256Algorithm::class, |
31
|
|
|
JWA::ALGO_A192CBC_HS384 => A192CBCHS384Algorithm::class, |
32
|
|
|
JWA::ALGO_A256CBC_HS512 => A256CBCHS512Algorithm::class, |
33
|
|
|
JWA::ALGO_A128GCM => A128GCMAlgorithm::class, |
34
|
|
|
JWA::ALGO_A192GCM => A192GCMAlgorithm::class, |
35
|
|
|
JWA::ALGO_A256GCM => A256GCMAlgorithm::class |
36
|
|
|
/* @formatter:on */ |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the content encryption algorithm by algorithm name. |
41
|
|
|
* |
42
|
|
|
* @param string $name Algorithm name |
43
|
|
|
* @throws \UnexpectedValueException If algorithm is not supported. |
44
|
|
|
* @return ContentEncryptionAlgorithm |
45
|
|
|
*/ |
46
|
18 |
View Code Duplication |
public static function algoByName($name) { |
|
|
|
|
47
|
18 |
|
if (!array_key_exists($name, self::MAP_ALGO_TO_CLASS)) { |
48
|
1 |
|
throw new \UnexpectedValueException( |
49
|
1 |
|
"No content encryption algorithm '$name'."); |
50
|
|
|
} |
51
|
17 |
|
$cls = self::MAP_ALGO_TO_CLASS[$name]; |
52
|
17 |
|
return new $cls(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the content encryption algorithm as specified in the given header. |
57
|
|
|
* |
58
|
|
|
* @param Header $header Header |
59
|
|
|
* @throws \UnexpectedValueException If content encryption algorithm |
60
|
|
|
* parameter is not present or algorithm is not supported. |
61
|
|
|
* @return ContentEncryptionAlgorithm |
62
|
|
|
*/ |
63
|
3 |
|
public static function algoByHeader(Header $header) { |
64
|
3 |
|
if (!$header->hasEncryptionAlgorithm()) { |
65
|
1 |
|
throw new \UnexpectedValueException( |
66
|
1 |
|
"No encryption algorithm parameter."); |
67
|
|
|
} |
68
|
2 |
|
return self::algoByName($header->encryptionAlgorithm()->value()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.