Skip to content

Commit 8c76624

Browse files
authored
fix(cli): stackit auth get-access-token does not work as expected (#922)
1 parent 7ba5ffc commit 8c76624

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

internal/cmd/auth/get-access-token/get_access_token.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4444
return &cliErr.SessionExpiredError{}
4545
}
4646

47-
// Try to get a valid access token, refreshing if necessary
48-
accessToken, err := auth.RefreshAccessToken(params.Printer)
47+
accessToken, err := auth.GetValidAccessToken(params.Printer)
4948
if err != nil {
5049
return err
5150
}

internal/pkg/auth/auth.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,22 @@ func getEmailFromToken(token string) (string, error) {
134134
return claims.Email, nil
135135
}
136136

137-
// RefreshAccessToken refreshes the access token if it's expired for the user token flow.
138-
// It returns the new access token or an error if the refresh fails.
139-
func RefreshAccessToken(p *print.Printer) (string, error) {
137+
// GetValidAccessToken returns a valid access token for the current authentication flow.
138+
// For user token flows, it refreshes the token if necessary.
139+
// For service account flows, it returns the current access token.
140+
func GetValidAccessToken(p *print.Printer) (string, error) {
140141
flow, err := GetAuthFlow()
141142
if err != nil {
142143
return "", fmt.Errorf("get authentication flow: %w", err)
143144
}
145+
146+
// For service account flows, just return the current token
147+
if flow == AUTH_FLOW_SERVICE_ACCOUNT_TOKEN || flow == AUTH_FLOW_SERVICE_ACCOUNT_KEY {
148+
return GetAccessToken()
149+
}
150+
144151
if flow != AUTH_FLOW_USER_TOKEN {
145-
return "", fmt.Errorf("token refresh is only supported for user token flow, current flow: %s", flow)
152+
return "", fmt.Errorf("unsupported authentication flow: %s", flow)
146153
}
147154

148155
// Load tokens from storage

0 commit comments

Comments
 (0)