|
|
@ -55,7 +55,7 @@ func secret() jwt.Keyfunc {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
func getClaimFromToken(tokensString string) (*Claims, error) {
|
|
|
|
token, err := jwt.ParseWithClaims(tokensString, &Claims{}, secret())
|
|
|
|
token, err := jwt.ParseWithClaims(tokensString, &Claims{}, secret())
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
@ -71,6 +71,18 @@ func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
|
|
|
|
claims, err = getClaimFromToken(tokensString)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1.check userid and platform class 0 not exists and 1 exists
|
|
|
|
// 1.check userid and platform class 0 not exists and 1 exists
|
|
|
|
existsInterface, err := db.DB.ExistsUserIDAndPlatform(claims.UID, Platform2class[claims.Platform])
|
|
|
|
existsInterface, err := db.DB.ExistsUserIDAndPlatform(claims.UID, Platform2class[claims.Platform])
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -82,29 +94,19 @@ func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
//OnlyOneTerminalAccess policy need to check all terminal
|
|
|
|
//OnlyOneTerminalAccess policy need to check all terminal
|
|
|
|
//When only one end is allowed to log in, there is a situation that needs to be paid attention to. After PC login,
|
|
|
|
//When only one end is allowed to log in, there is a situation that needs to be paid attention to. After PC login,
|
|
|
|
//mobile login should check two platform times. One of them is less than the redis storage time, which is the invalid token.
|
|
|
|
//mobile login should check two platform times. One of them is less than the redis storage time, which is the invalid token.
|
|
|
|
|
|
|
|
platform := "PC"
|
|
|
|
if Platform2class[claims.Platform] == "PC" {
|
|
|
|
if Platform2class[claims.Platform] == "PC" {
|
|
|
|
existsInterface, err = db.DB.ExistsUserIDAndPlatform(claims.UID, "Mobile")
|
|
|
|
platform = "Mobile"
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exists = existsInterface.(int64)
|
|
|
|
|
|
|
|
if exists == 1 {
|
|
|
|
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, "Mobile")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if res {
|
|
|
|
|
|
|
|
return nil, TokenInvalid
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
existsInterface, err = db.DB.ExistsUserIDAndPlatform(claims.UID, platform)
|
|
|
|
existsInterface, err = db.DB.ExistsUserIDAndPlatform(claims.UID, "PC")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exists = existsInterface.(int64)
|
|
|
|
exists = existsInterface.(int64)
|
|
|
|
if exists == 1 {
|
|
|
|
if exists == 1 {
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, "PC")
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, platform)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -113,7 +115,9 @@ func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// config.Config.MultiLoginPolicy.MobileAndPCTerminalAccessButOtherTerminalKickEachOther == true
|
|
|
|
|
|
|
|
// or PC/Mobile validate success
|
|
|
|
|
|
|
|
// final check
|
|
|
|
if exists == 1 {
|
|
|
|
if exists == 1 {
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, Platform2class[claims.Platform])
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, Platform2class[claims.Platform])
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -123,21 +127,7 @@ func ParseToken(tokensString string) (claims *Claims, err error) {
|
|
|
|
return nil, TokenInvalid
|
|
|
|
return nil, TokenInvalid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else if config.Config.MultiLoginPolicy.MobileAndPCTerminalAccessButOtherTerminalKickEachOther {
|
|
|
|
|
|
|
|
if exists == 1 {
|
|
|
|
|
|
|
|
res, err := MakeTheTokenInvalid(*claims, Platform2class[claims.Platform])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if res {
|
|
|
|
|
|
|
|
return nil, TokenInvalid
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
return claims, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, TokenUnknown
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func MakeTheTokenInvalid(currentClaims Claims, platformClass string) (bool, error) {
|
|
|
|
func MakeTheTokenInvalid(currentClaims Claims, platformClass string) (bool, error) {
|
|
|
@ -155,35 +145,16 @@ func MakeTheTokenInvalid(currentClaims Claims, platformClass string) (bool, erro
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {
|
|
|
|
func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {
|
|
|
|
token, err := jwt.ParseWithClaims(string(redisToken.([]uint8)), &Claims{}, secret())
|
|
|
|
return getClaimFromToken(string(redisToken.([]uint8)))
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
if ve, ok := err.(*jwt.ValidationError); ok {
|
|
|
|
|
|
|
|
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
|
|
|
|
|
|
|
|
return nil, TokenMalformed
|
|
|
|
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
|
|
|
|
|
|
|
|
return nil, TokenExpired
|
|
|
|
|
|
|
|
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
|
|
|
|
|
|
|
|
return nil, TokenNotValidYet
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return nil, TokenInvalid
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
|
|
|
|
|
|
return claims, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Validation token, false means failure, true means successful verification
|
|
|
|
//Validation token, false means failure, true means successful verification
|
|
|
|
func VerifyToken(token, uid string) bool {
|
|
|
|
func VerifyToken(token, uid string) bool {
|
|
|
|
claims, err := ParseToken(token)
|
|
|
|
claims, err := ParseToken(token)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil || claims.UID != uid {
|
|
|
|
return false
|
|
|
|
|
|
|
|
} else if claims.UID != uid {
|
|
|
|
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|