1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace xiaodi\JWTAuth\Traits; |
6
|
|
|
|
7
|
|
|
use Lcobucci\JWT\Signer; |
8
|
|
|
use Lcobucci\JWT\Token; |
9
|
|
|
use Lcobucci\JWT\Signer\Key; |
10
|
|
|
use xiaodi\JWTAuth\Exception\JWTException; |
11
|
|
|
use xiaodi\JWTAuth\Exception\JWTInvalidArgumentException; |
12
|
|
|
|
13
|
|
|
trait Jwt |
14
|
|
|
{ |
15
|
|
|
private $uniqidKey = 'uid'; |
16
|
|
|
private $signerKey; |
17
|
|
|
private $notBefore = 0; |
18
|
|
|
private $expiresAt = 3600; |
19
|
|
|
private $refreshTTL = 7200; |
20
|
|
|
private $signer = \Lcobucci\JWT\Signer\Hmac\Sha256::class; |
21
|
|
|
|
22
|
|
|
private $type = 'Header'; |
23
|
|
|
|
24
|
|
|
private $hasLogged = 50401; |
25
|
|
|
private $tokenAlready = 50402; |
26
|
|
|
private $relogin = 50400; |
27
|
|
|
|
28
|
|
|
private $iss; |
29
|
|
|
private $aud; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 获取 Token获取途径 |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function type() |
37
|
|
|
{ |
38
|
|
|
return $this->type; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* 设置 Token获取途径 |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function setType($type) |
47
|
|
|
{ |
48
|
|
|
return $this->type = $type; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 获取 用户表唯一标识值名 |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function getUniqidKey() |
57
|
|
|
{ |
58
|
|
|
return $this->uniqidKey; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 获取 刷新Token TTL |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function refreshTTL() |
67
|
|
|
{ |
68
|
|
|
return (int) $this->refreshTTL; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* 设置 刷新Token TTL |
73
|
|
|
* |
74
|
|
|
* @param [type] $value |
|
|
|
|
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
public function setRefreshTTL($value) |
78
|
|
|
{ |
79
|
|
|
$this->refreshTTL = (int) $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function getReloginCode() |
87
|
|
|
{ |
88
|
|
|
return (int) $this->relogin; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 获取 检测延迟 |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function notBefore() |
97
|
|
|
{ |
98
|
|
|
return (int) $this->notBefore; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 设置 检测延迟 |
103
|
|
|
* |
104
|
|
|
* @param [type] $value |
|
|
|
|
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setNotBefore($value) |
108
|
|
|
{ |
109
|
|
|
$this->notBefore = (int) $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 获取 TTl |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function ttl() |
118
|
|
|
{ |
119
|
|
|
return (int) $this->expiresAt; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 设置 TTL |
124
|
|
|
* |
125
|
|
|
* @param integer $value |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function setTTL(int $value) |
129
|
|
|
{ |
130
|
|
|
$this->expiresAt = $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function getAlreadyCode() |
138
|
|
|
{ |
139
|
|
|
return $this->tokenAlready; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function getHasLoggedCode() |
147
|
|
|
{ |
148
|
|
|
return $this->hasLogged; |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 设置有效期 |
153
|
|
|
* |
154
|
|
|
* @param [type] $value |
|
|
|
|
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function setExpiresAt($value) |
158
|
|
|
{ |
159
|
|
|
$this->expiresAt = (int) $value; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* 获取私钥. |
164
|
|
|
* |
165
|
|
|
* @return string|null |
166
|
|
|
*/ |
167
|
|
|
public function getSignerKey() |
168
|
|
|
{ |
169
|
|
|
return $this->signerKey; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* 设置私钥. |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function setSignerKey($key) |
178
|
|
|
{ |
179
|
|
|
return $this->signerKey = $key; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* 设置加密方式. |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function setSigner($signer) |
188
|
|
|
{ |
189
|
|
|
$this->signer = $signer; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* 是否注入用户对象. |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function injectUser() |
198
|
|
|
{ |
199
|
|
|
return $this->injectUser; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* 获取加密方式. |
204
|
|
|
* |
205
|
|
|
* @return Signer|Exception |
|
|
|
|
206
|
|
|
*/ |
207
|
|
|
private function getSigner() |
208
|
|
|
{ |
209
|
|
|
$signer = $this->signer; |
210
|
|
|
|
211
|
|
|
if (empty($signer)) { |
212
|
|
|
throw new JWTInvalidArgumentException('加密方式未配置.', 500); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$signer = new $signer(); |
216
|
|
|
|
217
|
|
|
if (!$signer instanceof Signer) { |
218
|
|
|
throw new JWTException('加密方式错误.', 500); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return $signer; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* 生成Key. |
226
|
|
|
* |
227
|
|
|
* @return Key |
228
|
|
|
*/ |
229
|
|
|
private function makeSignerKey() |
230
|
|
|
{ |
231
|
|
|
$key = $this->getSignerKey(); |
232
|
|
|
if (empty($key)) { |
233
|
|
|
throw new JWTException('私钥未配置.', 500); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return new Key($key); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* 获取 发布端 url |
241
|
|
|
* |
242
|
|
|
* @return void |
243
|
|
|
*/ |
244
|
|
|
public function iss() |
245
|
|
|
{ |
246
|
|
|
$iss = $this->app->request->root(true); |
247
|
|
|
|
248
|
|
|
return $this->iss ?: $iss; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* 获取 请求端 url |
253
|
|
|
* |
254
|
|
|
* @return void |
255
|
|
|
*/ |
256
|
|
|
public function aud() |
257
|
|
|
{ |
258
|
|
|
return $this->aud; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* 获取 已验证的Token对象 |
263
|
|
|
* |
264
|
|
|
* @return Token |
265
|
|
|
*/ |
266
|
|
|
public function getToken() |
267
|
|
|
{ |
268
|
|
|
return $this->token; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|