Skip to content

Commit 5dcca3b

Browse files
authored
fix: project list command does not work with service account (#691)
- fix: project list command does not work with service account - add GetAuthEmail() function to get the email of the authenticated user
1 parent 69e408c commit 5dcca3b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

internal/cmd/project/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient resourceMana
167167
}
168168

169169
if model.ParentId == nil && model.ProjectIdLike == nil && model.Member == nil {
170-
email, err := auth.GetAuthField(auth.USER_EMAIL)
170+
email, err := auth.GetAuthEmail()
171171
if err != nil {
172172
return req, fmt.Errorf("get email of authenticated user: %w", err)
173173
}

internal/cmd/project/list/list_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ func TestParseInput(t *testing.T) {
259259

260260
func TestBuildRequest(t *testing.T) {
261261
keyring.MockInit()
262-
err := auth.SetAuthField(auth.USER_EMAIL, "[email protected]")
262+
err := auth.SetAuthFlow(auth.AUTH_FLOW_USER_TOKEN)
263+
if err != nil {
264+
t.Fatalf("Failed to set auth flow: %v", err)
265+
}
266+
267+
err = auth.SetAuthField(auth.USER_EMAIL, "[email protected]")
263268
if err != nil {
264269
t.Fatalf("Failed to set auth user email: %v", err)
265270
}

internal/pkg/auth/storage.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8-
98
"os"
109
"path/filepath"
1110

@@ -23,7 +22,6 @@ type AuthFlow string
2322

2423
const (
2524
keyringService = "stackit-cli"
26-
textFileFolderName = "stackit"
2725
textFileName = "cli-auth-storage.txt"
2826
envAccessTokenName = "STACKIT_ACCESS_TOKEN"
2927
)
@@ -342,6 +340,29 @@ func GetProfileEmail(profile string) string {
342340
return email
343341
}
344342

343+
// GetAuthEmail returns the email of the authenticated account.
344+
// If the environment variable STACKIT_ACCESS_TOKEN is set, the email of this token will be returned.
345+
func GetAuthEmail() (string, error) {
346+
// If STACKIT_ACCESS_TOKEN is set, get the mail from the token
347+
if accessToken := os.Getenv(envAccessTokenName); accessToken != "" {
348+
email, err := getEmailFromToken(accessToken)
349+
if err != nil {
350+
return "", fmt.Errorf("error getting email from token: %w", err)
351+
}
352+
return email, nil
353+
}
354+
355+
profile, err := config.GetProfile()
356+
if err != nil {
357+
return "", fmt.Errorf("error getting profile: %w", err)
358+
}
359+
email := GetProfileEmail(profile)
360+
if email == "" {
361+
return "", fmt.Errorf("error getting profile email. email is empty")
362+
}
363+
return email, nil
364+
}
365+
345366
func LoginUser(email, accessToken, refreshToken, sessionExpiresAtUnix string) error {
346367
authFields := map[authFieldKey]string{
347368
SESSION_EXPIRES_AT_UNIX: sessionExpiresAtUnix,

0 commit comments

Comments
 (0)