2023-06-18 13:03:41 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2023-10-29 12:28:21 +00:00
|
|
|
"github.com/1f349/mjwt"
|
2024-07-27 19:25:56 +01:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2023-06-18 13:03:41 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateAccessToken(t *testing.T) {
|
2023-10-29 12:28:21 +00:00
|
|
|
t.Parallel()
|
2023-06-18 13:03:41 +01:00
|
|
|
|
2024-07-27 17:05:27 +01:00
|
|
|
ps := NewPermStorage()
|
2023-06-18 13:03:41 +01:00
|
|
|
ps.Set("mjwt:test")
|
|
|
|
ps.Set("mjwt:test2")
|
|
|
|
|
2024-07-27 17:05:27 +01:00
|
|
|
kStore := mjwt.NewKeyStore()
|
2024-07-27 19:25:56 +01:00
|
|
|
s, err := mjwt.NewIssuerWithKeyStore("mjwt.test", "key1", jwt.SigningMethodRS512, kStore)
|
2024-06-09 20:40:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-07-27 17:05:27 +01:00
|
|
|
accessToken, err := CreateAccessToken(s, "1", "test", nil, ps)
|
2024-06-09 20:40:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-07-27 17:05:27 +01:00
|
|
|
_, b, err := mjwt.ExtractClaims[AccessTokenClaims](kStore, accessToken)
|
2024-06-09 20:40:13 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "1", b.Subject)
|
|
|
|
assert.Equal(t, "test", b.ID)
|
|
|
|
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"))
|
|
|
|
}
|