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

Save signature to Token struct after successful signing #417

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func (t *Token) SignedString(key interface{}) (string, error) {
return "", err
}

t.Signature = sig

return sstr + "." + t.EncodeSegment(sig), nil
}

Expand Down
21 changes: 21 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package jwt_test

import (
"bytes"
"encoding/base64"
"encoding/json"
"math"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -124,3 +127,21 @@ func TestNumericDate_MarshalJSON(t *testing.T) {
}
}
}

func TestGetSignatureAfterSigning(t *testing.T) {
token := jwt.New(jwt.SigningMethodHS256, nil)
signedString, err := token.SignedString([]byte("test12345"))
if err != nil {
t.Fatal(err)
}

sigStr := signedString[strings.LastIndex(signedString, ".")+1:]
sig, err := base64.RawURLEncoding.DecodeString(sigStr)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(sig, token.Signature) {
t.Errorf("token.Signature not equal to signature in signed string")
}
}