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 Lcobucci\JWT\Signer; |
14
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
15
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha384; |
16
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha512; |
17
|
|
|
use yii\base\InvalidArgumentException; |
18
|
|
|
use yii\base\Model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
class Settings extends Model |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Supported algorithms |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $algorithms = [ |
32
|
|
|
'HS256' => Sha256::class, |
33
|
|
|
'HS384' => Sha384::class, |
34
|
|
|
'HS512' => Sha512::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The selected algorithm |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $algorithm = 'HS512'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The key used for token signature |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $key; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The default audience |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $selfConsumableAudience = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The available audiences |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private $selfConsumableIssuers = []; |
64
|
|
|
|
65
|
|
|
/** The entity issuing the token |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $issuer = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The self consumable token duration. Defaults to GeneralConfig::$userSessionDuration |
73
|
|
|
* |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
private $selfConsumableTokenDuration; |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/******************************************* |
80
|
|
|
* KEY |
81
|
|
|
*******************************************/ |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getKey(): string |
87
|
|
|
{ |
88
|
|
|
if (empty($this->key)) { |
89
|
|
|
return Craft::$app->getConfig()->getGeneral()->securityKey; |
90
|
|
|
} |
91
|
|
|
return $this->key; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/******************************************* |
96
|
|
|
* ISSUER |
97
|
|
|
*******************************************/ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string|null $issuer |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setIssuer(string $issuer = null) |
104
|
|
|
{ |
105
|
|
|
$this->issuer = $issuer; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
* @throws \craft\errors\SiteNotFoundException |
112
|
|
|
*/ |
113
|
|
|
public function getIssuer(): string |
114
|
|
|
{ |
115
|
|
|
if (null === $this->issuer) { |
116
|
|
|
return Craft::$app->getSites()->getCurrentSite()->baseUrl; |
117
|
|
|
} |
118
|
|
|
return (string)$this->issuer; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/******************************************* |
123
|
|
|
* SIGNER |
124
|
|
|
*******************************************/ |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Creates a Signer class based on the configured algorithm |
128
|
|
|
* |
129
|
|
|
* @return Signer |
130
|
|
|
* @throws \yii\base\InvalidConfigException |
131
|
|
|
*/ |
132
|
|
|
public function getSigner(): Signer |
133
|
|
|
{ |
134
|
|
|
return $this->resolveSigner($this->algorithm); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Resolves a Signer class based on an algorithm key |
139
|
|
|
* |
140
|
|
|
* @param $key |
141
|
|
|
* @return Signer |
142
|
|
|
* @throws \yii\base\InvalidConfigException |
143
|
|
|
*/ |
144
|
|
|
public function resolveSigner($key): Signer |
145
|
|
|
{ |
146
|
|
|
if (empty($this->algorithms[$key])) { |
147
|
|
|
throw new InvalidArgumentException('Algorithm not supported'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** @var Signer $signer */ |
151
|
|
|
$signer = Craft::createObject( |
152
|
|
|
$this->algorithms[$key] |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
return $signer; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/******************************************* |
160
|
|
|
* SELF CONSUMABLE TOKEN DURATION |
161
|
|
|
*******************************************/ |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return int |
165
|
|
|
* @throws \yii\base\InvalidConfigException |
166
|
|
|
*/ |
167
|
|
|
public function getSelfConsumableTokenDuration(): int |
168
|
|
|
{ |
169
|
|
|
if ($this->selfConsumableTokenDuration === null) { |
170
|
|
|
$this->selfConsumableTokenDuration = Craft::$app->getConfig()->getGeneral()->userSessionDuration; |
171
|
|
|
}; |
172
|
|
|
|
173
|
|
|
return ConfigHelper::durationInSeconds($this->selfConsumableTokenDuration); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/******************************************* |
177
|
|
|
* SELF CONSUMABLE ISSUER |
178
|
|
|
*******************************************/ |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param array|null $selfConsumableIssuers |
182
|
|
|
* @return $this |
183
|
|
|
*/ |
184
|
|
|
public function setSelfConsumableIssuers(array $selfConsumableIssuers = []) |
185
|
|
|
{ |
186
|
|
|
$this->selfConsumableIssuers = $selfConsumableIssuers; |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
* @throws \craft\errors\SiteNotFoundException |
193
|
|
|
*/ |
194
|
|
|
public function getSelfConsumableIssuers(): array |
195
|
|
|
{ |
196
|
|
|
if (empty($this->selfConsumableIssuers)) { |
197
|
|
|
return [Craft::$app->getSites()->getCurrentSite()->baseUrl]; |
198
|
|
|
} |
199
|
|
|
return (array)$this->selfConsumableIssuers; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/******************************************* |
204
|
|
|
* SELF CONSUMABLE AUDIENCE |
205
|
|
|
*******************************************/ |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string|null $selfConsumableAudience |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function setSelfConsumableAudience(string $selfConsumableAudience = null) |
212
|
|
|
{ |
213
|
|
|
$this->selfConsumableAudience = $selfConsumableAudience; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
* @throws \craft\errors\SiteNotFoundException |
220
|
|
|
*/ |
221
|
|
|
public function getSelfConsumableAudience(): string |
222
|
|
|
{ |
223
|
|
|
if (null === $this->selfConsumableAudience) { |
224
|
|
|
return Craft::$app->getSites()->getCurrentSite()->baseUrl; |
225
|
|
|
} |
226
|
|
|
return (string)$this->selfConsumableAudience; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param int $duration |
231
|
|
|
* @return $this |
232
|
|
|
* |
233
|
|
|
* @deprecated |
234
|
|
|
*/ |
235
|
|
|
public function setTokenExpiration(int $duration) |
236
|
|
|
{ |
237
|
|
|
$this->selfConsumableTokenDuration = $duration; |
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/******************************************* |
242
|
|
|
* ATTRIBUTES |
243
|
|
|
*******************************************/ |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritdoc |
247
|
|
|
*/ |
248
|
|
|
public function attributes() |
249
|
|
|
{ |
250
|
|
|
return array_merge( |
251
|
|
|
parent::attributes(), |
252
|
|
|
[ |
253
|
|
|
'key', |
254
|
|
|
'selfConsumableTokenExpiration', |
255
|
|
|
'selfConsumableAudience', |
256
|
|
|
'selfConsumableIssuers', |
257
|
|
|
'issuer' |
258
|
|
|
] |
259
|
|
|
); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|