|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2016 Spomky-Labs |
|
7
|
|
|
* |
|
8
|
|
|
* This software may be modified and distributed under the terms |
|
9
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jose\Object; |
|
13
|
|
|
|
|
14
|
|
|
use Assert\Assertion; |
|
15
|
|
|
use Base64Url\Base64Url; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class JWK. |
|
19
|
|
|
*/ |
|
20
|
|
|
final class JWK implements JWKInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $values = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* JWK constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $values |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $values = []) |
|
33
|
|
|
{ |
|
34
|
|
|
Assertion::keyExists($values, 'kty', 'The parameter "kty" is mandatory.'); |
|
35
|
|
|
|
|
36
|
|
|
$this->values = $values; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function jsonSerialize() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getAll(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function get($key) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($this->has($key)) { |
|
53
|
|
|
return $this->values[$key]; |
|
54
|
|
|
} |
|
55
|
|
|
throw new \InvalidArgumentException(sprintf('The value identified by "%s" does not exist.', $key)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function has($key) |
|
62
|
|
|
{ |
|
63
|
|
|
return array_key_exists($key, $this->getAll()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getAll() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->values; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function thumbprint($hash_algorithm) |
|
75
|
|
|
{ |
|
76
|
|
|
Assertion::inArray($hash_algorithm, hash_algos(), sprintf('Hash algorithm "%s" is not supported', $hash_algorithm)); |
|
77
|
|
|
|
|
78
|
|
|
$values = array_intersect_key($this->getAll(), array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k'])); |
|
79
|
|
|
ksort($values); |
|
80
|
|
|
$input = json_encode($values); |
|
81
|
|
|
|
|
82
|
|
|
return Base64Url::encode(hash($hash_algorithm, $input, true)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \Jose\Object\JWKInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function toPublic() |
|
89
|
|
|
{ |
|
90
|
|
|
$values = $this->getAll(); |
|
91
|
|
|
$values = array_diff_key($values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi'])); |
|
92
|
|
|
|
|
93
|
|
|
return new self($values); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|