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 default target |
71
|
|
|
* |
72
|
|
|
* @var int |
73
|
|
|
*/ |
74
|
|
|
private $issuer = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getKey(): string |
80
|
|
|
{ |
81
|
|
|
if (empty($this->key)) { |
82
|
|
|
return Craft::$app->getConfig()->getGeneral()->securityKey; |
83
|
|
|
} |
84
|
|
|
return $this->key; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array|null $selfConsumableIssuers |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function setSelfConsumableIssuers(array $selfConsumableIssuers = []) |
92
|
|
|
{ |
93
|
|
|
$this->selfConsumableIssuers = $selfConsumableIssuers; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
* @throws \craft\errors\SiteNotFoundException |
100
|
|
|
*/ |
101
|
|
|
public function getSelfConsumableIssuers(): array |
102
|
|
|
{ |
103
|
|
|
if (empty($this->selfConsumableIssuers)) { |
104
|
|
|
return [Craft::$app->getSites()->getCurrentSite()->baseUrl]; |
105
|
|
|
} |
106
|
|
|
return (array)$this->selfConsumableIssuers; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string|null $selfConsumableAudience |
113
|
|
|
* @return $this |
114
|
|
|
*/ |
115
|
|
|
public function setSelfConsumableAudience(string $selfConsumableAudience = null) |
116
|
|
|
{ |
117
|
|
|
$this->selfConsumableAudience = $selfConsumableAudience; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
* @throws \craft\errors\SiteNotFoundException |
124
|
|
|
*/ |
125
|
|
|
public function getSelfConsumableAudience(): string |
126
|
|
|
{ |
127
|
|
|
if (null === $this->selfConsumableAudience) { |
128
|
|
|
return Craft::$app->getSites()->getCurrentSite()->baseUrl; |
129
|
|
|
} |
130
|
|
|
return (string)$this->selfConsumableAudience; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string|null $issuer |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setIssuer(string $issuer = null) |
138
|
|
|
{ |
139
|
|
|
$this->issuer = $issuer; |
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
* @throws \craft\errors\SiteNotFoundException |
146
|
|
|
*/ |
147
|
|
|
public function getIssuer(): string |
148
|
|
|
{ |
149
|
|
|
if (null === $this->issuer) { |
150
|
|
|
return Craft::$app->getSites()->getCurrentSite()->baseUrl; |
151
|
|
|
} |
152
|
|
|
return (string)$this->issuer; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Creates a Signer class based on the configured algorithm |
157
|
|
|
* |
158
|
|
|
* @return Signer |
159
|
|
|
* @throws \yii\base\InvalidConfigException |
160
|
|
|
*/ |
161
|
|
|
public function getSigner(): Signer |
162
|
|
|
{ |
163
|
|
|
return $this->resolveSigner($this->algorithm); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Resolves a Signer class based on an algorithm key |
168
|
|
|
* |
169
|
|
|
* @param $key |
170
|
|
|
* @return Signer |
171
|
|
|
* @throws \yii\base\InvalidConfigException |
172
|
|
|
*/ |
173
|
|
|
public function resolveSigner($key): Signer |
174
|
|
|
{ |
175
|
|
|
if (empty($this->algorithms[$key])) { |
176
|
|
|
throw new InvalidArgumentException('Algorithm not supported'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** @var Signer $signer */ |
180
|
|
|
$signer = Craft::createObject( |
181
|
|
|
$this->algorithms[$key] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
return $signer; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
190
|
|
|
public function attributes() |
191
|
|
|
{ |
192
|
|
|
return array_merge( |
193
|
|
|
parent::attributes(), |
194
|
|
|
[ |
195
|
|
|
'key', |
196
|
|
|
'selfConsumableAudience', |
197
|
|
|
'selfConsumableIssuers', |
198
|
|
|
'issuer' |
199
|
|
|
] |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.