|
|
@ -99,23 +99,23 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
|
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
|
|
|
|
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
|
|
|
|
return nil, &constant.ErrTokenMalformed
|
|
|
|
return nil, constant.ErrTokenMalformed
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
|
|
|
return nil, &constant.ErrTokenExpired
|
|
|
|
return nil, constant.ErrTokenExpired
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
|
|
|
|
return nil, &constant.ErrTokenNotValidYet
|
|
|
|
return nil, constant.ErrTokenNotValidYet
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return nil, &constant.ErrTokenUnknown
|
|
|
|
return nil, constant.ErrTokenUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return nil, &constant.ErrTokenNotValidYet
|
|
|
|
return nil, constant.ErrTokenNotValidYet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
|
|
//log.NewDebug("", claims.UID, claims.Platform)
|
|
|
|
//log.NewDebug("", claims.UID, claims.Platform)
|
|
|
|
return claims, nil
|
|
|
|
return claims, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &constant.ErrTokenNotValidYet
|
|
|
|
return nil, constant.ErrTokenNotValidYet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -178,18 +178,30 @@ func ParseTokenGetUserID(token string, operationID string) (error, string) {
|
|
|
|
func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
|
|
|
|
func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
|
|
|
|
claims, err = GetClaimFromToken(tokensString)
|
|
|
|
claims, err = GetClaimFromToken(tokensString)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
|
|
|
|
//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)
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
return nil, utils.Wrap(err, "")
|
|
|
|
return nil, utils.Wrap(err, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform)
|
|
|
|
m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.NewError(operationID, "get token from redis err", err.Error(), tokensString)
|
|
|
|
log.NewError(operationID, "get token from redis err", err.Error(), tokensString)
|
|
|
|
return nil, utils.Wrap(&constant.ErrTokenInvalid, "get token from redis err")
|
|
|
|
return nil, utils.Wrap(constant.ErrTokenInvalid, "get token from redis err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m == nil {
|
|
|
|
if m == nil {
|
|
|
|
log.NewError(operationID, "get token from redis err, not in redis ", "m is nil", tokensString)
|
|
|
|
log.NewError(operationID, "get token from redis err, not in redis ", "m is nil", tokensString)
|
|
|
|
return nil, utils.Wrap(&constant.ErrTokenInvalid, "get token from redis err")
|
|
|
|
return nil, utils.Wrap(constant.ErrTokenInvalid, "get token from redis err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := m[tokensString]; ok {
|
|
|
|
if v, ok := m[tokensString]; ok {
|
|
|
|
switch v {
|
|
|
|
switch v {
|
|
|
@ -198,13 +210,13 @@ func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
|
|
|
|
return claims, nil
|
|
|
|
return claims, nil
|
|
|
|
case constant.KickedToken:
|
|
|
|
case constant.KickedToken:
|
|
|
|
log.Error(operationID, "this token has been kicked by other same terminal ", constant.ErrTokenKicked)
|
|
|
|
log.Error(operationID, "this token has been kicked by other same terminal ", constant.ErrTokenKicked)
|
|
|
|
return nil, utils.Wrap(&constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
|
|
|
|
return nil, utils.Wrap(constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
|
|
|
|
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.NewError(operationID, "redis token map not find", constant.ErrTokenUnknown)
|
|
|
|
log.NewError(operationID, "redis token map not find", constant.ErrTokenUnknown)
|
|
|
|
return nil, utils.Wrap(&constant.ErrTokenUnknown, "redis token map not find")
|
|
|
|
return nil, utils.Wrap(constant.ErrTokenUnknown, "redis token map not find")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) {
|
|
|
|
//func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) {
|
|
|
@ -242,19 +254,29 @@ func VerifyToken(token, uid string) (bool, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func WsVerifyToken(token, uid string, platformID string, operationID string) (bool, error, string) {
|
|
|
|
func WsVerifyToken(token, uid string, platformID string, operationID string) (bool, error, string) {
|
|
|
|
argMsg := "token: " + token + " operationID: " + operationID + " userID: " + uid + " platformID: " + platformID
|
|
|
|
argMsg := "args: token: " + token + " operationID: " + operationID + " userID: " + uid + " platformID: " + constant.PlatformIDToName(utils.StringToInt(platformID))
|
|
|
|
claims, err := ParseToken(token, operationID)
|
|
|
|
claims, err := ParseToken(token, operationID)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
errMsg := "parse token err " + err.Error() + argMsg
|
|
|
|
return false, utils.Wrap(err, errMsg), errMsg
|
|
|
|
return false, utils.Wrap(err, errMsg), errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if claims.UID != uid {
|
|
|
|
if claims.UID != uid {
|
|
|
|
errMsg := " uid is not same to token uid " + " claims.UID " + claims.UID + argMsg
|
|
|
|
errMsg := " uid is not same to token uid " + argMsg + " claims.UID: " + claims.UID
|
|
|
|
return false, utils.Wrap(&constant.ErrTokenDifferentUserID, errMsg), errMsg
|
|
|
|
return false, utils.Wrap(constant.ErrTokenDifferentUserID, errMsg), errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if claims.Platform != constant.PlatformIDToName(utils.StringToInt(platformID)) {
|
|
|
|
if claims.Platform != constant.PlatformIDToName(utils.StringToInt(platformID)) {
|
|
|
|
errMsg := " platform is not same to token platform " + argMsg + "claims platformID " + claims.Platform
|
|
|
|
errMsg := " platform is not same to token platform " + argMsg + " claims platformID: " + claims.Platform
|
|
|
|
return false, utils.Wrap(&constant.ErrTokenDifferentPlatformID, errMsg), errMsg
|
|
|
|
return false, utils.Wrap(constant.ErrTokenDifferentPlatformID, errMsg), errMsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.NewDebug(operationID, utils.GetSelfFuncName(), " check ok ", claims.UID, uid, claims.Platform)
|
|
|
|
log.NewDebug(operationID, utils.GetSelfFuncName(), " check ok ", claims.UID, uid, claims.Platform)
|
|
|
|
return true, nil, ""
|
|
|
|
return true, nil, ""
|
|
|
|