Added save signature to Token struct after successful signing. Corresponding test.

This commit is contained in:
Egor Sheff 2024-11-05 13:46:29 +03:00
parent bc8bdca5cc
commit d56d945cea
2 changed files with 23 additions and 0 deletions

View File

@ -71,6 +71,8 @@ func (t *Token) SignedString(key interface{}) (string, error) {
return "", err return "", err
} }
t.Signature = sig
return sstr + "." + t.EncodeSegment(sig), nil return sstr + "." + t.EncodeSegment(sig), nil
} }

View File

@ -1,8 +1,11 @@
package jwt_test package jwt_test
import ( import (
"bytes"
"encoding/base64"
"encoding/json" "encoding/json"
"math" "math"
"strings"
"testing" "testing"
"time" "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")
}
}