Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cmd/project/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/cmd/project/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ func TestParseInput(t *testing.T) {

func TestBuildRequest(t *testing.T) {
keyring.MockInit()
err := auth.SetAuthField(auth.USER_EMAIL, "[email protected]")
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, "[email protected]")
if err != nil {
t.Fatalf("Failed to set auth user email: %v", err)
}
Expand Down
25 changes: 23 additions & 2 deletions internal/pkg/auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"

"os"
"path/filepath"

Expand All @@ -23,7 +22,6 @@ type AuthFlow string

const (
keyringService = "stackit-cli"
textFileFolderName = "stackit"
textFileName = "cli-auth-storage.txt"
envAccessTokenName = "STACKIT_ACCESS_TOKEN"
)
Expand Down Expand Up @@ -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,
Expand Down