|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/jwt/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/jwt/organization/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\jwt\models; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\helpers\ConfigHelper; |
|
13
|
|
|
use craft\helpers\UrlHelper; |
|
14
|
|
|
use Lcobucci\JWT\Signer; |
|
15
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
|
16
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha384; |
|
17
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha512; |
|
18
|
|
|
use yii\base\InvalidArgumentException; |
|
19
|
|
|
use yii\base\Model; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Flipbox Factory <[email protected]> |
|
23
|
|
|
* @since 1.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Settings extends Model |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Supported algorithms |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
public $algorithms = [ |
|
33
|
|
|
'HS256' => Sha256::class, |
|
34
|
|
|
'HS384' => Sha384::class, |
|
35
|
|
|
'HS512' => Sha512::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The selected algorithm |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $algorithm = 'HS512'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The key used for token signature |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $key; |
|
51
|
|
|
|
|
52
|
|
|
/** The entity issuing the token |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $issuer = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The default audience |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $identityAudience = null; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The available audiences |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
private $identityIssuers = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The identity token duration. Defaults to GeneralConfig::$userSessionDuration |
|
74
|
|
|
* |
|
75
|
|
|
* @var int |
|
76
|
|
|
*/ |
|
77
|
|
|
private $identityTokenDuration; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The default identity token duration. Used if a session duration is set to anything less than 0. |
|
81
|
|
|
* |
|
82
|
|
|
* @var int |
|
83
|
|
|
*/ |
|
84
|
|
|
public $defaultTokenDuration = 60 * 60 * 24; |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* The default audience |
|
89
|
|
|
* |
|
90
|
|
|
* @var string |
|
91
|
|
|
*/ |
|
92
|
|
|
private $routeAudience = null; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* The available audiences |
|
96
|
|
|
* |
|
97
|
|
|
* @var array |
|
98
|
|
|
*/ |
|
99
|
|
|
private $routeIssuers = []; |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* The self consumable token duration. Defaults to GeneralConfig::$userSessionDuration |
|
103
|
|
|
* |
|
104
|
|
|
* @var int |
|
105
|
|
|
*/ |
|
106
|
|
|
private $routeTokenDuration; |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/******************************************* |
|
110
|
|
|
* KEY |
|
111
|
|
|
*******************************************/ |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getKey(): string |
|
117
|
|
|
{ |
|
118
|
|
|
if (empty($this->key)) { |
|
119
|
|
|
return Craft::$app->getConfig()->getGeneral()->securityKey; |
|
120
|
|
|
} |
|
121
|
|
|
return $this->key; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
/******************************************* |
|
126
|
|
|
* ISSUER |
|
127
|
|
|
*******************************************/ |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string|null $issuer |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setIssuer(string $issuer = null) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->issuer = $issuer; |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getIssuer(): string |
|
144
|
|
|
{ |
|
145
|
|
|
if (null === $this->issuer) { |
|
146
|
|
|
return $this->getDefaultUrl(); |
|
147
|
|
|
} |
|
148
|
|
|
return (string)$this->issuer; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/******************************************* |
|
153
|
|
|
* SIGNER |
|
154
|
|
|
*******************************************/ |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Creates a Signer class based on the configured algorithm |
|
158
|
|
|
* |
|
159
|
|
|
* @return Signer |
|
160
|
|
|
* @throws \yii\base\InvalidConfigException |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getSigner(): Signer |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->resolveSigner($this->algorithm); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Resolves a Signer class based on an algorithm key |
|
169
|
|
|
* |
|
170
|
|
|
* @param $key |
|
171
|
|
|
* @return Signer |
|
172
|
|
|
* @throws \yii\base\InvalidConfigException |
|
173
|
|
|
*/ |
|
174
|
|
|
public function resolveSigner($key): Signer |
|
175
|
|
|
{ |
|
176
|
|
|
if (empty($this->algorithms[$key])) { |
|
177
|
|
|
throw new InvalidArgumentException('Algorithm not supported'); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** @var Signer $signer */ |
|
181
|
|
|
$signer = Craft::createObject( |
|
182
|
|
|
$this->algorithms[$key] |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
return $signer; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/******************************************* |
|
190
|
|
|
* IDENTITY |
|
191
|
|
|
*******************************************/ |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param $duration |
|
195
|
|
|
* @return $this |
|
196
|
|
|
*/ |
|
197
|
|
|
public function setIdentityTokenDuration($duration) |
|
198
|
|
|
{ |
|
199
|
|
|
$this->identityTokenDuration = $duration; |
|
200
|
|
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return int |
|
205
|
|
|
* @throws \yii\base\InvalidConfigException |
|
206
|
|
|
* |
|
207
|
|
|
* @deprecated |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getSelfConsumableTokenDuration(): int |
|
210
|
|
|
{ |
|
211
|
|
|
Craft::$app->getDeprecator()->log( |
|
212
|
|
|
self::class . '::getSelfConsumableTokenDuration', |
|
213
|
|
|
self::class . '::getSelfConsumableTokenDuration() has been deprecated. ' . |
|
214
|
|
|
'Use getIdentityTokenDuration() instead.' |
|
215
|
|
|
); |
|
216
|
|
|
return $this->getIdentityTokenDuration(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return int |
|
221
|
|
|
* @throws \yii\base\InvalidConfigException |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getIdentityTokenDuration(): int |
|
224
|
|
|
{ |
|
225
|
|
|
if ($this->identityTokenDuration === null) { |
|
226
|
|
|
$this->identityTokenDuration = Craft::$app->getConfig()->getGeneral()->userSessionDuration; |
|
227
|
|
|
}; |
|
228
|
|
|
|
|
229
|
|
|
if ($this->identityTokenDuration <= 0) { |
|
230
|
|
|
$this->identityTokenDuration = $this->defaultTokenDuration; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return ConfigHelper::durationInSeconds($this->identityTokenDuration); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param array|null $issuers |
|
239
|
|
|
* @return $this |
|
240
|
|
|
* |
|
241
|
|
|
* @deprecated |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setSelfConsumableIssuers(array $issuers = []) |
|
244
|
|
|
{ |
|
245
|
|
|
Craft::$app->getDeprecator()->log( |
|
246
|
|
|
self::class . '::setSelfConsumableIssuers', |
|
247
|
|
|
self::class . '::setSelfConsumableIssuers() has been deprecated. Use setIdentityIssuers() instead.' |
|
248
|
|
|
); |
|
249
|
|
|
return $this->setIdentityIssuers($issuers); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param array|null $issuers |
|
254
|
|
|
* @return $this |
|
255
|
|
|
*/ |
|
256
|
|
|
public function setIdentityIssuers(array $issuers = []) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->identityIssuers = $issuers; |
|
259
|
|
|
return $this; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return array |
|
264
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
265
|
|
|
* |
|
266
|
|
|
* @deprecated |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getSelfConsumableIssuers(): array |
|
269
|
|
|
{ |
|
270
|
|
|
Craft::$app->getDeprecator()->log( |
|
271
|
|
|
self::class . '::getSelfConsumableIssuers', |
|
272
|
|
|
self::class . '::getSelfConsumableIssuers() has been deprecated. Use getIdentityIssuers() instead.' |
|
273
|
|
|
); |
|
274
|
|
|
return $this->getIdentityIssuers(); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return array |
|
279
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getIdentityIssuers(): array |
|
282
|
|
|
{ |
|
283
|
|
|
if (empty($this->identityIssuers)) { |
|
284
|
|
|
return [$this->getDefaultUrl()]; |
|
285
|
|
|
} |
|
286
|
|
|
return (array)$this->identityIssuers; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param string|null $audience |
|
292
|
|
|
* @return $this |
|
293
|
|
|
* |
|
294
|
|
|
* @deprecated |
|
295
|
|
|
*/ |
|
296
|
|
|
public function setSelfConsumableAudience(string $audience = null) |
|
297
|
|
|
{ |
|
298
|
|
|
Craft::$app->getDeprecator()->log( |
|
299
|
|
|
self::class . '::setSelfConsumableAudience', |
|
300
|
|
|
self::class . '::setSelfConsumableAudience() has been deprecated. Use setIdentityAudience() instead.' |
|
301
|
|
|
); |
|
302
|
|
|
return $this->setIdentityAudience($audience); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @param string|null $audience |
|
307
|
|
|
* @return $this |
|
308
|
|
|
*/ |
|
309
|
|
|
public function setIdentityAudience(string $audience = null) |
|
310
|
|
|
{ |
|
311
|
|
|
$this->identityAudience = $audience; |
|
312
|
|
|
return $this; |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @return string |
|
317
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
318
|
|
|
* |
|
319
|
|
|
* @deprecated |
|
320
|
|
|
*/ |
|
321
|
|
|
public function getSelfConsumableAudience(): string |
|
322
|
|
|
{ |
|
323
|
|
|
Craft::$app->getDeprecator()->log( |
|
324
|
|
|
self::class . '::getSelfConsumableAudience', |
|
325
|
|
|
self::class . '::getSelfConsumableAudience() has been deprecated. Use getIdentityAudience() instead.' |
|
326
|
|
|
); |
|
327
|
|
|
return $this->getIdentityAudience(); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @return string |
|
332
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
333
|
|
|
*/ |
|
334
|
|
|
public function getIdentityAudience(): string |
|
335
|
|
|
{ |
|
336
|
|
|
if (null === $this->identityAudience) { |
|
337
|
|
|
return $this->getDefaultUrl(); |
|
338
|
|
|
} |
|
339
|
|
|
return (string)$this->identityAudience; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
|
|
343
|
|
|
/******************************************* |
|
344
|
|
|
* ROUTE |
|
345
|
|
|
*******************************************/ |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param $duration |
|
349
|
|
|
* @return $this |
|
350
|
|
|
*/ |
|
351
|
|
|
public function setRouteTokenDuration($duration) |
|
352
|
|
|
{ |
|
353
|
|
|
$this->routeTokenDuration = $duration; |
|
354
|
|
|
return $this; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @return int |
|
359
|
|
|
* @throws \yii\base\InvalidConfigException |
|
360
|
|
|
*/ |
|
361
|
|
|
public function getRouteTokenDuration(): int |
|
362
|
|
|
{ |
|
363
|
|
|
if ($this->routeTokenDuration === null) { |
|
364
|
|
|
$this->routeTokenDuration = $this->defaultTokenDuration; |
|
365
|
|
|
}; |
|
366
|
|
|
|
|
367
|
|
|
return ConfigHelper::durationInSeconds($this->routeTokenDuration); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param string|null $audience |
|
372
|
|
|
* @return $this |
|
373
|
|
|
*/ |
|
374
|
|
|
public function setRouteAudience(string $audience = null) |
|
375
|
|
|
{ |
|
376
|
|
|
$this->routeAudience = $audience; |
|
377
|
|
|
return $this; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* @return string |
|
382
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
383
|
|
|
*/ |
|
384
|
|
|
public function getRouteAudience(): string |
|
385
|
|
|
{ |
|
386
|
|
|
if (null === $this->routeAudience) { |
|
387
|
|
|
return $this->getDefaultUrl(); |
|
388
|
|
|
} |
|
389
|
|
|
return (string)$this->routeAudience; |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* @param array|null $issuers |
|
394
|
|
|
* @return $this |
|
395
|
|
|
*/ |
|
396
|
|
|
public function setRouteIssuers(array $issuers = []) |
|
397
|
|
|
{ |
|
398
|
|
|
$this->routeIssuers = $issuers; |
|
399
|
|
|
return $this; |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
/** |
|
403
|
|
|
* @return array |
|
404
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
405
|
|
|
*/ |
|
406
|
|
|
public function getRouteIssuers(): array |
|
407
|
|
|
{ |
|
408
|
|
|
if (empty($this->routeIssuers)) { |
|
409
|
|
|
return [$this->getDefaultUrl()]; |
|
410
|
|
|
} |
|
411
|
|
|
return (array)$this->routeIssuers; |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
|
|
415
|
|
|
/******************************************* |
|
416
|
|
|
* SELF CONSUMABLE AUDIENCE |
|
417
|
|
|
*******************************************/ |
|
418
|
|
|
/** |
|
419
|
|
|
* @param int $duration |
|
420
|
|
|
* @return $this |
|
421
|
|
|
* |
|
422
|
|
|
* @deprecated |
|
423
|
|
|
*/ |
|
424
|
|
|
public function setTokenExpiration(int $duration) |
|
425
|
|
|
{ |
|
426
|
|
|
$this->identityTokenDuration = $duration; |
|
427
|
|
|
return $this; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/******************************************* |
|
431
|
|
|
* ATTRIBUTES |
|
432
|
|
|
*******************************************/ |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* @inheritdoc |
|
436
|
|
|
*/ |
|
437
|
|
|
public function attributes() |
|
438
|
|
|
{ |
|
439
|
|
|
return array_merge( |
|
440
|
|
|
parent::attributes(), |
|
441
|
|
|
[ |
|
442
|
|
|
'key', |
|
443
|
|
|
'identityTokenExpiration', |
|
444
|
|
|
'identityAudience', |
|
445
|
|
|
'identityIssuers', |
|
446
|
|
|
'routeTokenExpiration', |
|
447
|
|
|
'routeAudience', |
|
448
|
|
|
'routeIssuers', |
|
449
|
|
|
'issuer' |
|
450
|
|
|
] |
|
451
|
|
|
); |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* @return string |
|
456
|
|
|
* @throws \craft\errors\SiteNotFoundException |
|
457
|
|
|
*/ |
|
458
|
|
|
private function getDefaultUrl(): string |
|
459
|
|
|
{ |
|
460
|
|
|
return Craft::$app->getSites()->getCurrentSite()->getBaseUrl() ?? UrlHelper::baseRequestUrl(); |
|
461
|
|
|
} |
|
462
|
|
|
} |
|
463
|
|
|
|