added config options and nonce

This commit is contained in:
ston1th 2025-10-15 21:49:13 +02:00
commit 473d54569c
10 changed files with 146 additions and 85 deletions

View file

@ -1,4 +1,4 @@
// Copyright (C) 2018 Marius Schellenberger
// Copyright (C) 2025 Marius Schellenberger
package jwt
@ -17,7 +17,10 @@ var claims = Claims{
}
func TestValidate(t *testing.T) {
jwt, err := New(time.Second, NewMemBlacklist(), nil)
jwt, err := New(
WithExpiry(time.Second),
WithBlacklist(NewMemBlacklist()),
)
if err != nil {
t.Error(err)
}
@ -83,8 +86,35 @@ func TestValidate(t *testing.T) {
}
}
func TestNonce(t *testing.T) {
jwt, err := New(
WithExpiry(time.Second),
WithNonce(),
)
if err != nil {
t.Error(err)
}
token := NewToken(claims.Copy(), nil)
err = jwt.Sign(token)
if err != nil {
t.Error(err)
}
nt, err := DecodeToken(token.String())
if err != nil {
t.Error(err)
}
err = jwt.Verify(nt)
if err != nil {
t.Error(err)
}
nonce := nt.Claims.GetString(NonceClaim)
if nonce == "" {
t.Error(errors.New("nonce is empty"))
}
}
func TestNoBlacklist(t *testing.T) {
jwt, err := New(time.Second, nil, nil)
jwt, err := New(WithExpiry(time.Second))
if err != nil {
t.Error(err)
}
@ -118,21 +148,27 @@ func TestNoBlacklist(t *testing.T) {
}
func TestEmptySecretReader(t *testing.T) {
_, err := New(time.Second, nil, new(bytes.Buffer))
_, err := New(
WithExpiry(time.Second),
WithSecret(new(bytes.Buffer)),
)
if err == nil {
t.Error(errors.New("error should be secret reader error"))
}
}
func TestInvalidSecretReader(t *testing.T) {
_, err := New(time.Second, nil, bytes.NewBufferString("123"))
_, err := New(
WithExpiry(time.Second),
WithSecret(bytes.NewBufferString("123")),
)
if err != ErrInvalidKeySize {
t.Error(errors.New("error should be invalid key size"))
}
}
func BenchmarkDecodeToken(b *testing.B) {
jwt, _ := New(0, nil, nil)
jwt, _ := New()
t := NewToken(claims.Copy(), nil)
jwt.Sign(t)
token := t.String()
@ -145,19 +181,19 @@ func BenchmarkDecodeToken(b *testing.B) {
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
New(0, nil, nil)
New()
}
}
func BenchmarkNewWithBlacklist(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
New(0, NewMemBlacklist(), nil)
New(WithBlacklist(NewMemBlacklist()))
}
}
func BenchmarkSignHS256(b *testing.B) {
jwt, _ := New(0, nil, nil)
jwt, _ := New()
t := NewToken(claims.Copy(), nil)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
@ -166,8 +202,8 @@ func BenchmarkSignHS256(b *testing.B) {
}
func BenchmarkSignHS384(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS384())
jwt, _ := New()
t := NewToken(claims.Copy(), NewHash(HS384Name))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Sign(t)
@ -175,8 +211,8 @@ func BenchmarkSignHS384(b *testing.B) {
}
func BenchmarkSignHS512(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS512())
jwt, _ := New()
t := NewToken(claims.Copy(), NewHash(HS512Name))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
jwt.Sign(t)
@ -184,7 +220,7 @@ func BenchmarkSignHS512(b *testing.B) {
}
func BenchmarkVerifyHS256(b *testing.B) {
jwt, _ := New(0, nil, nil)
jwt, _ := New()
t := NewToken(claims.Copy(), nil)
jwt.Sign(t)
b.ReportAllocs()
@ -194,8 +230,8 @@ func BenchmarkVerifyHS256(b *testing.B) {
}
func BenchmarkVerifyHS384(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS384())
jwt, _ := New()
t := NewToken(claims.Copy(), NewHash(HS384Name))
jwt.Sign(t)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
@ -204,8 +240,8 @@ func BenchmarkVerifyHS384(b *testing.B) {
}
func BenchmarkVerifyHS512(b *testing.B) {
jwt, _ := New(0, nil, nil)
t := NewToken(claims.Copy(), NewHS512())
jwt, _ := New()
t := NewToken(claims.Copy(), NewHash(HS512Name))
jwt.Sign(t)
b.ReportAllocs()
for i := 0; i < b.N; i++ {