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 xiaodi\JWTAuth\Exception\JWTException; |
10
|
|
|
use xiaodi\JWTAuth\Exception\JWTInvalidArgumentException; |
11
|
|
|
|
12
|
|
|
trait Jwt |
13
|
|
|
{ |
14
|
|
|
private $signerKey; |
15
|
|
|
private $notBefore = 0; |
16
|
|
|
private $expiresAt = 3600; |
17
|
|
|
private $refreshTTL = 7200; |
18
|
|
|
private $signer = \Lcobucci\JWT\Signer\Hmac\Sha256::class; |
19
|
|
|
|
20
|
|
|
private $type = 'Header'; |
21
|
|
|
|
22
|
|
|
private $hasLogged = 50401; |
23
|
|
|
private $tokenAlready = 50402; |
24
|
|
|
private $relogin = 50400; |
25
|
|
|
|
26
|
|
|
private $iss; |
27
|
|
|
private $aud; |
28
|
|
|
|
29
|
|
|
public function refreshTTL() |
30
|
|
|
{ |
31
|
|
|
return (int) $this->refreshTTL; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setRefreshTTL($value) |
35
|
|
|
{ |
36
|
|
|
$this->refreshTTL = (int) $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getReloginCode() |
40
|
|
|
{ |
41
|
|
|
return (int) $this->relogin; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function notBefore() |
45
|
|
|
{ |
46
|
|
|
return (int) $this->notBefore; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setNotBefore($value) |
50
|
|
|
{ |
51
|
|
|
$this->notBefore = (int) $value; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function ttl() |
55
|
|
|
{ |
56
|
|
|
return (int) $this->expiresAt; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setTTL(int $value) |
60
|
|
|
{ |
61
|
|
|
$this->ttl = $value; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function type() |
65
|
|
|
{ |
66
|
|
|
return $this->type; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setType($type) |
70
|
|
|
{ |
71
|
|
|
return $this->type = $type; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getAlreadyCode() |
75
|
|
|
{ |
76
|
|
|
return $this->tokenAlready; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getHasLoggedCode() |
80
|
|
|
{ |
81
|
|
|
return $this->hasLogged; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setExpiresAt($value) |
85
|
|
|
{ |
86
|
|
|
$this->expiresAt = (int) $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 获取私钥. |
91
|
|
|
* |
92
|
|
|
* @return string|null |
93
|
|
|
*/ |
94
|
|
|
public function getSignerKey() |
95
|
|
|
{ |
96
|
|
|
return $this->signerKey; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 设置私钥. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function setSignerKey($key) |
105
|
|
|
{ |
106
|
|
|
return $this->signerKey = $key; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* 设置加密方式. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function setSigner($signer) |
115
|
|
|
{ |
116
|
|
|
$this->signer = $signer; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* 是否注入用户对象. |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function injectUser() |
125
|
|
|
{ |
126
|
|
|
return $this->injectUser; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* 获取加密方式. |
131
|
|
|
* |
132
|
|
|
* @return Signer|Exception |
|
|
|
|
133
|
|
|
*/ |
134
|
|
|
private function getSigner() |
135
|
|
|
{ |
136
|
|
|
$signer = $this->signer; |
137
|
|
|
|
138
|
|
|
if (empty($signer)) { |
139
|
|
|
throw new JWTInvalidArgumentException('加密方式未配置.', 500); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$signer = new $signer(); |
143
|
|
|
|
144
|
|
|
if (!$signer instanceof Signer) { |
145
|
|
|
throw new JWTException('加密方式错误.', 500); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $signer; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 生成Key. |
153
|
|
|
* |
154
|
|
|
* @return Key |
155
|
|
|
*/ |
156
|
|
|
private function makeSignerKey() |
157
|
|
|
{ |
158
|
|
|
$key = $this->getSignerKey(); |
159
|
|
|
if (empty($key)) { |
160
|
|
|
throw new JWTException('私钥未配置.', 500); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return new Key($key); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function iss() |
167
|
|
|
{ |
168
|
|
|
$iss = $this->app->request->root(true); |
169
|
|
|
return $this->iss ?: $iss; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function aud() |
173
|
|
|
{ |
174
|
|
|
return $this->aud; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|