|
|
@ -3,14 +3,20 @@ package jwt
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
|
|
|
i "github.com/portainer/portainer/api/internal/testhelpers"
|
|
|
|
"github.com/portainer/portainer/api/datastore"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestService_GenerateTokenForKubeconfig(t *testing.T) {
|
|
|
|
func TestService_GenerateTokenForKubeconfig(t *testing.T) {
|
|
|
|
|
|
|
|
_, store := datastore.MustNewTestStore(t, true, false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err := store.User().Create(&portainer.User{ID: 1})
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
type fields struct {
|
|
|
|
userSessionTimeout string
|
|
|
|
userSessionTimeout string
|
|
|
|
dataStore dataservices.DataStore
|
|
|
|
dataStore dataservices.DataStore
|
|
|
@ -20,13 +26,17 @@ func TestService_GenerateTokenForKubeconfig(t *testing.T) {
|
|
|
|
data *portainer.TokenData
|
|
|
|
data *portainer.TokenData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mySettings := &portainer.Settings{
|
|
|
|
settings, err := store.Settings().Settings()
|
|
|
|
KubeconfigExpiry: "0",
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
settings.KubeconfigExpiry = "0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = store.Settings().UpdateSettings(settings)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
myFields := fields{
|
|
|
|
myFields := fields{
|
|
|
|
userSessionTimeout: "24h",
|
|
|
|
userSessionTimeout: "24h",
|
|
|
|
dataStore: i.NewDatastore(i.WithSettingsService(mySettings)),
|
|
|
|
dataStore: store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
myTokenData := &portainer.TokenData{
|
|
|
|
myTokenData := &portainer.TokenData{
|
|
|
@ -66,6 +76,9 @@ func TestService_GenerateTokenForKubeconfig(t *testing.T) {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, _, _, err = service.ParseAndVerifyToken(got)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
|
|
parsedToken, err := jwt.ParseWithClaims(got, &claims{}, func(token *jwt.Token) (any, error) {
|
|
|
|
parsedToken, err := jwt.ParseWithClaims(got, &claims{}, func(token *jwt.Token) (any, error) {
|
|
|
|
return service.secrets[kubeConfigScope], nil
|
|
|
|
return service.secrets[kubeConfigScope], nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|