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 Base64Url\Base64Url; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class JWK. |
18
|
|
|
*/ |
19
|
|
|
final class JWK implements JWKInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $values = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* JWK constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $values |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $values = []) |
32
|
|
|
{ |
33
|
|
|
if (!array_key_exists('kty', $values)) { |
34
|
|
|
throw new \InvalidArgumentException('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
|
|
|
if (false === in_array($hash_algorithm, hash_algos())) { |
77
|
|
|
throw new \InvalidArgumentException(sprintf('Hash algorithm "%s" is not supported', $hash_algorithm)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$values = array_intersect_key($this->getAll(), array_flip(['kty', 'n', 'e', 'crv', 'x', 'y', 'k'])); |
81
|
|
|
ksort($values); |
82
|
|
|
$input = json_encode($values); |
83
|
|
|
|
84
|
|
|
return Base64Url::encode(hash($hash_algorithm, $input, true)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \Jose\Object\JWKInterface |
89
|
|
|
*/ |
90
|
|
|
public function toPublic() |
91
|
|
|
{ |
92
|
|
|
$values = $this->getAll(); |
93
|
|
|
$values = array_diff_key($values, array_flip(['p', 'd', 'q', 'dp', 'dq', 'qi'])); |
94
|
|
|
|
95
|
|
|
return new self($values); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|