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