WsVerifyToken

test-errcode
withchao 1 year ago
parent b3c60aaf10
commit aa8c250c61

@ -7,6 +7,7 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"net/http" "net/http"
"strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -214,11 +215,11 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
var ( var (
token string token string
userID string userID string
platformID string platformIDStr string
exists bool exists bool
compression bool compression bool
) )
token, exists = connContext.Query(Token) token, exists = connContext.Query(Token)
@ -231,13 +232,17 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
httpError(connContext, errs.ErrConnArgsErr) httpError(connContext, errs.ErrConnArgsErr)
return return
} }
platformID, exists = connContext.Query(PlatformID) platformIDStr, exists = connContext.Query(PlatformID)
if !exists || utils.StringToInt(platformID) == 0 { if !exists {
httpError(connContext, errs.ErrConnArgsErr) httpError(connContext, errs.ErrConnArgsErr)
return return
} }
err := tokenverify.WsVerifyToken(token, userID, platformID) platformID, err := strconv.Atoi(platformIDStr)
if err != nil { if err != nil {
httpError(connContext, errs.ErrConnArgsErr)
return
}
if err := tokenverify.WsVerifyToken(token, userID, platformID); err != nil {
httpError(connContext, err) httpError(connContext, err)
return return
} }

@ -8,7 +8,6 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
"strconv"
"time" "time"
) )
@ -89,11 +88,7 @@ func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {
func IsManagerUserID(opUserID string) bool { func IsManagerUserID(opUserID string) bool {
return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid)
} }
func WsVerifyToken(token, userID, platformID string) error { func WsVerifyToken(token, userID string, platformID int) error {
platformIDInt, err := strconv.Atoi(platformID)
if err != nil {
return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not int", platformID))
}
claim, err := GetClaimFromToken(token) claim, err := GetClaimFromToken(token)
if err != nil { if err != nil {
return err return err
@ -101,8 +96,8 @@ func WsVerifyToken(token, userID, platformID string) error {
if claim.UserID != userID { if claim.UserID != userID {
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UserID, userID)) return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UserID, userID))
} }
if claim.PlatformID != platformIDInt { if claim.PlatformID != platformID {
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformIDInt)) return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID))
} }
return nil return nil
} }

Loading…
Cancel
Save