From e01ed05a3131827abc8404a9d4f6d4ee4a100501 Mon Sep 17 00:00:00 2001 From: hyeonjae Date: Thu, 3 Feb 2022 20:47:58 +0900 Subject: [PATCH] remove unnecessary for loop in token signing string for readability (#34) * remove unnecessary for loop in token signing string for readability - add testcase - add benchmark - improve performance slightly * Fix benchtests on token_test.go * Update token_test.go to v4 Co-authored-by: hyeonjae Co-authored-by: Luis Gabriel Gomez --- token.go | 25 +++++++--------- token_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 token_test.go diff --git a/token.go b/token.go index 1234413..09b4cde 100644 --- a/token.go +++ b/token.go @@ -74,22 +74,19 @@ func (t *Token) SignedString(key interface{}) (string, error) { // the SignedString. func (t *Token) SigningString() (string, error) { var err error - parts := make([]string, 2) - for i := range parts { - var jsonValue []byte - if i == 0 { - if jsonValue, err = json.Marshal(t.Header); err != nil { - return "", err - } - } else { - if jsonValue, err = json.Marshal(t.Claims); err != nil { - return "", err - } - } + var jsonValue []byte - parts[i] = EncodeSegment(jsonValue) + if jsonValue, err = json.Marshal(t.Header); err != nil { + return "", err } - return strings.Join(parts, "."), nil + header := EncodeSegment(jsonValue) + + if jsonValue, err = json.Marshal(t.Claims); err != nil { + return "", err + } + claim := EncodeSegment(jsonValue) + + return strings.Join([]string{header, claim}, "."), nil } // Parse parses, validates, verifies the signature and returns the parsed token. diff --git a/token_test.go b/token_test.go new file mode 100644 index 0000000..7944b51 --- /dev/null +++ b/token_test.go @@ -0,0 +1,79 @@ +package jwt_test + +import ( + "testing" + + "github.com/golang-jwt/jwt/v4" +) + +func TestToken_SigningString(t1 *testing.T) { + type fields struct { + Raw string + Method jwt.SigningMethod + Header map[string]interface{} + Claims jwt.Claims + Signature string + Valid bool + } + tests := []struct { + name string + fields fields + want string + wantErr bool + }{ + { + name: "", + fields: fields{ + Raw: "", + Method: jwt.SigningMethodHS256, + Header: map[string]interface{}{ + "typ": "JWT", + "alg": jwt.SigningMethodHS256.Alg(), + }, + Claims: jwt.StandardClaims{}, + Signature: "", + Valid: false, + }, + want: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30", + wantErr: false, + }, + } + for _, tt := range tests { + t1.Run(tt.name, func(t1 *testing.T) { + t := &jwt.Token{ + Raw: tt.fields.Raw, + Method: tt.fields.Method, + Header: tt.fields.Header, + Claims: tt.fields.Claims, + Signature: tt.fields.Signature, + Valid: tt.fields.Valid, + } + got, err := t.SigningString() + if (err != nil) != tt.wantErr { + t1.Errorf("SigningString() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t1.Errorf("SigningString() got = %v, want %v", got, tt.want) + } + }) + } +} + +func BenchmarkToken_SigningString(b *testing.B) { + t := &jwt.Token{ + Method: jwt.SigningMethodHS256, + Header: map[string]interface{}{ + "typ": "JWT", + "alg": jwt.SigningMethodHS256.Alg(), + }, + Claims: jwt.StandardClaims{}, + } + b.Run("BenchmarkToken_SigningString", func(b *testing.B) { + b.ResetTimer() + b.ReportAllocs() + for i := 0; i