From 1a3b5d018a99faa71fb17d28204aa8c4d277e884 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Tue, 21 Jun 2022 19:57:48 +0800 Subject: [PATCH] Handling token exceptions --- internal/msg_gateway/gate/ws_server.go | 21 +++++++++++++ pkg/common/token_verify/jwt_token.go | 42 +++++++++++++------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/internal/msg_gateway/gate/ws_server.go b/internal/msg_gateway/gate/ws_server.go index 26d1d052e..6f51ca993 100644 --- a/internal/msg_gateway/gate/ws_server.go +++ b/internal/msg_gateway/gate/ws_server.go @@ -292,9 +292,30 @@ func (ws *WServer) headerCheck(w http.ResponseWriter, r *http.Request, operation query := r.URL.Query() if len(query["token"]) != 0 && len(query["sendID"]) != 0 && len(query["platformID"]) != 0 { if ok, err, msg := token_verify.WsVerifyToken(query["token"][0], query["sendID"][0], query["platformID"][0], operationID); !ok { + if errors.Is(err, constant.ErrTokenExpired) { + status = int(constant.ErrTokenExpired.ErrCode) + } + if errors.Is(err, constant.ErrTokenInvalid) { + status = int(constant.ErrTokenInvalid.ErrCode) + } + if errors.Is(err, constant.ErrTokenMalformed) { + status = int(constant.ErrTokenMalformed.ErrCode) + } + if errors.Is(err, constant.ErrTokenNotValidYet) { + status = int(constant.ErrTokenNotValidYet.ErrCode) + } if errors.Is(err, constant.ErrTokenUnknown) { status = int(constant.ErrTokenUnknown.ErrCode) } + if errors.Is(err, constant.ErrTokenKicked) { + status = int(constant.ErrTokenKicked.ErrCode) + } + if errors.Is(err, constant.ErrTokenDifferentPlatformID) { + status = int(constant.ErrTokenDifferentPlatformID.ErrCode) + } + if errors.Is(err, constant.ErrTokenDifferentUserID) { + status = int(constant.ErrTokenDifferentUserID.ErrCode) + } //switch errors.Cause(err) { //case constant.ErrTokenExpired: // status = int(constant.ErrTokenExpired.ErrCode) diff --git a/pkg/common/token_verify/jwt_token.go b/pkg/common/token_verify/jwt_token.go index 40aba4a88..474657b50 100644 --- a/pkg/common/token_verify/jwt_token.go +++ b/pkg/common/token_verify/jwt_token.go @@ -180,18 +180,18 @@ func ParseTokenGetUserID(token string, operationID string) (error, string) { func ParseToken(tokensString, operationID string) (claims *Claims, err error) { claims, err = GetClaimFromToken(tokensString) if err != nil { - if errors.Is(err, constant.ErrTokenUnknown) { - errMsg := "GetClaimFromToken failed ErrTokenUnknown " + err.Error() - log.Error(operationID, errMsg) - } - info := err.(constant.ErrInfo) - log.Error(operationID, "detail info , ", info.ErrCode, info.ErrMsg) - - e := errors.Unwrap(err) - if errors.Is(e, constant.ErrTokenUnknown) { - errMsg := "ParseToken failed ErrTokenUnknown " + e.Error() - log.Error(operationID, errMsg) - } + //if errors.Is(err, constant.ErrTokenUnknown) { + // errMsg := "GetClaimFromToken failed ErrTokenUnknown " + err.Error() + // log.Error(operationID, errMsg) + //} + //info := err.(constant.ErrInfo) + //log.Error(operationID, "detail info , ", info.ErrCode, info.ErrMsg) + // + //e := errors.Unwrap(err) + //if errors.Is(e, constant.ErrTokenUnknown) { + // errMsg := "ParseToken failed ErrTokenUnknown " + e.Error() + // log.Error(operationID, errMsg) + //} log.NewError(operationID, "token validate err", err.Error(), tokensString, "type ", reflect.TypeOf(err), "type2: ", reflect.TypeOf(e)) return nil, utils.Wrap(err, "") @@ -260,15 +260,15 @@ func WsVerifyToken(token, uid string, platformID string, operationID string) (bo argMsg := "token: " + token + " operationID: " + operationID + " userID: " + uid + " platformID: " + platformID claims, err := ParseToken(token, operationID) if err != nil { - if errors.Is(err, constant.ErrTokenUnknown) { - errMsg := "ParseToken failed ErrTokenUnknown " + err.Error() - log.Error(operationID, errMsg) - } - e := errors.Unwrap(err) - if errors.Is(e, constant.ErrTokenUnknown) { - errMsg := "ParseToken failed ErrTokenUnknown " + e.Error() - log.Error(operationID, errMsg) - } + //if errors.Is(err, constant.ErrTokenUnknown) { + // errMsg := "ParseToken failed ErrTokenUnknown " + err.Error() + // log.Error(operationID, errMsg) + //} + //e := errors.Unwrap(err) + //if errors.Is(e, constant.ErrTokenUnknown) { + // errMsg := "ParseToken failed ErrTokenUnknown " + e.Error() + // log.Error(operationID, errMsg) + //} errMsg := "parse token err " + err.Error() + argMsg return false, utils.Wrap(err, errMsg), errMsg