Merge remote-tracking branch 'origin/superGroup' into superGroup

pull/236/head
Gordon 3 years ago
commit 5646db52a9

@ -10,7 +10,9 @@ import (
"bytes"
"encoding/gob"
go_redis "github.com/go-redis/redis/v8"
"gopkg.in/errgo.v2/errors"
"github.com/pkg/errors"
//"gopkg.in/errgo.v2/errors"
"net/http"
"sync"
"time"
@ -290,25 +292,29 @@ 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 {
switch errors.Cause(err) {
case constant.ErrTokenExpired:
status = int(constant.ErrTokenExpired.ErrCode)
case constant.ErrTokenInvalid:
status = int(constant.ErrTokenInvalid.ErrCode)
case constant.ErrTokenMalformed:
status = int(constant.ErrTokenMalformed.ErrCode)
case constant.ErrTokenNotValidYet:
status = int(constant.ErrTokenNotValidYet.ErrCode)
case constant.ErrTokenUnknown:
if errors.Is(err, constant.ErrTokenUnknown) {
status = int(constant.ErrTokenUnknown.ErrCode)
case constant.ErrTokenKicked:
status = int(constant.ErrTokenKicked.ErrCode)
case constant.ErrTokenDifferentPlatformID:
status = int(constant.ErrTokenDifferentPlatformID.ErrCode)
case constant.ErrTokenDifferentUserID:
status = int(constant.ErrTokenDifferentUserID.ErrCode)
}
log.Error(operationID, "Token verify failed ", "query ", query, msg, err.Error())
//switch errors.Cause(err) {
//case constant.ErrTokenExpired:
// status = int(constant.ErrTokenExpired.ErrCode)
//case constant.ErrTokenInvalid:
// status = int(constant.ErrTokenInvalid.ErrCode)
//case constant.ErrTokenMalformed:
// status = int(constant.ErrTokenMalformed.ErrCode)
//case constant.ErrTokenNotValidYet:
// status = int(constant.ErrTokenNotValidYet.ErrCode)
//case constant.ErrTokenUnknown:
// status = int(constant.ErrTokenUnknown.ErrCode)
//case constant.ErrTokenKicked:
// status = int(constant.ErrTokenKicked.ErrCode)
//case constant.ErrTokenDifferentPlatformID:
// status = int(constant.ErrTokenDifferentPlatformID.ErrCode)
//case constant.ErrTokenDifferentUserID:
// status = int(constant.ErrTokenDifferentUserID.ErrCode)
//}
log.Error(operationID, "Token verify failed ", "query ", query, msg, err.Error(), "status: ", status)
w.Header().Set("Sec-Websocket-Version", "13")
w.Header().Set("ws_err_msg", err.Error())
http.Error(w, err.Error(), status)

@ -12,36 +12,10 @@ var (
OK = ErrInfo{0, ""}
ErrServer = ErrInfo{500, "server error"}
// ErrMysql = ErrInfo{100, ""}
// ErrMongo = ErrInfo{110, ""}
// ErrRedis = ErrInfo{120, ""}
ErrParseToken = ErrInfo{700, ParseTokenMsg.Error()}
// ErrCreateToken = ErrInfo{201, "Create token failed"}
// ErrAppServerKey = ErrInfo{300, "key error"}
ErrTencentCredential = ErrInfo{400, ThirdPartyMsg.Error()}
// ErrorUserRegister = ErrInfo{600, "User registration failed"}
// ErrAccountExists = ErrInfo{601, "The account is already registered and cannot be registered again"}
// ErrUserPassword = ErrInfo{602, "User password error"}
// ErrRefreshToken = ErrInfo{605, "Failed to refresh token"}
// ErrAddFriend = ErrInfo{606, "Failed to add friends"}
// ErrAgreeToAddFriend = ErrInfo{607, "Failed to agree application"}
// ErrAddFriendToBlack = ErrInfo{608, "Failed to add friends to the blacklist"}
// ErrGetBlackList = ErrInfo{609, "Failed to get blacklist"}
// ErrDeleteFriend = ErrInfo{610, "Failed to delete friend"}
// ErrGetFriendApplyList = ErrInfo{611, "Failed to get friend application list"}
// ErrGetFriendList = ErrInfo{612, "Failed to get friend list"}
// ErrRemoveBlackList = ErrInfo{613, "Failed to remove blacklist"}
// ErrSearchUserInfo = ErrInfo{614, "Can't find the user information"}
// ErrDelAppleDeviceToken = ErrInfo{615, ""}
// ErrModifyUserInfo = ErrInfo{616, "update user some attribute failed"}
// ErrSetFriendComment = ErrInfo{617, "set friend comment failed"}
// ErrSearchUserInfoFromTheGroup = ErrInfo{618, "There is no such group or the user not in the group"}
// ErrCreateGroup = ErrInfo{619, "create group chat failed"}
// ErrJoinGroupApplication = ErrInfo{620, "Failed to apply to join the group"}
// ErrQuitGroup = ErrInfo{621, "Failed to quit the group"}
// ErrSetGroupInfo = ErrInfo{622, "Failed to set group info"}
// ErrParam = ErrInfo{700, "param failed"}
ErrTokenExpired = ErrInfo{701, TokenExpiredMsg.Error()}
ErrTokenInvalid = ErrInfo{702, TokenInvalidMsg.Error()}
ErrTokenMalformed = ErrInfo{703, TokenMalformedMsg.Error()}

@ -8,6 +8,8 @@ import (
"Open_IM/pkg/utils"
go_redis "github.com/go-redis/redis/v8"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"reflect"
"time"
)
@ -99,23 +101,23 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, &constant.ErrTokenMalformed
return nil, constant.ErrTokenMalformed
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, &constant.ErrTokenExpired
return nil, constant.ErrTokenExpired
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
return nil, &constant.ErrTokenNotValidYet
return nil, constant.ErrTokenNotValidYet
} else {
return nil, &constant.ErrTokenUnknown
return nil, constant.ErrTokenUnknown
}
} else {
return nil, &constant.ErrTokenNotValidYet
return nil, constant.ErrTokenNotValidYet
}
} else {
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
//log.NewDebug("", claims.UID, claims.Platform)
return claims, nil
}
return nil, &constant.ErrTokenNotValidYet
return nil, constant.ErrTokenNotValidYet
}
}
@ -178,7 +180,23 @@ func ParseTokenGetUserID(token string, operationID string) (error, string) {
func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
claims, err = GetClaimFromToken(tokensString)
if err != nil {
log.NewError(operationID, "token validate err", err.Error(), tokensString)
if errors.Is(err, constant.ErrTokenUnknown) {
errMsg := "GetClaimFromToken failed ErrTokenUnknown " + err.Error()
log.Error(operationID, errMsg)
}
info := err.(constant.ErrInfo)
if info == constant.ErrTokenUnknown {
errMsg := "info == constant.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)
}
log.NewError(operationID, "token validate err", err.Error(), tokensString, "type ", reflect.TypeOf(err), "type2: ", reflect.TypeOf(e))
return nil, utils.Wrap(err, "")
}
@ -245,6 +263,16 @@ 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)
}
errMsg := "parse token err " + err.Error() + argMsg
return false, utils.Wrap(err, errMsg), errMsg
}

Loading…
Cancel
Save