1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JWX\JWT; |
4
|
|
|
|
5
|
|
|
use JWX\JWT\Parameter\JWTParameter; |
6
|
|
|
use JWX\JWT\Parameter\RegisteredJWTParameter; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents a header used in JWS and JWE. |
11
|
|
|
*/ |
12
|
|
|
class Header implements |
13
|
|
|
\Countable, \IteratorAggregate |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Parameters. |
17
|
|
|
* |
18
|
|
|
* @var JWTParameter[] $_parameters |
19
|
|
|
*/ |
20
|
|
|
protected $_parameters; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor |
24
|
|
|
* |
25
|
|
|
* @param JWTParameter ...$params Parameters |
26
|
|
|
*/ |
27
|
131 |
|
public function __construct(JWTParameter ...$params) { |
28
|
131 |
|
$this->_parameters = array(); |
29
|
131 |
|
foreach ($params as $param) { |
30
|
62 |
|
$this->_parameters[$param->name()] = $param; |
31
|
131 |
|
} |
32
|
131 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize from an array representing a JSON object. |
36
|
|
|
* |
37
|
|
|
* @param array $members |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
45 |
|
public static function fromArray(array $members) { |
41
|
45 |
|
$params = array(); |
42
|
45 |
|
foreach ($members as $name => $value) { |
43
|
45 |
|
$params[] = JWTParameter::fromNameAndValue($name, $value); |
44
|
45 |
|
} |
45
|
45 |
|
return new self(...$params); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initialize from a JSON. |
50
|
|
|
* |
51
|
|
|
* @param string $json |
52
|
|
|
* @throws \UnexpectedValueException |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
33 |
View Code Duplication |
public static function fromJSON($json) { |
|
|
|
|
56
|
33 |
|
$members = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
57
|
33 |
|
if (!is_array($members)) { |
58
|
1 |
|
throw new \UnexpectedValueException("Invalid JSON."); |
59
|
|
|
} |
60
|
32 |
|
return self::fromArray($members); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get self with parameters added. |
65
|
|
|
* |
66
|
|
|
* @param JWTParameter ...$param |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
32 |
|
public function withParameters(JWTParameter ...$params) { |
70
|
32 |
|
$obj = clone $this; |
71
|
32 |
|
foreach ($params as $param) { |
72
|
32 |
|
$obj->_parameters[$param->name()] = $param; |
73
|
32 |
|
} |
74
|
32 |
|
return $obj; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get all parameters. |
79
|
|
|
* |
80
|
|
|
* @return JWTParameter[] |
81
|
|
|
*/ |
82
|
28 |
|
public function parameters() { |
83
|
28 |
|
return $this->_parameters; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Whether parameters are present. |
88
|
|
|
* |
89
|
|
|
* Returns false if any of the given parameters is not set. |
90
|
|
|
* |
91
|
|
|
* @param string ...$names Parameter names |
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
70 |
View Code Duplication |
public function has(...$names) { |
|
|
|
|
95
|
70 |
|
foreach ($names as $name) { |
96
|
70 |
|
if (!isset($this->_parameters[$name])) { |
97
|
41 |
|
return false; |
98
|
|
|
} |
99
|
43 |
|
} |
100
|
42 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get a parameter. |
105
|
|
|
* |
106
|
|
|
* @param string $name Parameter name |
107
|
|
|
* @throws \LogicException |
108
|
|
|
* @return JWTParameter |
109
|
|
|
*/ |
110
|
37 |
|
public function get($name) { |
111
|
37 |
|
if (!$this->has($name)) { |
112
|
1 |
|
throw new \LogicException("Parameter $name doesn't exists."); |
113
|
|
|
} |
114
|
36 |
|
return $this->_parameters[$name]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get a parameter and check that it's an instance of the expected type. |
119
|
|
|
* |
120
|
|
|
* @param string $name Parameter name |
121
|
|
|
* @throws \RuntimeException |
122
|
|
|
* @return JWTParameter |
123
|
|
|
*/ |
124
|
4 |
|
public function getTyped($name) { |
125
|
4 |
|
$param = $this->get($name); |
126
|
4 |
|
if (array_key_exists($name, RegisteredJWTParameter::MAP_NAME_TO_CLASS)) { |
127
|
4 |
|
$cls = RegisteredJWTParameter::MAP_NAME_TO_CLASS[$name]; |
128
|
4 |
|
if (!$param instanceof $cls) { |
129
|
|
|
throw new \RuntimeException( |
130
|
|
|
"$cls expected, got " . get_class($param)); |
131
|
|
|
} |
132
|
4 |
|
} |
133
|
4 |
|
return $param; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Convert to a JSON. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
70 |
View Code Duplication |
public function toJSON() { |
|
|
|
|
142
|
70 |
|
if (empty($this->_parameters)) { |
143
|
1 |
|
return ""; |
144
|
|
|
} |
145
|
69 |
|
$data = array(); |
146
|
69 |
|
foreach ($this->_parameters as $param) { |
147
|
69 |
|
$data[$param->name()] = $param->value(); |
148
|
69 |
|
} |
149
|
69 |
|
return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the number of parameters. |
154
|
|
|
* |
155
|
|
|
* @see Countable::count() |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
3 |
|
public function count() { |
159
|
3 |
|
return count($this->_parameters); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get iterator for the parameters. |
164
|
|
|
* |
165
|
|
|
* @see IteratorAggregate::getIterator() |
166
|
|
|
* @return \ArrayIterator |
167
|
|
|
*/ |
168
|
1 |
|
public function getIterator() { |
169
|
1 |
|
return new \ArrayIterator($this->_parameters); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|