2021-08-22 20:23:13 +03:00
|
|
|
package jwt_test
|
|
|
|
|
|
|
|
import (
|
2024-11-05 13:46:29 +03:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2021-08-22 20:23:13 +03:00
|
|
|
"encoding/json"
|
2022-06-04 05:13:34 +03:00
|
|
|
"math"
|
2024-11-05 13:46:29 +03:00
|
|
|
"strings"
|
2021-08-22 20:23:13 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-21 16:32:25 +03:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
2021-08-22 20:23:13 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNumericDate(t *testing.T) {
|
|
|
|
var s struct {
|
|
|
|
Iat jwt.NumericDate `json:"iat"`
|
|
|
|
Exp jwt.NumericDate `json:"exp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
oldPrecision := jwt.TimePrecision
|
|
|
|
|
|
|
|
jwt.TimePrecision = time.Microsecond
|
|
|
|
|
2022-02-10 05:54:31 +03:00
|
|
|
raw := `{"iat":1516239022.000000,"exp":1516239022.123450}`
|
2021-08-22 20:23:13 +03:00
|
|
|
|
2022-02-10 05:54:31 +03:00
|
|
|
if err := json.Unmarshal([]byte(raw), &s); err != nil {
|
|
|
|
t.Fatalf("Unexpected error: %s", err)
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b, _ := json.Marshal(s)
|
|
|
|
|
|
|
|
if raw != string(b) {
|
2023-12-21 21:42:56 +03:00
|
|
|
t.Errorf("Serialized format of numeric date mismatch. Expecting: %s Got: %s", raw, string(b))
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
jwt.TimePrecision = oldPrecision
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSingleArrayMarshal(t *testing.T) {
|
|
|
|
jwt.MarshalSingleStringAsArray = false
|
|
|
|
|
|
|
|
s := jwt.ClaimStrings{"test"}
|
|
|
|
expected := `"test"`
|
|
|
|
|
|
|
|
b, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected != string(b) {
|
2023-12-21 21:42:56 +03:00
|
|
|
t.Errorf("Serialized format of string array mismatch. Expecting: %s Got: %s", expected, string(b))
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
jwt.MarshalSingleStringAsArray = true
|
|
|
|
|
|
|
|
expected = `["test"]`
|
|
|
|
|
|
|
|
b, err = json.Marshal(s)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected != string(b) {
|
2023-12-21 21:42:56 +03:00
|
|
|
t.Errorf("Serialized format of string array mismatch. Expecting: %s Got: %s", expected, string(b))
|
2021-08-22 20:23:13 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 05:54:31 +03:00
|
|
|
|
|
|
|
func TestNumericDate_MarshalJSON(t *testing.T) {
|
|
|
|
// Do not run this test in parallel because it's changing
|
|
|
|
// global state.
|
|
|
|
oldPrecision := jwt.TimePrecision
|
|
|
|
t.Cleanup(func() {
|
|
|
|
jwt.TimePrecision = oldPrecision
|
|
|
|
})
|
|
|
|
|
|
|
|
tt := []struct {
|
|
|
|
in time.Time
|
|
|
|
want string
|
|
|
|
precision time.Duration
|
|
|
|
}{
|
|
|
|
{time.Unix(5243700879, 0), "5243700879", time.Second},
|
|
|
|
{time.Unix(5243700879, 0), "5243700879.000", time.Millisecond},
|
2022-06-04 05:13:34 +03:00
|
|
|
{time.Unix(5243700879, 0), "5243700879.000000", time.Microsecond},
|
|
|
|
{time.Unix(5243700879, 0), "5243700879.000000000", time.Nanosecond},
|
2022-02-10 05:54:31 +03:00
|
|
|
//
|
|
|
|
{time.Unix(4239425898, 0), "4239425898", time.Second},
|
|
|
|
{time.Unix(4239425898, 0), "4239425898.000", time.Millisecond},
|
|
|
|
{time.Unix(4239425898, 0), "4239425898.000000", time.Microsecond},
|
|
|
|
{time.Unix(4239425898, 0), "4239425898.000000000", time.Nanosecond},
|
|
|
|
//
|
2022-06-04 05:13:34 +03:00
|
|
|
{time.Unix(253402271999, 0), "253402271999", time.Second},
|
|
|
|
{time.Unix(253402271999, 0), "253402271999.000", time.Millisecond},
|
|
|
|
{time.Unix(253402271999, 0), "253402271999.000000", time.Microsecond},
|
|
|
|
{time.Unix(253402271999, 0), "253402271999.000000000", time.Nanosecond},
|
|
|
|
//
|
2022-02-10 05:54:31 +03:00
|
|
|
{time.Unix(0, 1644285000210402000), "1644285000", time.Second},
|
|
|
|
{time.Unix(0, 1644285000210402000), "1644285000.210", time.Millisecond},
|
|
|
|
{time.Unix(0, 1644285000210402000), "1644285000.210402", time.Microsecond},
|
2022-06-04 05:13:34 +03:00
|
|
|
{time.Unix(0, 1644285000210402000), "1644285000.210402000", time.Nanosecond},
|
2022-02-10 05:54:31 +03:00
|
|
|
//
|
|
|
|
{time.Unix(0, 1644285315063096000), "1644285315", time.Second},
|
|
|
|
{time.Unix(0, 1644285315063096000), "1644285315.063", time.Millisecond},
|
|
|
|
{time.Unix(0, 1644285315063096000), "1644285315.063096", time.Microsecond},
|
2022-06-04 05:13:34 +03:00
|
|
|
{time.Unix(0, 1644285315063096000), "1644285315.063096000", time.Nanosecond},
|
|
|
|
// Maximum time that a go time.Time can represent
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999", time.Millisecond},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999", time.Microsecond},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807.999999999", time.Nanosecond},
|
|
|
|
// Strange precisions
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775807", time.Second},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854775756", time.Minute},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854774016", time.Hour},
|
|
|
|
{time.Unix(math.MaxInt64, 999999999), "9223372036854745216", 24 * time.Hour},
|
2022-02-10 05:54:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range tt {
|
|
|
|
jwt.TimePrecision = tc.precision
|
|
|
|
by, err := jwt.NewNumericDate(tc.in).MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got := string(by); got != tc.want {
|
|
|
|
t.Errorf("[%d]: failed encoding: got %q want %q", i, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-05 13:46:29 +03:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|