mirror of
https://github.com/1f349/mjwt.git
synced 2024-11-09 22:22:48 +00:00
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package mjwt
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"fmt"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type testClaims struct{ TestValue string }
|
|
|
|
func (t testClaims) Valid() error {
|
|
if t.TestValue != "hello" && t.TestValue != "world" {
|
|
return fmt.Errorf("TestValue should be hello")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t testClaims) Type() string { return "testClaims" }
|
|
|
|
type testClaims2 struct{ TestValue2 string }
|
|
|
|
func (t testClaims2) Valid() error {
|
|
if t.TestValue2 != "world" {
|
|
return fmt.Errorf("TestValue2 should be world")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t testClaims2) Type() string { return "testClaims2" }
|
|
|
|
func TestExtractClaims(t *testing.T) {
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
assert.NoError(t, err)
|
|
|
|
s := NewMJwtSigner("mjwt.test", key)
|
|
token, err := s.GenerateJwt("1", "test", nil, 10*time.Minute, testClaims{TestValue: "hello"})
|
|
assert.NoError(t, err)
|
|
|
|
m := NewMJwtVerifier(&key.PublicKey)
|
|
_, _, err = ExtractClaims[testClaims](m, token)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestExtractClaimsFail(t *testing.T) {
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
assert.NoError(t, err)
|
|
|
|
s := NewMJwtSigner("mjwt.test", key)
|
|
token, err := s.GenerateJwt("1", "test", nil, 10*time.Minute, testClaims{TestValue: "test"})
|
|
assert.NoError(t, err)
|
|
|
|
m := NewMJwtVerifier(&key.PublicKey)
|
|
_, _, err = ExtractClaims[testClaims2](m, token)
|
|
assert.Error(t, err)
|
|
assert.ErrorIs(t, err, ErrClaimTypeMismatch)
|
|
}
|