2023-06-18 13:03:41 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2023-10-29 12:28:21 +00:00
|
|
|
"github.com/1f349/mjwt"
|
2023-06-18 13:03:41 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateRefreshToken(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-18 13:03:41 +01:00
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
s := mjwt.NewMJwtSigner("mjwt.test", key)
|
|
|
|
|
2023-06-20 00:32:16 +01:00
|
|
|
refreshToken, err := CreateRefreshToken(s, "1", "test", "test2", nil)
|
2023-06-18 13:03:41 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, b, err := mjwt.ExtractClaims[RefreshTokenClaims](s, refreshToken)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", b.Subject)
|
|
|
|
assert.Equal(t, "test", b.ID)
|
2023-06-20 00:32:16 +01:00
|
|
|
assert.Equal(t, "test2", b.Claims.AccessTokenId)
|
2023-06-18 13:03:41 +01:00
|
|
|
}
|
2024-06-09 20:40:13 +01:00
|
|
|
|
|
|
|
func TestCreateRefreshTokenWithKID(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
kStore := mjwt.NewMJwtKeyStore()
|
|
|
|
kStore.SetKey("test", key)
|
|
|
|
|
|
|
|
s := mjwt.NewMJwtSignerWithKeyStore("mjwt.test", nil, kStore)
|
|
|
|
|
|
|
|
refreshToken, err := CreateRefreshTokenWithKID(s, "1", "test", "test2", nil, "test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, b, err := mjwt.ExtractClaims[RefreshTokenClaims](s, refreshToken)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", b.Subject)
|
|
|
|
assert.Equal(t, "test", b.ID)
|
|
|
|
assert.Equal(t, "test2", b.Claims.AccessTokenId)
|
|
|
|
}
|