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; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class JWT. |
16
|
|
|
*/ |
17
|
|
|
class JWT implements JWTInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $encoded_protected_header = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $input = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $encoded_payload = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $protected_headers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $unprotected_headers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null |
46
|
|
|
*/ |
47
|
|
|
protected $payload = null; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function getEncodedProtectedHeader() |
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
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getProtectedHeader() |
77
|
|
|
{ |
78
|
|
|
return $this->protected_headers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getUnprotectedHeader() |
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 withEncodedProtectedHeader($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 withProtectedHeader(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 withUnprotectedHeader(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 withProtectedHeaderValue($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 withUnprotectedHeaderValue($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 getProtectedHeaderValue($key) |
189
|
|
|
{ |
190
|
|
|
$protected_header = $this->getProtectedHeader(); |
191
|
|
|
if (array_key_exists($key, $protected_header)) { |
192
|
|
|
return $protected_header[$key]; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function getUnprotectedHeaderValue($key) |
200
|
|
|
{ |
201
|
|
|
$unprotected_header = $this->getUnprotectedHeader(); |
202
|
|
|
if (array_key_exists($key, $unprotected_header)) { |
203
|
|
|
return $unprotected_header[$key]; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function getHeaderValue($key) |
211
|
|
|
{ |
212
|
|
|
if (null !== ($value = $this->getProtectedHeaderValue($key))) { |
213
|
|
|
return $value; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this->getUnprotectedHeaderValue($key); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritdoc} |
221
|
|
|
*/ |
222
|
|
|
public function getHeaderOrPayloadValue($key) |
223
|
|
|
{ |
224
|
|
|
return $this->getHeaderValue($key) ? $this->getHeaderValue($key) : $this->getPayloadValue($key); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function getPayloadValue($key) |
231
|
|
|
{ |
232
|
|
|
$payload = $this->getPayload(); |
233
|
|
|
|
234
|
|
|
return is_array($payload) && array_key_exists($key, $payload) ? $payload[$key] : null; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function getType() |
241
|
|
|
{ |
242
|
|
|
return $this->getHeaderValue('jty'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function getContentType() |
249
|
|
|
{ |
250
|
|
|
return $this->getHeaderValue('cty'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
public function getIssuer() |
257
|
|
|
{ |
258
|
|
|
return $this->getHeaderOrPayloadValue('iss'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* {@inheritdoc} |
263
|
|
|
*/ |
264
|
|
|
public function getSubject() |
265
|
|
|
{ |
266
|
|
|
return $this->getHeaderOrPayloadValue('sub'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* {@inheritdoc} |
271
|
|
|
*/ |
272
|
|
|
public function getAudience() |
273
|
|
|
{ |
274
|
|
|
return $this->getHeaderOrPayloadValue('aud'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* {@inheritdoc} |
279
|
|
|
*/ |
280
|
|
|
public function getExpirationTime() |
281
|
|
|
{ |
282
|
|
|
return $this->getPayloadValue('exp'); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
|
public function getNotBefore() |
289
|
|
|
{ |
290
|
|
|
return $this->getPayloadValue('nbf'); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
|
|
*/ |
296
|
|
|
public function getIssuedAt() |
297
|
|
|
{ |
298
|
|
|
return $this->getPayloadValue('iat'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function getJWTID() |
305
|
|
|
{ |
306
|
|
|
return $this->getPayloadValue('jti'); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
public function getAlgorithm() |
313
|
|
|
{ |
314
|
|
|
return $this->getHeaderValue('alg'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* {@inheritdoc} |
319
|
|
|
*/ |
320
|
|
|
public function getKeyID() |
321
|
|
|
{ |
322
|
|
|
return $this->getHeaderValue('kid'); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* {@inheritdoc} |
327
|
|
|
*/ |
328
|
|
|
public function getJWKUrl() |
329
|
|
|
{ |
330
|
|
|
return $this->getHeaderValue('jku'); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* {@inheritdoc} |
335
|
|
|
*/ |
336
|
|
|
public function getJWK() |
337
|
|
|
{ |
338
|
|
|
return $this->getHeaderValue('jwk'); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritdoc} |
343
|
|
|
*/ |
344
|
|
|
public function getX509Url() |
345
|
|
|
{ |
346
|
|
|
return $this->getHeaderValue('x5u'); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* {@inheritdoc} |
351
|
|
|
*/ |
352
|
|
|
public function getX509CertificateChain() |
353
|
|
|
{ |
354
|
|
|
return $this->getHeaderValue('x5c'); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritdoc} |
359
|
|
|
*/ |
360
|
|
|
public function getX509CertificateSha1Thumbprint() |
361
|
|
|
{ |
362
|
|
|
return $this->getHeaderValue('x5t'); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritdoc} |
367
|
|
|
*/ |
368
|
|
|
public function getX509CertificateSha256Thumbprint() |
369
|
|
|
{ |
370
|
|
|
return $this->getHeaderValue('x5t#256'); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* {@inheritdoc} |
375
|
|
|
*/ |
376
|
|
|
public function getCritical() |
377
|
|
|
{ |
378
|
|
|
return $this->getProtectedHeaderValue('crit'); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|