From 30bff1e64b63fcec630568459e5de10eb3f2e02b Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Tue, 27 Aug 2024 13:49:17 +0800 Subject: [PATCH] add jwt parser --- go.work | 1 + jwt/claims.go | 7 ++++--- jwt/claims_test.go | 3 ++- jwt/define.go | 3 ++- jwt/define_test.go | 3 ++- jwt/go.mod | 11 ++++++++++- jwt/token.go | 7 +++++-- 7 files changed, 26 insertions(+), 9 deletions(-) diff --git a/go.work b/go.work index d9fccaf..90a2422 100644 --- a/go.work +++ b/go.work @@ -4,4 +4,5 @@ use ( ./errors ./validator ./worker_pool + ./jwt ) diff --git a/jwt/claims.go b/jwt/claims.go index af9f1a7..d4a7a62 100644 --- a/jwt/claims.go +++ b/jwt/claims.go @@ -5,7 +5,7 @@ type DataClaims map[string]string const ( idCode = "id" roleCode = "role" - deviceIdCode = "device_id" + deviceIDCode = "device_id" scopeCode = "scope" uidCode = "uid" ) @@ -26,7 +26,7 @@ func (c DataClaims) SetRole(role string) { } func (c DataClaims) SetDeviceID(deviceID string) { - c.Set(deviceIdCode, deviceID) + c.Set(deviceIDCode, deviceID) } func (c DataClaims) SetScope(scope string) { @@ -43,6 +43,7 @@ func (c DataClaims) Get(key string) string { if val, ok := c[key]; ok { return val } + return "" } @@ -59,7 +60,7 @@ func (c DataClaims) ID() string { } func (c DataClaims) DeviceID() string { - return c.Get(deviceIdCode) + return c.Get(deviceIDCode) } func (c DataClaims) UID() string { diff --git a/jwt/claims_test.go b/jwt/claims_test.go index ec90cc9..260b8c6 100644 --- a/jwt/claims_test.go +++ b/jwt/claims_test.go @@ -1,8 +1,9 @@ package jwt import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestDataClaimsSettersAndGetters(t *testing.T) { diff --git a/jwt/define.go b/jwt/define.go index 0d54cd1..f8cf828 100644 --- a/jwt/define.go +++ b/jwt/define.go @@ -1,8 +1,9 @@ package jwt import ( - "github.com/golang-jwt/jwt/v4" "time" + + "github.com/golang-jwt/jwt/v4" ) type Token struct { diff --git a/jwt/define_test.go b/jwt/define_test.go index 27b8c5a..2496e64 100644 --- a/jwt/define_test.go +++ b/jwt/define_test.go @@ -1,9 +1,10 @@ package jwt import ( - "github.com/stretchr/testify/require" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestAccessTokenExpires(t *testing.T) { diff --git a/jwt/go.mod b/jwt/go.mod index ebde9c2..c2a705a 100644 --- a/jwt/go.mod +++ b/jwt/go.mod @@ -2,4 +2,13 @@ module code.30cm.net/digimon/library-go/jwt go 1.22.3 -require github.com/golang-jwt/jwt/v4 v4.5.0 +require ( + github.com/golang-jwt/jwt/v4 v4.5.0 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/jwt/token.go b/jwt/token.go index 049e3a5..a58e6fe 100644 --- a/jwt/token.go +++ b/jwt/token.go @@ -2,8 +2,9 @@ package jwt import ( "fmt" - "github.com/golang-jwt/jwt/v4" "time" + + "github.com/golang-jwt/jwt/v4" ) func GenerateAccessToken(token Token, data any, sign string, issuer string) (string, error) { @@ -35,6 +36,7 @@ func ParseToken(accessToken string, secret string, validate bool) (jwt.MapClaims if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("token unexpected signing method: %v", token.Header["alg"]) } + return []byte(secret), nil }) if err != nil { @@ -42,7 +44,7 @@ func ParseToken(accessToken string, secret string, validate bool) (jwt.MapClaims } } else { parser := jwt.NewParser(jwt.WithoutClaimsValidation()) - token, err = parser.Parse(accessToken, func(token *jwt.Token) (interface{}, error) { + token, err = parser.Parse(accessToken, func(_ *jwt.Token) (interface{}, error) { return []byte(secret), nil }) if err != nil { @@ -84,5 +86,6 @@ func convertMap(input map[string]interface{}) map[string]string { output[key] = fmt.Sprintf("%v", value) } } + return output }