1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace xiaodi\JWTAuth\Traits; |
||
6 | |||
7 | use Lcobucci\JWT\Signer; |
||
8 | use Lcobucci\JWT\Signer\Key; |
||
9 | use Lcobucci\JWT\Token; |
||
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 $refresh = 50001; |
||
25 | private $relogin = 50002; |
||
26 | |||
27 | private $iss; |
||
28 | private $aud; |
||
29 | |||
30 | /** |
||
31 | * 获取 Token获取途径. |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function type() |
||
36 | { |
||
37 | return $this->type; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * 设置 Token获取途径. |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public function setType($type) |
||
46 | { |
||
47 | return $this->type = $type; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * 获取 用户表唯一标识值名. |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function getUniqidKey() |
||
56 | { |
||
57 | return $this->uniqidKey; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
58 | } |
||
59 | |||
60 | /** |
||
61 | * 获取 刷新Token TTL. |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | public function refreshTTL() |
||
66 | { |
||
67 | return (int) $this->refreshTTL; |
||
0 ignored issues
–
show
|
|||
68 | } |
||
69 | |||
70 | /** |
||
71 | * 设置 刷新Token TTL. |
||
72 | * |
||
73 | * @param [type] $value |
||
0 ignored issues
–
show
|
|||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function setRefreshTTL($value) |
||
78 | { |
||
79 | $this->refreshTTL = (int) $value; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return void |
||
84 | */ |
||
85 | public function getReloginCode() |
||
86 | { |
||
87 | return (int) $this->relogin; |
||
0 ignored issues
–
show
|
|||
88 | } |
||
89 | |||
90 | /** |
||
91 | * 获取 检测延迟 |
||
92 | * |
||
93 | * @return void |
||
94 | */ |
||
95 | public function notBefore() |
||
96 | { |
||
97 | return (int) $this->notBefore; |
||
0 ignored issues
–
show
|
|||
98 | } |
||
99 | |||
100 | /** |
||
101 | * 设置 检测延迟 |
||
102 | * |
||
103 | * @param [type] $value |
||
0 ignored issues
–
show
|
|||
104 | * |
||
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; |
||
0 ignored issues
–
show
|
|||
120 | } |
||
121 | |||
122 | /** |
||
123 | * 设置 TTL. |
||
124 | * |
||
125 | * @param int $value |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function setTTL(int $value) |
||
130 | { |
||
131 | $this->expiresAt = $value; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return void |
||
136 | */ |
||
137 | public function getAlreadyCode() |
||
138 | { |
||
139 | return $this->refresh; |
||
0 ignored issues
–
show
|
|||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return void |
||
144 | */ |
||
145 | public function getHasLoggedCode() |
||
146 | { |
||
147 | return $this->hasLogged; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * 设置有效期 |
||
152 | * |
||
153 | * @param [type] $value |
||
0 ignored issues
–
show
|
|||
154 | * |
||
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 |
||
0 ignored issues
–
show
|
|||
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 |