1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWS\Algorithm; |
6
|
|
|
|
7
|
|
|
use Sop\JWX\JWA\JWA; |
8
|
|
|
use Sop\JWX\JWK\JWK; |
9
|
|
|
use Sop\JWX\JWK\JWKSet; |
10
|
|
|
use Sop\JWX\JWS\SignatureAlgorithm; |
11
|
|
|
use Sop\JWX\JWT\Header\Header; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Factory class to construct signature algorithm instances. |
15
|
|
|
*/ |
16
|
|
|
class SignatureAlgorithmFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Mapping from algorithm name to class name. |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public const MAP_ALGO_TO_CLASS = [ |
26
|
|
|
JWA::ALGO_HS256 => HS256Algorithm::class, |
27
|
|
|
JWA::ALGO_HS384 => HS384Algorithm::class, |
28
|
|
|
JWA::ALGO_HS512 => HS512Algorithm::class, |
29
|
|
|
JWA::ALGO_RS256 => RS256Algorithm::class, |
30
|
|
|
JWA::ALGO_RS384 => RS384Algorithm::class, |
31
|
|
|
JWA::ALGO_RS512 => RS512Algorithm::class, |
32
|
|
|
JWA::ALGO_ES256 => ES256Algorithm::class, |
33
|
|
|
JWA::ALGO_ES384 => ES384Algorithm::class, |
34
|
|
|
JWA::ALGO_ES512 => ES512Algorithm::class, |
35
|
|
|
]; |
36
|
|
|
/** |
37
|
|
|
* Header. |
38
|
|
|
* |
39
|
|
|
* @var Header |
40
|
|
|
*/ |
41
|
|
|
protected $_header; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
*/ |
46
|
14 |
|
public function __construct(Header $header) |
47
|
|
|
{ |
48
|
14 |
|
$this->_header = $header; |
49
|
14 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get signature algorithm by given JWK. |
53
|
|
|
*/ |
54
|
11 |
|
public function algoByKey(JWK $jwk): SignatureAlgorithm |
55
|
|
|
{ |
56
|
11 |
|
$alg = JWA::deriveAlgorithmName($this->_header, $jwk); |
57
|
11 |
|
$cls = self::_algoClassByName($alg); |
58
|
10 |
|
return $cls::fromJWK($jwk, $this->_header); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get signature algorithm using a matching key from given JWK set. |
63
|
|
|
* |
64
|
|
|
* @throws \UnexpectedValueException If a key cannot be found |
65
|
|
|
*/ |
66
|
7 |
|
public function algoByKeys(JWKSet $set): SignatureAlgorithm |
67
|
|
|
{ |
68
|
7 |
|
if (!$this->_header->hasKeyID()) { |
69
|
1 |
|
throw new \UnexpectedValueException('No key ID paremeter.'); |
70
|
|
|
} |
71
|
6 |
|
$id = $this->_header->keyID()->value(); |
72
|
6 |
|
if (!$set->hasKeyID($id)) { |
73
|
2 |
|
throw new \UnexpectedValueException("No key for ID '{$id}'."); |
74
|
|
|
} |
75
|
4 |
|
return $this->algoByKey($set->keyByID($id)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the algorithm implementation class name by an algorithm name. |
80
|
|
|
* |
81
|
|
|
* @param string $alg Algorithm name |
82
|
|
|
* |
83
|
|
|
* @throws \UnexpectedValueException |
84
|
|
|
* |
85
|
|
|
* @return string Class name |
86
|
|
|
*/ |
87
|
11 |
|
private static function _algoClassByName(string $alg): string |
88
|
|
|
{ |
89
|
11 |
|
if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) { |
90
|
1 |
|
throw new \UnexpectedValueException( |
91
|
1 |
|
"Algorithm '{$alg}' not supported."); |
92
|
|
|
} |
93
|
10 |
|
return self::MAP_ALGO_TO_CLASS[$alg]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|