Skip to content

Commit

Permalink
chore: optimize auth error response (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozline authored Jan 16, 2025
1 parent 6320d50 commit 40c0b53
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 4 additions & 1 deletion api/mw/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ func CreateToken(tokenType int64) (string, error) {

// CheckToken 会检查 token 是否有效,如果有效则返回 token 类型,否则返回错误(type 会返回 -1)
func CheckToken(token string) (int64, error) {
if token == "" {
return -1, errno.AuthMissing
}
// 解析 token,但不进行签名验证
tokenStruct, _, err := new(jwt.Parser).ParseUnverified(token, &Claims{})
if err != nil {
return -1, err
return -1, errno.AuthInvalid.WithError(err)
}

unverifiedClaims, ok := tokenStruct.Claims.(*Claims)
Expand Down
1 change: 0 additions & 1 deletion pkg/errno/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const (
AuthInvalidCode = 30002 // 鉴权无效
AuthAccessExpiredCode = 30003 // 访问令牌过期
AuthRefreshExpiredCode = 30004 // 刷新令牌过期
AuthMissingCode = 30005 // 鉴权缺失

BizErrorCode = 40001 // 业务错误
BizLogicCode = 40002 // 业务逻辑错误
Expand Down
6 changes: 3 additions & 3 deletions pkg/errno/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ var (
Success = NewErrNo(SuccessCode, "ok")
CustomLaunchScreenSuccess = NewErrNo(consts.StatusOK, "ok") // 兼容处理

AuthError = NewErrNo(AuthErrorCode, "鉴权失败") // 鉴权失败,通常是内部错误,如解析失败
AuthInvalid = NewErrNo(AuthInvalidCode, "鉴权无效") // 鉴权无效,如令牌颁发者不是 west2-online
AuthError = NewErrNo(AuthErrorCode, "鉴权失败") // 鉴权失败,通常是内部错误,如解析失败

Check failure on line 27 in pkg/errno/default.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (goimports)
AuthInvalid = NewErrNo(AuthInvalidCode, "鉴权无效") // 鉴权无效,如令牌颁发者不是 west2-online
AuthAccessExpired = NewErrNo(AuthAccessExpiredCode, "访问令牌过期") // 访问令牌过期
AuthRefreshExpired = NewErrNo(AuthRefreshExpiredCode, "刷新令牌过期") // 刷新令牌过期
AuthMissing = NewErrNo(AuthMissingCode, "鉴权缺失") // 鉴权缺失,如访问令牌缺失
AuthMissing = NewErrNo(AuthInvalidCode, "缺失合法鉴权数据") // 鉴权缺失,如访问令牌缺失

ParamError = NewErrNo(ParamErrorCode, "参数错误") // 参数校验失败,可能是参数为空、参数类型错误等
ParamMissingHeader = NewErrNo(ParamMissingHeaderCode, "缺失合法学生请求头数据")
Expand Down

0 comments on commit 40c0b53

Please sign in to comment.