Skip to content

Commit 8f2b0b2

Browse files
committed
feat:令牌过期错误码
1 parent 9e63785 commit 8f2b0b2

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ $extend = [
172172
];
173173
$token = Tinywan\Jwt\JwtToken::generateToken($extend);
174174
```
175+
176+
11、令牌过期错误码
177+
178+
* 访问令牌
179+
* 身份验证令牌无效:`401011`
180+
* 身份验证令牌尚未生效:`401012`
181+
* 身份验证会话已过期,请重新登录!:`401013`
182+
* 获取的扩展字段不存在:`401014`
183+
* 访问令牌未知错误:`401015`
184+
* 刷新令牌
185+
* 刷新令牌无效:`401021`
186+
* 刷新令牌尚未生效:`401022`
187+
* 刷新令牌会话已过期,请再次登录!:`401023`
188+
* 刷新令牌获取的扩展字段不存在:`401024`
189+
* 刷新令牌未知错误:`401025`
190+
175191
## 签名算法
176192

177193
JWT 最常见的几种签名算法(JWA):`HS256(HMAC-SHA256)``RS256(RSA-SHA256)` 还有 `ES256(ECDSA-SHA256)`

src/JwtToken.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,22 @@ public static function refreshToken(): array
100100
try {
101101
$extend = self::verifyToken($token, self::REFRESH_TOKEN);
102102
} catch (SignatureInvalidException $signatureInvalidException) {
103-
throw new JwtRefreshTokenExpiredException('刷新令牌无效');
103+
throw new JwtRefreshTokenExpiredException('刷新令牌无效',401021);
104104
} catch (BeforeValidException $beforeValidException) {
105-
throw new JwtRefreshTokenExpiredException('刷新令牌尚未生效');
105+
throw new JwtRefreshTokenExpiredException('刷新令牌尚未生效',401022);
106106
} catch (ExpiredException $expiredException) {
107-
throw new JwtRefreshTokenExpiredException('刷新令牌会话已过期,请再次登录!');
107+
throw new JwtRefreshTokenExpiredException('刷新令牌会话已过期,请再次登录!',401023);
108108
} catch (UnexpectedValueException $unexpectedValueException) {
109-
throw new JwtRefreshTokenExpiredException('刷新令牌获取的扩展字段不存在');
109+
throw new JwtRefreshTokenExpiredException('刷新令牌获取的扩展字段不存在',401024);
110110
} catch (JwtCacheTokenException | \Exception $exception) {
111-
throw new JwtRefreshTokenExpiredException($exception->getMessage());
111+
throw new JwtRefreshTokenExpiredException($exception->getMessage(),401025);
112112
}
113113
$payload = self::generatePayload($config, $extend['extend']);
114114
$secretKey = self::getPrivateKey($config);
115115
$extend['exp'] = time() + $config['access_exp'];
116116
$newToken['access_token'] = self::makeToken($extend, $secretKey, $config['algorithms']);
117-
if (!isset($config['refresh_disable']) || (isset($config['refresh_disable']) && $config['refresh_disable'] === false)) {
117+
if (!isset($config['refresh_disable']) || ($config['refresh_disable'] === false)) {
118118
$refreshSecretKey = self::getPrivateKey($config, self::REFRESH_TOKEN);
119-
$payload['exp'] = time() + $config['refresh_exp'];
120119
$newToken['refresh_token'] = self::makeToken($payload['refreshPayload'], $refreshSecretKey, $config['algorithms']);
121120
}
122121
if ($config['is_single_device']) {
@@ -148,14 +147,14 @@ public static function generateToken(array $extend): array
148147
'expires_in' => $config['access_exp'],
149148
'access_token' => self::makeToken($payload['accessPayload'], $secretKey, $config['algorithms'])
150149
];
151-
if (!isset($config['refresh_disable']) || (isset($config['refresh_disable']) && $config['refresh_disable'] === false)) {
150+
if (!isset($config['refresh_disable']) || ($config['refresh_disable'] === false)) {
152151
$refreshSecretKey = self::getPrivateKey($config, self::REFRESH_TOKEN);
153152
$token['refresh_token'] = self::makeToken($payload['refreshPayload'], $refreshSecretKey, $config['algorithms']);
154153
}
155154
if ($config['is_single_device']) {
156155
$client = $extend['client'] ?? self::TOKEN_CLIENT_WEB;
157156
RedisHandler::generateToken($config['cache_token_pre'], (string)$client, (string)$extend['id'], $config['access_exp'], $token['access_token']);
158-
if (!isset($config['refresh_disable']) || (isset($config['refresh_disable']) && $config['refresh_disable'] === false)) {
157+
if (!isset($config['refresh_disable']) || ($config['refresh_disable'] === false)) {
159158
if (isset($config["cache_refresh_token_pre"])) {
160159
RedisHandler::generateToken($config["cache_refresh_token_pre"], (string)$client, (string)$extend['id'], $config['refresh_exp'], $token['refresh_token']);
161160
}
@@ -178,15 +177,15 @@ public static function verify(int $tokenType = self::ACCESS_TOKEN, string $token
178177
try {
179178
return self::verifyToken($token, $tokenType);
180179
} catch (SignatureInvalidException $signatureInvalidException) {
181-
throw new JwtTokenException('身份验证令牌无效');
180+
throw new JwtTokenException('身份验证令牌无效',401011);
182181
} catch (BeforeValidException $beforeValidException) {
183-
throw new JwtTokenException('身份验证令牌尚未生效');
182+
throw new JwtTokenException('身份验证令牌尚未生效',401012);
184183
} catch (ExpiredException $expiredException) {
185-
throw new JwtTokenExpiredException('身份验证会话已过期,请重新登录!');
184+
throw new JwtTokenExpiredException('身份验证会话已过期,请重新登录!',401013);
186185
} catch (UnexpectedValueException $unexpectedValueException) {
187-
throw new JwtTokenException('获取的扩展字段不存在');
186+
throw new JwtTokenException('获取的扩展字段不存在',401014);
188187
} catch (JwtCacheTokenException | \Exception $exception) {
189-
throw new JwtTokenException($exception->getMessage());
188+
throw new JwtTokenException($exception->getMessage(),401015);
190189
}
191190
}
192191

@@ -221,29 +220,29 @@ private static function getTokenFromHeaders(): string
221220
if (!$authorization || 'undefined' == $authorization) {
222221
$config = self::_getConfig();
223222
if (!isset($config['is_support_get_token']) || false === $config['is_support_get_token']) {
224-
throw new JwtTokenException('请求未携带authorization信息');
223+
throw new JwtTokenException('请求未携带authorization信息',401000);
225224
}
226225
$authorization = request()->get($config['is_support_get_token_key']);
227226
if (empty($authorization)) {
228-
throw new JwtTokenException('请求未携带authorization信息');
227+
throw new JwtTokenException('请求未携带authorization信息',401000);
229228
}
230229
$authorization = 'Bearer '.$authorization;
231230
}
232231

233232
if (self::REFRESH_TOKEN != substr_count($authorization, '.')) {
234-
throw new JwtTokenException('非法的authorization信息');
233+
throw new JwtTokenException('非法的authorization信息',401001);
235234
}
236235

237236
if (2 != count(explode(' ', $authorization))) {
238-
throw new JwtTokenException('Bearer验证中的凭证格式有误,中间必须有个空格');
237+
throw new JwtTokenException('Bearer验证中的凭证格式有误,中间必须有个空格',401000);
239238
}
240239

241240
[$type, $token] = explode(' ', $authorization);
242241
if ('Bearer' !== $type) {
243-
throw new JwtTokenException('接口认证方式需为Bearer');
242+
throw new JwtTokenException('接口认证方式需为Bearer',401000);
244243
}
245244
if (!$token || 'undefined' === $token) {
246-
throw new JwtTokenException('尝试获取的Authorization信息不存在');
245+
throw new JwtTokenException('尝试获取的Authorization信息不存在',401000);
247246
}
248247

249248
return $token;

0 commit comments

Comments
 (0)