From d56d945ceafaa3449f816304e5de1afd957bc8fc Mon Sep 17 00:00:00 2001 From: Egor Sheff Date: Tue, 5 Nov 2024 13:46:29 +0300 Subject: [PATCH] Added save signature to Token struct after successful signing. Corresponding test. --- token.go | 2 ++ types_test.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/token.go b/token.go index 9c7f4ab0..d9d96c0c 100644 --- a/token.go +++ b/token.go @@ -71,6 +71,8 @@ func (t *Token) SignedString(key interface{}) (string, error) { return "", err } + t.Signature = sig + return sstr + "." + t.EncodeSegment(sig), nil } diff --git a/types_test.go b/types_test.go index bd7b139f..028379dd 100644 --- a/types_test.go +++ b/types_test.go @@ -1,8 +1,11 @@ package jwt_test import ( + "bytes" + "encoding/base64" "encoding/json" "math" + "strings" "testing" "time" @@ -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") + } +}