diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 07e11d204..d80b5e61f 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -242,14 +242,13 @@ func statusRun(opts *StatusOptions) error { activeUser, _ = authCfg.ActiveUser(hostname) } entry := buildEntry(httpClient, buildEntryOptions{ - active: true, - gitProtocol: gitProtocol, - hostname: hostname, - showToken: showToken, - token: activeUserToken, - tokenSource: activeUserTokenSource, - username: activeUser, - includeScope: opts.includeScope(), + active: true, + gitProtocol: gitProtocol, + hostname: hostname, + showToken: showToken, + token: activeUserToken, + tokenSource: activeUserTokenSource, + username: activeUser, }) statuses[hostname] = append(statuses[hostname], entry) @@ -268,14 +267,13 @@ func statusRun(opts *StatusOptions) error { } token, tokenSource, _ := authCfg.TokenForUser(hostname, username) entry := buildEntry(httpClient, buildEntryOptions{ - active: false, - gitProtocol: gitProtocol, - hostname: hostname, - showToken: showToken, - token: token, - tokenSource: tokenSource, - username: username, - includeScope: opts.includeScope(), + active: false, + gitProtocol: gitProtocol, + hostname: hostname, + showToken: showToken, + token: token, + tokenSource: tokenSource, + username: username, }) statuses[hostname] = append(statuses[hostname], entry) @@ -350,14 +348,13 @@ func expectScopes(token string) bool { } type buildEntryOptions struct { - active bool - gitProtocol string - hostname string - showToken bool - token string - tokenSource string - username string - includeScope bool + active bool + gitProtocol string + hostname string + showToken bool + token string + tokenSource string + username string } func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { @@ -393,21 +390,19 @@ func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { } } - if opts.includeScope { - // Get scopes for token. - scopesHeader, err := shared.GetScopes(httpClient, opts.hostname, opts.token) - if err != nil { - var networkError net.Error - if errors.As(err, &networkError) && networkError.Timeout() { - entry.State = authStateTimeout - return entry - } - - entry.State = authStateError + // Get scopes for token. + scopesHeader, err := shared.GetScopes(httpClient, opts.hostname, opts.token) + if err != nil { + var networkError net.Error + if errors.As(err, &networkError) && networkError.Timeout() { + entry.State = authStateTimeout return entry } - entry.Scopes = scopesHeader + + entry.State = authStateError + return entry } + entry.Scopes = scopesHeader entry.State = authStateSuccess return entry @@ -420,10 +415,3 @@ func authTokenWriteable(src string) bool { func isValidEntry(entry authEntry) bool { return entry.State == authStateSuccess } - -func (opts *StatusOptions) includeScope() bool { - if opts.Exporter == nil { - return true - } - return slices.Contains(opts.Exporter.Fields(), "scopes") -} diff --git a/pkg/cmd/auth/status/status_test.go b/pkg/cmd/auth/status/status_test.go index 126e7fa7e..f9c5fb9dd 100644 --- a/pkg/cmd/auth/status/status_test.go +++ b/pkg/cmd/auth/status/status_test.go @@ -586,6 +586,21 @@ func Test_statusRun(t *testing.T) { login(t, c, "github.com", "monalisa2", "gho_abc123", "https") login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https") }, + httpStubs: func(reg *httpmock.Registry) { + // mock for HeaderHasMinimumScopes api requests to github.com + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + + // mock for HeaderHasMinimumScopes api requests to a non-github.com host + reg.Register( + httpmock.REST("GET", "api/v3/"), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + + }, wantOut: `{` + `"ghe.io":[` + `{"active":true,"host":"ghe.io","login":"monalisa-ghe","state":"success"}` + @@ -606,6 +621,15 @@ func Test_statusRun(t *testing.T) { login(t, c, "github.com", "monalisa2", "gho_abc123", "https") login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https") }, + httpStubs: func(reg *httpmock.Registry) { + // mocks for HeaderHasMinimumScopes api requests to github.com + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + }, wantOut: `{` + `"github.com":[` + `{"active":true,"host":"github.com","login":"monalisa2","state":"success"},` + @@ -623,6 +647,15 @@ func Test_statusRun(t *testing.T) { login(t, c, "github.com", "monalisa2", "gho_abc123", "https") login(t, c, "ghe.io", "monalisa-ghe", "gho_abc123", "https") }, + httpStubs: func(reg *httpmock.Registry) { + // mocks for HeaderHasMinimumScopes api requests to github.com + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + reg.Register( + httpmock.REST("GET", "api/v3/"), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + }, wantOut: `{` + `"ghe.io":[` + `{"active":true,"host":"ghe.io","login":"monalisa-ghe","state":"success"}` + @@ -671,6 +704,12 @@ func Test_statusRun(t *testing.T) { cfgStubs: func(t *testing.T, c gh.Config) { login(t, c, "github.com", "monalisa", "abc123", "https") }, + httpStubs: func(reg *httpmock.Registry) { + // mocks for HeaderHasMinimumScopes api requests to github.com + reg.Register( + httpmock.REST("GET", ""), + httpmock.WithHeader(httpmock.ScopesResponder("repo,read:org"), "X-Oauth-Scopes", "repo, read:org")) + }, wantOut: `{"github.com":[{"active":true,"host":"github.com","login":"monalisa","state":"success","token":"abc123"}]}` + "\n", }, }