Skip to content

Commit d87db35

Browse files
committed
add AuthMode to config
- AuthModeAccessToken and AuthModeOAuth - And check for CI and required AuthModeAccessToken
1 parent c6de254 commit d87db35

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

cmd/src/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var (
8282

8383
errConfigMerge = errors.New("when using a configuration file, zero or all environment variables must be set")
8484
errConfigAuthorizationConflict = errors.New("when passing an 'Authorization' additional headers, SRC_ACCESS_TOKEN must never be set")
85+
errCIAccessTokenRequired = errors.New("SRC_ACCESS_TOKEN must be set in CI")
8586
)
8687

8788
// commands contains all registered subcommands.
@@ -122,6 +123,20 @@ type config struct {
122123
ConfigFilePath string
123124
}
124125

126+
type AuthMode int
127+
128+
const (
129+
AuthModeOAuth AuthMode = iota
130+
AuthModeAccessToken
131+
)
132+
133+
func (c *config) AuthMode() AuthMode {
134+
if c.AccessToken != "" {
135+
return AuthModeAccessToken
136+
}
137+
return AuthModeOAuth
138+
}
139+
125140
// apiClient returns an api.Client built from the configuration.
126141
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
127142
opts := api.ClientOpts{
@@ -260,9 +275,18 @@ func readConfig() (*config, error) {
260275

261276
cfg.Endpoint = cleanEndpoint(cfg.Endpoint)
262277

278+
if isCI() && cfg.AccessToken == "" {
279+
return nil, errCIAccessTokenRequired
280+
}
281+
263282
return &cfg, nil
264283
}
265284

285+
func isCI() bool {
286+
value, ok := os.LookupEnv("CI")
287+
return ok && value != ""
288+
}
289+
266290
func cleanEndpoint(urlStr string) string {
267291
return strings.TrimSuffix(urlStr, "/")
268292
}

cmd/src/main_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestReadConfig(t *testing.T) {
3030
tests := []struct {
3131
name string
3232
fileContents *config
33+
envCI string
3334
envToken string
3435
envFooHeader string
3536
envHeaders string
@@ -283,6 +284,24 @@ func TestReadConfig(t *testing.T) {
283284
envHeaders: "Authorization:Bearer",
284285
wantErr: errConfigAuthorizationConflict.Error(),
285286
},
287+
{
288+
name: "CI requires access token",
289+
envCI: "1",
290+
wantErr: errCIAccessTokenRequired.Error(),
291+
},
292+
{
293+
name: "CI allows access token from config file",
294+
envCI: "1",
295+
fileContents: &config{
296+
Endpoint: "https://example.com/",
297+
AccessToken: "deadbeef",
298+
},
299+
want: &config{
300+
Endpoint: "https://example.com",
301+
AccessToken: "deadbeef",
302+
AdditionalHeaders: map[string]string{},
303+
},
304+
},
286305
}
287306

288307
for _, test := range tests {
@@ -297,6 +316,7 @@ func TestReadConfig(t *testing.T) {
297316
setEnv("SRC_ACCESS_TOKEN", test.envToken)
298317
setEnv("SRC_ENDPOINT", test.envEndpoint)
299318
setEnv("SRC_PROXY", test.envProxy)
319+
setEnv("CI", test.envCI)
300320

301321
tmpDir := t.TempDir()
302322
testHomeDir = tmpDir
@@ -345,3 +365,17 @@ func TestReadConfig(t *testing.T) {
345365
})
346366
}
347367
}
368+
369+
func TestConfigAuthMode(t *testing.T) {
370+
t.Run("oauth when access token is empty", func(t *testing.T) {
371+
if got := (&config{}).AuthMode(); got != AuthModeOAuth {
372+
t.Fatalf("AuthMode() = %v, want %v", got, AuthModeOAuth)
373+
}
374+
})
375+
376+
t.Run("access token when configured", func(t *testing.T) {
377+
if got := (&config{AccessToken: "token"}).AuthMode(); got != AuthModeAccessToken {
378+
t.Fatalf("AuthMode() = %v, want %v", got, AuthModeAccessToken)
379+
}
380+
})
381+
}

0 commit comments

Comments
 (0)