mirror of
https://github.com/1f349/mjwt.git
synced 2024-11-14 15:31:36 +00:00
34 lines
849 B
Go
34 lines
849 B
Go
package auth
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"github.com/MrMelon54/mjwt"
|
|
"github.com/MrMelon54/mjwt/claims"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
func TestCreateRefreshToken(t *testing.T) {
|
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
assert.NoError(t, err)
|
|
|
|
ps := claims.NewPermStorage()
|
|
ps.Set("mjwt:test")
|
|
ps.Set("mjwt:test2")
|
|
|
|
s := mjwt.NewMJwtSigner("mjwt.test", key)
|
|
|
|
refreshToken, err := CreateRefreshToken(s, "1", "test", 1, ps)
|
|
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, uint64(1), b.Claims.UserId)
|
|
assert.True(t, b.Claims.Perms.Has("mjwt:test"))
|
|
assert.True(t, b.Claims.Perms.Has("mjwt:test2"))
|
|
assert.False(t, b.Claims.Perms.Has("mjwt:test3"))
|
|
}
|