From b02830c8916547b5a952bd9cb7b63f3b0e656385 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Tue, 30 Nov 2021 15:37:51 +0800 Subject: [PATCH] token modify --- internal/utils/jwt_token_test.go | 6 ++++++ pkg/common/token_verify/jwt_token.go | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/utils/jwt_token_test.go b/internal/utils/jwt_token_test.go index 721df496e..2c6550d87 100644 --- a/internal/utils/jwt_token_test.go +++ b/internal/utils/jwt_token_test.go @@ -81,3 +81,9 @@ func Test_ParseToken(t *testing.T) { assert.Equal(t, claims.UID, uid) } } +func Test_GetClaimFromToken(t *testing.T) { + token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiJvcGVuSU0xMjM0NTYiLCJQbGF0Zm9ybSI6IiIsImV4cCI6MTYzODg0NjQ3NiwibmJmIjoxNjM4MjQxNjc2LCJpYXQiOjE2MzgyNDE2NzZ9.W8RZB7ec5ySFj-rGE2Aho2z32g3MprQMdCyPiQu_C2I" + c, err := token_verify.GetClaimFromToken(token) + assert.Nil(t, c) + assert.Nil(t, err) +} diff --git a/pkg/common/token_verify/jwt_token.go b/pkg/common/token_verify/jwt_token.go index 2f5f32c23..961919605 100644 --- a/pkg/common/token_verify/jwt_token.go +++ b/pkg/common/token_verify/jwt_token.go @@ -55,7 +55,7 @@ func secret() jwt.Keyfunc { } } -func getClaimFromToken(tokensString string) (*Claims, error) { +func GetClaimFromToken(tokensString string) (*Claims, error) { token, err := jwt.ParseWithClaims(tokensString, &Claims{}, secret()) if err != nil { if ve, ok := err.(*jwt.ValidationError); ok { @@ -82,7 +82,7 @@ func getClaimFromToken(tokensString string) (*Claims, error) { func ParseToken(tokensString string) (claims *Claims, err error) { - claims, err = getClaimFromToken(tokensString) + claims, err = GetClaimFromToken(tokensString) if err != nil { log.NewError("", "token validate err", err.Error()) return nil, err @@ -112,7 +112,7 @@ func ParseToken(tokensString string) (claims *Claims, err error) { return nil, &constant.ErrTokenUnknown } } - return nil, err + return nil, &constant.ErrTokenUnknown } //func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) { @@ -132,7 +132,7 @@ func ParseToken(tokensString string) (claims *Claims, err error) { //} func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) { - return getClaimFromToken(string(redisToken.([]uint8))) + return GetClaimFromToken(string(redisToken.([]uint8))) } //Validation token, false means failure, true means successful verification