1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ================================== |
4
|
|
|
* Responsible PHP API |
5
|
|
|
* ================================== |
6
|
|
|
* |
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
8
|
|
|
* |
9
|
|
|
* @api Responible API |
10
|
|
|
* @package responsible\core\auth |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\auth; |
16
|
|
|
|
17
|
|
|
use responsible\core\auth; |
18
|
|
|
use responsible\core\exception; |
19
|
|
|
use responsible\core\keys; |
20
|
|
|
use responsible\core\configuration; |
21
|
|
|
use responsible\core\headers\header; |
22
|
|
|
|
23
|
|
|
class jwt extends \responsible\core\auth\authorise |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* [CTY - Json Web Token content type] |
27
|
|
|
* @link https://tools.ietf.org/html/rfc7519#section-5.2 |
28
|
|
|
*/ |
29
|
|
|
const CYT = 'JWT'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* [$TIMESTAMP Set the current timestamp] |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
protected static $TIMESTAMP; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* [$LEEWAY Cater for time skew in sever time differences] |
39
|
|
|
* @var integer |
40
|
|
|
*/ |
41
|
|
|
protected static $LEEWAY = 10; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* [$EXPIRES Default token expiry] |
45
|
|
|
* 300 = 5 minutes |
46
|
|
|
* @var integer |
47
|
|
|
*/ |
48
|
|
|
protected $EXPIRES = 300; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* [$algorithyms Supported algorithms] |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected static $ALGORITHMS = [ |
55
|
|
|
'HS256','sha256', |
56
|
|
|
'HS384','sha384', |
57
|
|
|
'HS512','sha512', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* [$ALGORITHMS_ACRONYM Get the JWT acronym support] |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected static $ALGORITHMS_ACRONYM = [ |
65
|
|
|
'sha256' => ['hash' => 'sha256'], |
66
|
|
|
'sha384' => ['hash' => 'sha384'], |
67
|
|
|
'sha512' => ['hash' => 'sha512'], |
68
|
|
|
'HS256' => ['hash' => 'sha256'], |
69
|
|
|
'HS384' => ['hash' => 'sha384'], |
70
|
|
|
'HS512' => ['hash' => 'sha512'], |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* [$token] |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $token; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* [$key Client secret key] |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $key; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* [$payload Clients payload] |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $payload; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Responsible API options |
93
|
|
|
*/ |
94
|
|
|
protected static $options; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* [__construct] |
98
|
|
|
*/ |
99
|
|
|
public function __construct() |
100
|
|
|
{ |
101
|
|
|
self::$TIMESTAMP = (new \DateTime('now'))->getTimestamp(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* [encode] |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function encode() |
109
|
|
|
{ |
110
|
|
|
$encode = new auth\jwtEncoder; |
111
|
|
|
|
112
|
|
|
$encoded = |
113
|
|
|
$encode->setPayload($this->getPayload()) |
114
|
|
|
->key($this->getKey()) |
115
|
|
|
->encode() |
116
|
|
|
; |
117
|
|
|
|
118
|
|
|
return $encoded; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* [decode Decode the token] |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function decode() |
126
|
|
|
{ |
127
|
|
|
$decode = new auth\jwtDecoder; |
128
|
|
|
|
129
|
|
|
$decoded = |
130
|
|
|
$decode->token($this->getToken()) |
131
|
|
|
->key($this->getKey()) |
132
|
|
|
->decode() |
133
|
|
|
; |
134
|
|
|
|
135
|
|
|
return $decoded; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* [token Set the token] |
140
|
|
|
* |
141
|
|
|
* @param string |
142
|
|
|
* @return self |
143
|
|
|
*/ |
144
|
|
|
public function token($token = null) |
145
|
|
|
{ |
146
|
|
|
if (is_null($token) || empty($token) || !is_string($token)) { |
147
|
|
|
$this->setUnauthorised(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->token = $token; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* [key - Set the secret key] |
157
|
|
|
* |
158
|
|
|
* @param string |
159
|
|
|
* @return self |
160
|
|
|
*/ |
161
|
|
|
public function key($key = null) |
162
|
|
|
{ |
163
|
|
|
if (is_null($key) || empty($key) || !is_string($key)) { |
164
|
|
|
$this->setUnauthorised(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->key = $key; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* [setUnauthorised Render unauthorised response] |
174
|
|
|
*/ |
175
|
|
|
protected function setUnauthorised() |
176
|
|
|
{ |
177
|
|
|
$header = new header; |
178
|
|
|
$header->setOptions($this->getOptions()); |
179
|
|
|
$header->unauthorised(); |
180
|
|
|
// @codeCoverageIgnoreStart |
181
|
|
|
} |
182
|
|
|
// @codeCoverageIgnoreEnd |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* [payload Set the clients payload] |
186
|
|
|
* @param array $payload |
187
|
|
|
* @return self |
188
|
|
|
*/ |
189
|
|
|
public function setPayload($payload) |
190
|
|
|
{ |
191
|
|
|
$this->payload = $payload; |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* [getToken Get the Json Web Token] |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
protected function getToken() |
200
|
|
|
{ |
201
|
|
|
return $this->token; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* [getKey Get the client secret key] |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
protected function getKey() |
209
|
|
|
{ |
210
|
|
|
return $this->key; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* [getPayload Get the clients payload] |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
protected function getPayload() |
218
|
|
|
{ |
219
|
|
|
return $this->payload; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* [getLeeway Get the default leeway] |
224
|
|
|
* @return integer |
225
|
|
|
*/ |
226
|
|
|
public static function getLeeway() |
227
|
|
|
{ |
228
|
|
|
return self::$LEEWAY; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* [getTimestamp Get the current timestamp] |
233
|
|
|
* @return integer |
234
|
|
|
*/ |
235
|
|
|
public static function getCurrentTimestamp() |
236
|
|
|
{ |
237
|
|
|
return self::$TIMESTAMP; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* [setOptions Inherit Responsible API options] |
242
|
|
|
* @param array $options |
243
|
|
|
*/ |
244
|
|
|
public function setOptions($options) |
245
|
|
|
{ |
246
|
|
|
parent::setOptions($options); |
247
|
|
|
self::$options = $options; |
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* [getOptions] |
253
|
|
|
* @return array |
254
|
|
|
*/ |
255
|
|
|
public function getOptions():?array |
256
|
|
|
{ |
257
|
|
|
return self::$options; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* [messages Common error messages] |
262
|
|
|
* @param string $type [message type] |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
protected static function messages($type) |
266
|
|
|
{ |
267
|
|
|
$error = []; |
268
|
|
|
|
269
|
|
|
switch ($type) { |
270
|
|
|
case 'denied_token': |
271
|
|
|
$error = [ |
272
|
|
|
'error' => 'invalid token', |
273
|
|
|
'description' => 'Permission denied - invalid token' |
274
|
|
|
]; |
275
|
|
|
break; |
276
|
|
|
|
277
|
|
|
case 'denied_key': |
278
|
|
|
$error = [ |
279
|
|
|
'error' => 'invalid key', |
280
|
|
|
'description' => 'Permission denied - invalid key' |
281
|
|
|
]; |
282
|
|
|
break; |
283
|
|
|
|
284
|
|
|
case 'expired': |
285
|
|
|
$error = [ |
286
|
|
|
'error' => 'expired', |
287
|
|
|
'description' => 'Token expired' |
288
|
|
|
]; |
289
|
|
|
break; |
290
|
|
|
|
291
|
|
|
case 'not_ready': |
292
|
|
|
$error = [ |
293
|
|
|
'error' => 'not ready', |
294
|
|
|
'description' => 'The token supplied is not ready to be accessed at the moment.' |
295
|
|
|
]; |
296
|
|
|
break; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $error; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* [getAlgorithm Check if the algorithm in the header is supported by the Responsible API] |
304
|
|
|
* @param string $type [Algorithm hash] |
305
|
|
|
* @return mixed |
306
|
|
|
*/ |
307
|
|
|
public static function getAlgorithm($type = '') |
|
|
|
|
308
|
|
|
{ |
309
|
|
|
return self::resolveAlgorithm(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* [resolveAlgorithm Resolve the algorythm to use in the JWT header] |
314
|
|
|
* @return array |
315
|
|
|
*/ |
316
|
|
|
protected static function resolveAlgorithm() |
317
|
|
|
{ |
318
|
|
|
$algoKey = (self::$options['jwt']['algo']) ?? 'HS256'; |
319
|
|
|
$algoKey = (isset(self::$ALGORITHMS_ACRONYM[$algoKey])) ? $algoKey : 'HS256'; |
320
|
|
|
|
321
|
|
|
$ALGO = [ |
322
|
|
|
'header' => $algoKey, |
323
|
|
|
'hash' => self::$ALGORITHMS_ACRONYM[$algoKey]['hash'], |
324
|
|
|
]; |
325
|
|
|
|
326
|
|
|
if (array_search($algoKey, self::$ALGORITHMS) !== FALSE) { |
327
|
|
|
$ALGO = [ |
328
|
|
|
'header' => $algoKey, |
329
|
|
|
'hash' => self::$ALGORITHMS_ACRONYM[$algoKey]['hash'], |
330
|
|
|
]; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $ALGO; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.