|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License (MIT) |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2014-2015 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
|
|
|
/** |
|
15
|
|
|
* Class JWT. |
|
16
|
|
|
*/ |
|
17
|
|
|
class JWT implements JWTInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $encoded_protected_header = ''; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $input = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $encoded_payload = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $protected_headers = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $unprotected_headers = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var null |
|
46
|
|
|
*/ |
|
47
|
|
|
private $payload = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getEncodedProtectedHeaders() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->encoded_protected_header; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getInput() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->input; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getEncodedPayload() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->encoded_payload; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getProtectedHeaders() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->protected_headers; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getUnprotectedHeaders() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->unprotected_headers; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPayload() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->payload; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
public function withInput($input) |
|
101
|
|
|
{ |
|
102
|
|
|
$jwt = clone $this; |
|
103
|
|
|
$jwt->input = $input; |
|
104
|
|
|
|
|
105
|
|
|
return $jwt; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function withEncodedProtectedHeaders($encoded_protected_header) |
|
112
|
|
|
{ |
|
113
|
|
|
$jwt = clone $this; |
|
114
|
|
|
$jwt->encoded_protected_header = $encoded_protected_header; |
|
115
|
|
|
|
|
116
|
|
|
return $jwt; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function withEncodedPayload($encoded_payload) |
|
123
|
|
|
{ |
|
124
|
|
|
$jwt = clone $this; |
|
125
|
|
|
$jwt->encoded_payload = $encoded_payload; |
|
126
|
|
|
|
|
127
|
|
|
return $jwt; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function withProtectedHeaders(array $values) |
|
134
|
|
|
{ |
|
135
|
|
|
$jwt = clone $this; |
|
136
|
|
|
$jwt->protected_headers = $values; |
|
137
|
|
|
|
|
138
|
|
|
return $jwt; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function withUnprotectedHeaders(array $values) |
|
145
|
|
|
{ |
|
146
|
|
|
$jwt = clone $this; |
|
147
|
|
|
$jwt->unprotected_headers = $values; |
|
148
|
|
|
|
|
149
|
|
|
return $jwt; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritdoc} |
|
154
|
|
|
*/ |
|
155
|
|
|
public function withProtectedHeader($key, $value) |
|
156
|
|
|
{ |
|
157
|
|
|
$jwt = clone $this; |
|
158
|
|
|
$jwt->protected_headers[$key] = $value; |
|
159
|
|
|
|
|
160
|
|
|
return $jwt; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
|
|
public function withUnprotectedHeader($key, $value) |
|
167
|
|
|
{ |
|
168
|
|
|
$jwt = clone $this; |
|
169
|
|
|
$jwt->unprotected_headers[$key] = $value; |
|
170
|
|
|
|
|
171
|
|
|
return $jwt; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritdoc} |
|
176
|
|
|
*/ |
|
177
|
|
|
public function withPayload($payload) |
|
178
|
|
|
{ |
|
179
|
|
|
$jwt = clone $this; |
|
180
|
|
|
$jwt->payload = $payload; |
|
181
|
|
|
|
|
182
|
|
|
return $jwt; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getProtectedHeader($key) |
|
189
|
|
|
{ |
|
190
|
|
|
if ($this->hasProtectedHeader($key)) { |
|
191
|
|
|
return $this->protected_headers[$key]; |
|
192
|
|
|
} |
|
193
|
|
|
throw new \InvalidArgumentException(sprintf('The protected header "%" does not exist', $key)); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
|
|
public function hasProtectedHeader($key) |
|
200
|
|
|
{ |
|
201
|
|
|
return array_key_exists($key, $this->protected_headers); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* {@inheritdoc} |
|
206
|
|
|
*/ |
|
207
|
|
|
public function withoutProtectedHeader($key) |
|
208
|
|
|
{ |
|
209
|
|
|
if (!$this->hasProtectedHeader($key)) { |
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
$jwt = clone $this; |
|
213
|
|
|
unset($jwt->protected_headers[$key]); |
|
214
|
|
|
|
|
215
|
|
|
return $jwt; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* {@inheritdoc} |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getUnprotectedHeader($key) |
|
222
|
|
|
{ |
|
223
|
|
|
if ($this->hasUnprotectedHeader($key)) { |
|
224
|
|
|
return $this->unprotected_headers[$key]; |
|
225
|
|
|
} |
|
226
|
|
|
throw new \InvalidArgumentException(sprintf('The unprotected header "%" does not exist', $key)); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* {@inheritdoc} |
|
231
|
|
|
*/ |
|
232
|
|
|
public function hasUnprotectedHeader($key) |
|
233
|
|
|
{ |
|
234
|
|
|
return array_key_exists($key, $this->unprotected_headers); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* {@inheritdoc} |
|
239
|
|
|
*/ |
|
240
|
|
|
public function withoutUnprotectedHeader($key) |
|
241
|
|
|
{ |
|
242
|
|
|
if (!$this->hasUnprotectedHeader($key)) { |
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
$jwt = clone $this; |
|
246
|
|
|
unset($jwt->unprotected_headers[$key]); |
|
247
|
|
|
|
|
248
|
|
|
return $jwt; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* {@inheritdoc} |
|
253
|
|
|
*/ |
|
254
|
|
|
public function getHeaders() |
|
255
|
|
|
{ |
|
256
|
|
|
return array_merge($this->protected_headers, $this->unprotected_headers); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* {@inheritdoc} |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getHeader($key) |
|
263
|
|
|
{ |
|
264
|
|
|
if ($this->hasProtectedHeader($key)) { |
|
265
|
|
|
return $this->getProtectedHeader($key); |
|
266
|
|
|
} elseif ($this->hasUnprotectedHeader($key)) { |
|
267
|
|
|
return $this->getUnprotectedHeader($key); |
|
268
|
|
|
} |
|
269
|
|
|
throw new \InvalidArgumentException(sprintf('The protected or unprotected headers do not contain header "%"', $key)); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* {@inheritdoc} |
|
274
|
|
|
*/ |
|
275
|
|
|
public function hasHeader($key) |
|
276
|
|
|
{ |
|
277
|
|
|
return $this->hasProtectedHeader($key) || $this->hasUnprotectedHeader($key); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* {@inheritdoc} |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getHeaderOrClaim($key) |
|
284
|
|
|
{ |
|
285
|
|
|
if ($this->hasHeader($key)) { |
|
286
|
|
|
return $this->getHeader($key); |
|
287
|
|
|
} elseif ($this->hasClaim($key)) { |
|
288
|
|
|
return $this->getClaim($key); |
|
289
|
|
|
} |
|
290
|
|
|
throw new \InvalidArgumentException(sprintf('The header or claim do not contain value with key "%"', $key)); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* {@inheritdoc} |
|
295
|
|
|
*/ |
|
296
|
|
|
public function hasHeaderOrClaim($key) |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->hasHeader($key) || $this->hasClaim($key); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* {@inheritdoc} |
|
303
|
|
|
*/ |
|
304
|
|
|
public function getClaim($key) |
|
305
|
|
|
{ |
|
306
|
|
|
if ($this->hasClaim($key)) { |
|
307
|
|
|
return $this->payload[$key]; |
|
308
|
|
|
} |
|
309
|
|
|
throw new \InvalidArgumentException(sprintf('The payload does not contain claim "%"', $key)); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* {@inheritdoc} |
|
314
|
|
|
*/ |
|
315
|
|
|
public function getClaims() |
|
316
|
|
|
{ |
|
317
|
|
|
if (is_array($this->payload)) { |
|
318
|
|
|
return $this->payload; |
|
319
|
|
|
} |
|
320
|
|
|
throw new \InvalidArgumentException('The payload does not contain claims'); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* {@inheritdoc} |
|
325
|
|
|
*/ |
|
326
|
|
|
public function hasClaim($key) |
|
327
|
|
|
{ |
|
328
|
|
|
return $this->hasClaims() && array_key_exists($key, $this->payload); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* {@inheritdoc} |
|
333
|
|
|
*/ |
|
334
|
|
|
public function hasClaims() |
|
335
|
|
|
{ |
|
336
|
|
|
return is_array($this->payload); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* {@inheritdoc} |
|
341
|
|
|
*/ |
|
342
|
|
|
public function withClaim($key, $value) |
|
343
|
|
|
{ |
|
344
|
|
|
$jwt = clone $this; |
|
345
|
|
|
if (!is_array($jwt->payload)) { |
|
346
|
|
|
$jwt->payload = []; |
|
|
|
|
|
|
347
|
|
|
} |
|
348
|
|
|
$jwt->payload[$key] = $value; |
|
349
|
|
|
|
|
350
|
|
|
return $jwt; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* {@inheritdoc} |
|
355
|
|
|
*/ |
|
356
|
|
|
public function withoutClaim($key) |
|
357
|
|
|
{ |
|
358
|
|
|
if (!$this->hasClaim($key)) { |
|
359
|
|
|
return $this; |
|
360
|
|
|
} |
|
361
|
|
|
$jwt = clone $this; |
|
362
|
|
|
unset($jwt->payload[$key]); |
|
363
|
|
|
|
|
364
|
|
|
return $jwt; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* {@inheritdoc} |
|
369
|
|
|
*/ |
|
370
|
|
|
public function withClaims(array $claims) |
|
371
|
|
|
{ |
|
372
|
|
|
$jwt = clone $this; |
|
373
|
|
|
$jwt->payload = $claims; |
|
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
return $jwt; |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..