Skip to content

Commit

Permalink
remove unnecessary for loop in token signing string for readability (g…
Browse files Browse the repository at this point in the history
…olang-jwt#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 <hyeonjae@ip-192-168-1-3.ap-northeast-2.compute.internal>
Co-authored-by: Luis Gabriel Gomez <lggomez@users.noreply.github.com>
  • Loading branch information
3 people authored and oxisto committed Feb 21, 2023
1 parent 99736c3 commit d226bad
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
17 changes: 17 additions & 0 deletions token.go
Expand Up @@ -74,6 +74,7 @@ func (t *Token) SignedString(key interface{}) (string, error) {
// need this for something special, just go straight for
// the SignedString.
func (t *Token) SigningString() (string, error) {
<<<<<<< HEAD
h, err := json.Marshal(t.Header)
if err != nil {
return "", err
Expand All @@ -85,6 +86,22 @@ func (t *Token) SigningString() (string, error) {
}

return EncodeSegment(h) + "." + EncodeSegment(c), nil
=======
var err error
var jsonValue []byte

if jsonValue, err = json.Marshal(t.Header); err != nil {
return "", err
}
header := EncodeSegment(jsonValue)

if jsonValue, err = json.Marshal(t.Claims); err != nil {
return "", err
}
claim := EncodeSegment(jsonValue)

return strings.Join([]string{header, claim}, "."), nil
>>>>>>> e01ed05 (remove unnecessary for loop in token signing string for readability (#34))
}

// Parse parses, validates, verifies the signature and returns the parsed token.
Expand Down
79 changes: 79 additions & 0 deletions 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<b.N; i++ {
t.SigningString()
}
})
}

0 comments on commit d226bad

Please sign in to comment.