diff --git a/pkg/common/tokenverify/jwt_token.go b/pkg/common/tokenverify/jwt_token.go index e79306c25..1f2f0797c 100644 --- a/pkg/common/tokenverify/jwt_token.go +++ b/pkg/common/tokenverify/jwt_token.go @@ -4,10 +4,12 @@ import ( "context" "fmt" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "github.com/golang-jwt/jwt/v4" + "strconv" "time" ) @@ -89,15 +91,23 @@ func IsManagerUserID(opUserID string) bool { return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) } func WsVerifyToken(token, userID, platformID string) error { - //claim, err := GetClaimFromToken(token) - //if err != nil { - // return err - //} - //if claim.UID != userID { - // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) - //} - //if claim.Platform != platformID { - // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) - //} + platformIDInt, err := strconv.Atoi(platformID) + if err != nil { + return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not int", platformID)) + } + platform := constant.PlatformIDToName(platformIDInt) + if platform == "" { + return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not exist", platformID)) + } + claim, err := GetClaimFromToken(token) + if err != nil { + return err + } + if claim.UID != userID { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) + } + if claim.Platform != platform { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != %s", claim.Platform, platform)) + } return nil } diff --git a/pkg/common/tokenverify/jwt_token_test.go b/pkg/common/tokenverify/jwt_token_test.go index f7caaac82..122fea9f2 100644 --- a/pkg/common/tokenverify/jwt_token_test.go +++ b/pkg/common/tokenverify/jwt_token_test.go @@ -1,12 +1,23 @@ package tokenverify import ( - "github.com/stretchr/testify/assert" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" + "github.com/golang-jwt/jwt/v4" "testing" ) func Test_ParseToken(t *testing.T) { - token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiJvcGVuSU1BZG1pbiIsIlBsYXRmb3JtIjoiQVBhZCIsImV4cCI6MTY3NDYxNTA2MSwibmJmIjoxNjY2ODM4NzYxLCJpYXQiOjE2NjY4MzkwNjF9.l8RiIu6pR4ItwDOpNIDYA9LBzIcpk8r8n6NRtXjqOp8" - _, err := GetClaimFromToken(token) - assert.Nil(t, err) + config.Config.TokenPolicy.AccessSecret = "OpenIM_server" + claims1 := BuildClaims("123456", constant.AndroidPlatformStr, 10) + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims1) + tokenString, err := token.SignedString([]byte(config.Config.TokenPolicy.AccessSecret)) + if err != nil { + t.Fatal(err) + } + claim2, err := GetClaimFromToken(tokenString) + if err != nil { + t.Fatal(err) + } + t.Log(claim2) }