|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|