|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWK\Symmetric; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWK\JWK; |
|
8
|
|
|
use Sop\JWX\JWK\Parameter\JWKParameter; |
|
9
|
|
|
use Sop\JWX\JWK\Parameter\KeyTypeParameter; |
|
10
|
|
|
use Sop\JWX\JWK\Parameter\KeyValueParameter; |
|
11
|
|
|
use Sop\JWX\Util\Base64; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* JWK containing a symmetric key. |
|
15
|
|
|
* |
|
16
|
|
|
* @see http://tools.ietf.org/html/rfc7518#section-6.4 |
|
17
|
|
|
*/ |
|
18
|
|
|
class SymmetricKeyJWK extends JWK |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Parameter names managed by this class. |
|
22
|
|
|
* |
|
23
|
|
|
* @internal |
|
24
|
|
|
* |
|
25
|
|
|
* @var string[] |
|
26
|
|
|
*/ |
|
27
|
|
|
public const MANAGED_PARAMS = [ |
|
28
|
|
|
JWKParameter::PARAM_KEY_TYPE, |
|
29
|
|
|
JWKParameter::PARAM_KEY_VALUE, |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param JWKParameter ...$params |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \UnexpectedValueException If missing required parameter |
|
38
|
|
|
*/ |
|
39
|
69 |
|
public function __construct(JWKParameter ...$params) |
|
40
|
|
|
{ |
|
41
|
69 |
|
parent::__construct(...$params); |
|
42
|
69 |
|
foreach (self::MANAGED_PARAMS as $name) { |
|
43
|
69 |
|
if (!$this->has($name)) { |
|
44
|
1 |
|
throw new \UnexpectedValueException( |
|
45
|
69 |
|
"Missing '{$name}' parameter."); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
68 |
|
if (KeyTypeParameter::TYPE_OCT !== $this->keyTypeParameter()->value()) { |
|
49
|
1 |
|
throw new \UnexpectedValueException('Invalid key type.'); |
|
50
|
|
|
} |
|
51
|
67 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Initialize from a key string. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $key Symmetric key |
|
57
|
|
|
* @param JWKParameter ...$params Optional additional parameters |
|
58
|
|
|
*/ |
|
59
|
39 |
|
public static function fromKey(string $key, JWKParameter ...$params): self |
|
60
|
|
|
{ |
|
61
|
39 |
|
$params[] = new KeyTypeParameter(KeyTypeParameter::TYPE_OCT); |
|
62
|
39 |
|
$params[] = KeyValueParameter::fromString($key); |
|
63
|
39 |
|
return new self(...$params); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the symmetric key. |
|
68
|
|
|
*/ |
|
69
|
39 |
|
public function key(): string |
|
70
|
|
|
{ |
|
71
|
39 |
|
return Base64::urlDecode($this->keyValueParameter()->value()); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|