Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 token 抛出异常时 code 不正确的问题 #39

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ QQ 群号:643205479 / 814597236

## 版本日志

最新版本:`0.3.7`
最新版本:`0.3.8`

### 0.3.8

1. `F` 修复 token 抛出异常时 code 不正确的问题

### 0.3.7

Expand Down
40 changes: 27 additions & 13 deletions lib/jwt/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Token {
*
* @param token 令牌
*/
public verifyToken(token: string) {
public verifyToken(token: string, type = TokenType.ACCESS) {
if (!this.secret) {
throw new Error('secret can not be empty');
}
Expand All @@ -127,9 +127,29 @@ class Token {
decode = jwtGenerator.verify(token, this.secret);
} catch (error) {
if (error instanceof TokenExpiredError) {
throw new ExpiredTokenException();
if (type === TokenType.ACCESS) {
throw new ExpiredTokenException({
code: 10051
});
} else if (type === TokenType.REFRESH) {
throw new ExpiredTokenException({
code: 10052
});
} else {
throw new ExpiredTokenException();
}
} else {
throw new InvalidTokenException();
if (type === TokenType.ACCESS) {
throw new InvalidTokenException({
code: 10041
});
} else if (type === TokenType.REFRESH) {
throw new InvalidTokenException({
code: 10042
});
} else {
throw new InvalidTokenException();
}
}
}
return decode;
Expand All @@ -150,10 +170,7 @@ const jwt = new Token(
* @param payload 负载,支持 string 和 object
* @param options 参数
*/
function createAccessToken(
payload: string | object,
options?: SignOptions
) {
function createAccessToken(payload: string | object, options?: SignOptions) {
// type: TokenType.REFRESH
let exp: number = Math.floor(Date.now() / 1000) + jwt.accessExp;
if (typeof payload === 'string') {
Expand All @@ -176,10 +193,7 @@ function createAccessToken(
* @param payload 负载,支持 string 和 object
* @param options 参数
*/
function createRefreshToken(
payload: string | object,
options?: SignOptions
) {
function createRefreshToken(payload: string | object, options?: SignOptions) {
let exp: number = Math.floor(Date.now() / 1000) + jwt.refreshExp;
// type: TokenType.REFRESH
if (typeof payload === 'string') {
Expand Down Expand Up @@ -279,14 +293,14 @@ function parseHeader(ctx: RouterContext, type = TokenType.ACCESS) {

if (/^Bearer$/i.test(scheme)) {
// @ts-ignore
const obj = ctx.jwt.verifyToken(token);
const obj = ctx.jwt.verifyToken(token, type);
if (!get(obj, 'type') || get(obj, 'type') !== type) {
ctx.throw(new AuthFailed({ code: 10250 }));
}
if (!get(obj, 'scope') || get(obj, 'scope') !== 'lin') {
ctx.throw(new AuthFailed({ code: 10251 }));
}
return obj
return obj;
}
} else {
ctx.throw(new AuthFailed());
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lin-mizar",
"version": "0.3.7",
"version": "0.3.8",
"description": "The core library of Lin CMS",
"main": "lin/index.js",
"scripts": {
Expand Down