|
12 | 12 | use xiaodi\JWTAuth\Handle\RequestToken; |
13 | 13 | use Lcobucci\JWT\Configuration; |
14 | 14 | use Lcobucci\JWT\Token as JwtToken; |
15 | | -use Lcobucci\JWT\Signer\Key\InMemory; |
16 | 15 | use Lcobucci\JWT\Validation\Constraint\SignedWith; |
17 | 16 | use Lcobucci\JWT\Validation\Constraint\ValidAt; |
18 | 17 | use Lcobucci\Clock\SystemClock; |
19 | 18 | use xiaodi\JWTAuth\Exception\JWTException; |
| 19 | +use Lcobucci\JWT\Signer\Key\InMemory; |
20 | 20 |
|
21 | 21 | class Token |
22 | 22 | { |
@@ -59,7 +59,7 @@ public function initJwtConfiguration() |
59 | 59 | { |
60 | 60 | $this->jwtConfiguration = Configuration::forSymmetricSigner( |
61 | 61 | $this->config->getSigner(), |
62 | | - InMemory::base64Encoded($this->config->getSigningKey()) |
| 62 | + $this->config->getSignerKey() |
63 | 63 | ); |
64 | 64 | } |
65 | 65 |
|
@@ -123,24 +123,55 @@ public function parse(string $token): JwtToken |
123 | 123 | return $this->token; |
124 | 124 | } |
125 | 125 |
|
| 126 | + protected function getValidateConfig() |
| 127 | + { |
| 128 | + return Configuration::forSymmetricSigner( |
| 129 | + $this->config->getSigner(), |
| 130 | + $this->config->RSASigner() ? $this->config->getPublicKey() : $this->config->getHamcKey() |
| 131 | + ); |
| 132 | + } |
| 133 | + |
126 | 134 | /** |
127 | | - * 效验 Token |
| 135 | + * 效验合法性 Token |
128 | 136 | * @param string $token |
129 | 137 | * @return boolean |
130 | 138 | */ |
131 | 139 | public function validate(string $token) |
132 | 140 | { |
133 | 141 | $token = $this->parse($token); |
134 | | - $this->jwtConfiguration->setValidationConstraints( |
| 142 | + |
| 143 | + $jwtConfiguration = $this->getValidateConfig(); |
| 144 | + |
| 145 | + $jwtConfiguration->setValidationConstraints( |
| 146 | + new SignedWith($jwtConfiguration->signer(), $jwtConfiguration->signingKey()) |
| 147 | + ); |
| 148 | + |
| 149 | + $constraints = $jwtConfiguration->validationConstraints(); |
| 150 | + |
| 151 | + return $jwtConfiguration->validator()->validate($token, ...$constraints); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * 效验是否过期 Token |
| 156 | + * @param string $token |
| 157 | + * @return boolean |
| 158 | + */ |
| 159 | + public function validateExp(string $token) |
| 160 | + { |
| 161 | + $token = $this->parse($token); |
| 162 | + |
| 163 | + $jwtConfiguration = $this->getValidateConfig(); |
| 164 | + |
| 165 | + $jwtConfiguration->setValidationConstraints( |
135 | 166 | new ValidAt(new SystemClock(new DateTimeZone(\date_default_timezone_get()))), |
136 | | - new SignedWith($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey()) |
137 | 167 | ); |
138 | 168 |
|
139 | | - $constraints = $this->jwtConfiguration->validationConstraints(); |
| 169 | + $constraints = $jwtConfiguration->validationConstraints(); |
140 | 170 |
|
141 | | - return $this->jwtConfiguration->validator()->validate($token, ...$constraints); |
| 171 | + return $jwtConfiguration->validator()->validate($token, ...$constraints); |
142 | 172 | } |
143 | 173 |
|
| 174 | + |
144 | 175 | public function login(JwtToken $token) |
145 | 176 | { |
146 | 177 | $this->app->get('jwt.manange')->login($token); |
|
0 commit comments