Skip to content

Commit ab826c8

Browse files
fix: add access token env checks in existing auth flow
Relates to STACKITTPR-761
1 parent 858442f commit ab826c8

4 files changed

Lines changed: 132 additions & 0 deletions

File tree

internal/pkg/auth/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func UserSessionExpired() (bool, error) {
134134
}
135135

136136
func GetAccessToken() (string, error) {
137+
if accessToken := os.Getenv(envAccessTokenName); accessToken != "" {
138+
return accessToken, nil
139+
}
137140
accessToken, err := GetAuthField(ACCESS_TOKEN)
138141
if err != nil {
139142
return "", fmt.Errorf("get %s: %w", ACCESS_TOKEN, err)

internal/pkg/auth/auth_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,68 @@ func TestInitKeyFlow(t *testing.T) {
335335
})
336336
}
337337
}
338+
339+
func TestGetAccessToken_EnvVar(t *testing.T) {
340+
const envValue = "token-from-env"
341+
const storedValue = "stored-token"
342+
343+
tests := []struct {
344+
description string
345+
envToken string
346+
storedToken string
347+
expectedToken string
348+
isValid bool
349+
}{
350+
{
351+
description: "env var set and no stored token",
352+
envToken: envValue,
353+
expectedToken: envValue,
354+
isValid: true,
355+
},
356+
{
357+
description: "env var set and stored token present",
358+
envToken: envValue,
359+
storedToken: storedValue,
360+
expectedToken: envValue,
361+
isValid: true,
362+
},
363+
{
364+
description: "env var not set and stored token present",
365+
storedToken: storedValue,
366+
expectedToken: storedValue,
367+
isValid: true,
368+
},
369+
{
370+
description: "env var not set and no stored token",
371+
isValid: false,
372+
},
373+
}
374+
375+
for _, tt := range tests {
376+
t.Run(tt.description, func(t *testing.T) {
377+
keyring.MockInit()
378+
if tt.envToken != "" {
379+
t.Setenv(envAccessTokenName, tt.envToken)
380+
}
381+
if tt.storedToken != "" {
382+
if err := SetAuthField(ACCESS_TOKEN, tt.storedToken); err != nil {
383+
t.Fatalf("Failed to set stored token: %v", err)
384+
}
385+
if err := SetAuthFlow(AUTH_FLOW_SERVICE_ACCOUNT_TOKEN); err != nil {
386+
t.Fatalf("Failed to set auth flow: %v", err)
387+
}
388+
}
389+
390+
got, err := GetAccessToken()
391+
if err != nil {
392+
if !tt.isValid {
393+
return
394+
}
395+
t.Fatalf("unexpected error: %v", err)
396+
}
397+
if tt.expectedToken != got {
398+
t.Errorf("expected token %q, got %q", tt.expectedToken, got)
399+
}
400+
})
401+
}
402+
}

internal/pkg/auth/storage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ func GetAuthFieldMap(keyMap map[authFieldKey]string) error {
234234
}
235235

236236
func GetAuthFlow() (AuthFlow, error) {
237+
if accessToken := os.Getenv(envAccessTokenName); accessToken != "" {
238+
return AUTH_FLOW_SERVICE_ACCOUNT_TOKEN, nil
239+
}
237240
value, err := GetAuthField(authFlowType)
238241
return AuthFlow(value), err
239242
}

internal/pkg/auth/storage_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,64 @@ func TestAuthorizeDeauthorizeUserProfileAuth(t *testing.T) {
12161216
})
12171217
}
12181218
}
1219+
1220+
func TestGetAuthFlow_EnvVar(t *testing.T) {
1221+
const envValue = "some-token"
1222+
1223+
tests := []struct {
1224+
description string
1225+
envToken string
1226+
storedFlow AuthFlow
1227+
expectedFlow AuthFlow
1228+
isValid bool
1229+
}{
1230+
{
1231+
description: "env var set and no stored flow",
1232+
envToken: envValue,
1233+
expectedFlow: AUTH_FLOW_SERVICE_ACCOUNT_TOKEN,
1234+
isValid: true,
1235+
},
1236+
{
1237+
description: "env var set and different flow stored",
1238+
envToken: envValue,
1239+
storedFlow: AUTH_FLOW_USER_TOKEN,
1240+
expectedFlow: AUTH_FLOW_SERVICE_ACCOUNT_TOKEN,
1241+
isValid: true,
1242+
},
1243+
{
1244+
description: "env var not set and stored flow present",
1245+
storedFlow: AUTH_FLOW_SERVICE_ACCOUNT_KEY,
1246+
expectedFlow: AUTH_FLOW_SERVICE_ACCOUNT_KEY,
1247+
isValid: true,
1248+
},
1249+
{
1250+
description: "env var not set and no stored flow",
1251+
isValid: false,
1252+
},
1253+
}
1254+
1255+
for _, tt := range tests {
1256+
t.Run(tt.description, func(t *testing.T) {
1257+
keyring.MockInit()
1258+
if tt.envToken != "" {
1259+
t.Setenv(envAccessTokenName, tt.envToken)
1260+
}
1261+
if tt.storedFlow != "" {
1262+
if err := SetAuthFlow(tt.storedFlow); err != nil {
1263+
t.Fatalf("Failed to set stored auth flow: %v", err)
1264+
}
1265+
}
1266+
1267+
got, err := GetAuthFlow()
1268+
if err != nil {
1269+
if !tt.isValid {
1270+
return
1271+
}
1272+
t.Fatalf("unexpected error: %v", err)
1273+
}
1274+
if tt.expectedFlow != got {
1275+
t.Errorf("expected flow %q, got %q", tt.expectedFlow, got)
1276+
}
1277+
})
1278+
}
1279+
}

0 commit comments

Comments
 (0)