From a53b5cd1b4bf7a67f8cdb5ffe5fe34bbd62541e7 Mon Sep 17 00:00:00 2001 From: Marcel Jacek Date: Mon, 7 Apr 2025 13:50:58 +0200 Subject: [PATCH] add GetAuthEmail() function, to get the email of the authenticated user or service account - fix: project list command does not work with service account - it uses the GetAuthEmail() function to get correct email --- internal/cmd/project/list/list.go | 2 +- internal/cmd/project/list/list_test.go | 7 ++++++- internal/pkg/auth/storage.go | 25 +++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/internal/cmd/project/list/list.go b/internal/cmd/project/list/list.go index 9051dc145..4459558ad 100644 --- a/internal/cmd/project/list/list.go +++ b/internal/cmd/project/list/list.go @@ -167,7 +167,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient resourceMana } if model.ParentId == nil && model.ProjectIdLike == nil && model.Member == nil { - email, err := auth.GetAuthField(auth.USER_EMAIL) + email, err := auth.GetAuthEmail() if err != nil { return req, fmt.Errorf("get email of authenticated user: %w", err) } diff --git a/internal/cmd/project/list/list_test.go b/internal/cmd/project/list/list_test.go index 7c6e73cee..47db45bac 100644 --- a/internal/cmd/project/list/list_test.go +++ b/internal/cmd/project/list/list_test.go @@ -259,7 +259,12 @@ func TestParseInput(t *testing.T) { func TestBuildRequest(t *testing.T) { keyring.MockInit() - err := auth.SetAuthField(auth.USER_EMAIL, "test@test.com") + err := auth.SetAuthFlow(auth.AUTH_FLOW_USER_TOKEN) + if err != nil { + t.Fatalf("Failed to set auth flow: %v", err) + } + + err = auth.SetAuthField(auth.USER_EMAIL, "test@test.com") if err != nil { t.Fatalf("Failed to set auth user email: %v", err) } diff --git a/internal/pkg/auth/storage.go b/internal/pkg/auth/storage.go index 7b6901424..5e857f6a7 100644 --- a/internal/pkg/auth/storage.go +++ b/internal/pkg/auth/storage.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "os" "path/filepath" @@ -23,7 +22,6 @@ type AuthFlow string const ( keyringService = "stackit-cli" - textFileFolderName = "stackit" textFileName = "cli-auth-storage.txt" envAccessTokenName = "STACKIT_ACCESS_TOKEN" ) @@ -342,6 +340,29 @@ func GetProfileEmail(profile string) string { return email } +// GetAuthEmail returns the email of the authenticated account. +// If the environment variable STACKIT_ACCESS_TOKEN is set, the email of this token will be returned. +func GetAuthEmail() (string, error) { + // If STACKIT_ACCESS_TOKEN is set, get the mail from the token + if accessToken := os.Getenv(envAccessTokenName); accessToken != "" { + email, err := getEmailFromToken(accessToken) + if err != nil { + return "", fmt.Errorf("error getting email from token: %w", err) + } + return email, nil + } + + profile, err := config.GetProfile() + if err != nil { + return "", fmt.Errorf("error getting profile: %w", err) + } + email := GetProfileEmail(profile) + if email == "" { + return "", fmt.Errorf("error getting profile email. email is empty") + } + return email, nil +} + func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) error { authFields := map[authFieldKey]string{ SESSION_EXPIRES_AT_UNIX: sessionExpiresAtUnix,