WsVerifyToken

test-errcode
withchao 1 year ago
parent 3d119d4632
commit 24e7941b48

@ -4,10 +4,12 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "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/common/mcontext"
"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,15 +91,23 @@ 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, platformID string) error {
//claim, err := GetClaimFromToken(token) platformIDInt, err := strconv.Atoi(platformID)
//if err != nil { if err != nil {
// return err return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not int", platformID))
//} }
//if claim.UID != userID { platform := constant.PlatformIDToName(platformIDInt)
// return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) if platform == "" {
//} return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not exist", platformID))
//if claim.Platform != platformID { }
// return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, 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 return nil
} }

@ -1,12 +1,23 @@
package tokenverify package tokenverify
import ( 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" "testing"
) )
func Test_ParseToken(t *testing.T) { func Test_ParseToken(t *testing.T) {
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiJvcGVuSU1BZG1pbiIsIlBsYXRmb3JtIjoiQVBhZCIsImV4cCI6MTY3NDYxNTA2MSwibmJmIjoxNjY2ODM4NzYxLCJpYXQiOjE2NjY4MzkwNjF9.l8RiIu6pR4ItwDOpNIDYA9LBzIcpk8r8n6NRtXjqOp8" config.Config.TokenPolicy.AccessSecret = "OpenIM_server"
_, err := GetClaimFromToken(token) claims1 := BuildClaims("123456", constant.AndroidPlatformStr, 10)
assert.Nil(t, err) 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)
} }

Loading…
Cancel
Save