Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary for loop in token signing string for readability #34

Merged
merged 3 commits into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions token.go
Expand Up @@ -64,22 +64,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
}
}

parts[i] = EncodeSegment(jsonValue)
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
}
return strings.Join(parts, "."), nil
claim := EncodeSegment(jsonValue)

return strings.Join([]string{header, claim}, "."), nil
}

// Parse, validate, and return a token.
Expand Down
77 changes: 77 additions & 0 deletions token_test.go
@@ -0,0 +1,77 @@
package jwt_test

import (
"testing"

"github.com/golang-jwt/jwt"
lggomez marked this conversation as resolved.
Show resolved Hide resolved
lggomez marked this conversation as resolved.
Show resolved Hide resolved
)

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) {
oxisto marked this conversation as resolved.
Show resolved Hide resolved
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) {
lggomez marked this conversation as resolved.
Show resolved Hide resolved
for i := 0; i<b.N; i++ {
t.SigningString()
}
})
}