Skip to content

Commit

Permalink
Rename authCfg.Token to authCfg.ActiveToken and authCfg.SetToken to a…
Browse files Browse the repository at this point in the history
…uthCfg.SetActiveToken
  • Loading branch information
samcoe authored and williammartin committed Dec 6, 2023
1 parent 1a3392a commit 024cb93
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions api/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

type tokenGetter interface {
Token(string) (string, string)
ActiveToken(string) (string, string)
}

type HTTPClientOptions struct {
Expand Down Expand Up @@ -99,7 +99,7 @@ func AddAuthTokenHeader(rt http.RoundTripper, cfg tokenGetter) http.RoundTripper
// If the host has changed during a redirect do not add the authentication token header.
if !redirectHostnameChange {
hostname := ghinstance.NormalizeHostname(getHost(req))
if token, _ := cfg.Token(hostname); token != "" {
if token, _ := cfg.ActiveToken(hostname); token != "" {
req.Header.Set(authorization, fmt.Sprintf("token %s", token))
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestHTTPClientSanitizeControlCharactersC1(t *testing.T) {

type tinyConfig map[string]string

func (c tinyConfig) Token(host string) (string, string) {
func (c tinyConfig) ActiveToken(host string) (string, string) {
return c[fmt.Sprintf("%s:%s", host, "oauth_token")], "oauth_token"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/authflow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type cfg struct {
token string
}

func (c cfg) Token(hostname string) (string, string) {
func (c cfg) ActiveToken(hostname string) (string, string) {
return c.token, "oauth_token"
}

Expand Down
14 changes: 7 additions & 7 deletions internal/config/auth_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestTokenStoredInConfig(t *testing.T) {
require.NoError(t, err)

// When we get the token
token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")

// Then the token is successfully fetched
// and the source is set to oauth_token but this isn't great:
Expand All @@ -74,7 +74,7 @@ func TestTokenStoredInEnv(t *testing.T) {
t.Setenv("GH_TOKEN", "test-token")

// When we get the token
token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")

// Then the token is successfully fetched
// and the source is set to the name of the env var
Expand All @@ -89,7 +89,7 @@ func TestTokenStoredInKeyring(t *testing.T) {
require.NoError(t, err)

// When we get the token
token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")

// Then the token is successfully fetched
// and the source is set to keyring
Expand Down Expand Up @@ -454,7 +454,7 @@ func TestSwitchUserMakesInsecureTokenActive(t *testing.T) {
require.NoError(t, authCfg.SwitchUser("github.com", "test-user-1"))

// Their insecure token is now active
token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")
require.Equal(t, "test-token-1", token)
require.Equal(t, oauthTokenKey, source)
}
Expand Down Expand Up @@ -513,7 +513,7 @@ func TestSwitchUserErrorsAndRestoresUserAndInsecureConfigUnderFailure(t *testing
require.NoError(t, err)
require.Equal(t, "test-user-2", activeUser)

token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")
require.Equal(t, "test-token-2", token)
require.Equal(t, "oauth_token", source)
}
Expand All @@ -539,7 +539,7 @@ func TestSwitchUserErrorsAndRestoresUserAndKeyringUnderFailure(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "test-user-2", activeUser)

token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")
require.Equal(t, "test-token-2", token)
require.Equal(t, "keyring", source)
}
Expand Down Expand Up @@ -740,7 +740,7 @@ func TestTokenWorksRightAfterMigration(t *testing.T) {
require.NoError(t, c.Migrate(m))

// Then we can still get the token correctly
token, source := authCfg.Token("github.com")
token, source := authCfg.ActiveToken("github.com")
require.Equal(t, "test-token", token)
require.Equal(t, oauthTokenKey, source)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ type AuthConfig struct {
tokenOverride func(string) (string, string)
}

// Token will retrieve the auth token for the given hostname,
// ActiveToken will retrieve the active auth token for the given hostname,
// searching environment variables, plain text config, and
// lastly encrypted storage.
func (c *AuthConfig) Token(hostname string) (string, string) {
func (c *AuthConfig) ActiveToken(hostname string) (string, string) {
if c.tokenOverride != nil {
return c.tokenOverride(hostname)
}
Expand Down Expand Up @@ -249,9 +249,9 @@ func (c *AuthConfig) HasEnvToken() bool {
return token != ""
}

// SetToken will override any token resolution and return the given
// token and source for all calls to Token. Use for testing purposes only.
func (c *AuthConfig) SetToken(token, source string) {
// SetActiveToken will override any token resolution and return the given
// token and source for all calls to ActiveToken. Use for testing purposes only.
func (c *AuthConfig) SetActiveToken(token, source string) {
c.tokenOverride = func(_ string) (string, string) {
return token, source
}
Expand Down Expand Up @@ -358,7 +358,7 @@ func (c *AuthConfig) SwitchUser(hostname, user string) error {
return fmt.Errorf("failed to get active user: %s", err)
}

previouslyActiveToken, previousSource := c.Token(hostname)
previouslyActiveToken, previousSource := c.ActiveToken(hostname)
if previousSource != "keyring" && previousSource != "oauth_token" {
return fmt.Errorf("currently active token for %s is from %s", hostname, previousSource)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/auth/gitcredential/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
const tokenUser = "x-access-token"

type config interface {
Token(string) (string, string)
ActiveToken(string) (string, string)
ActiveUser(string) (string, error)
}

Expand Down Expand Up @@ -113,10 +113,10 @@ func helperRun(opts *CredentialOptions) error {

lookupHost := wants["host"]
var gotUser string
gotToken, source := cfg.Token(lookupHost)
gotToken, source := cfg.ActiveToken(lookupHost)
if gotToken == "" && strings.HasPrefix(lookupHost, "gist.") {
lookupHost = strings.TrimPrefix(lookupHost, "gist.")
gotToken, source = cfg.Token(lookupHost)
gotToken, source = cfg.ActiveToken(lookupHost)
}

if strings.HasSuffix(source, "_TOKEN") {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/gitcredential/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type tinyConfig map[string]string

func (c tinyConfig) Token(host string) (string, string) {
func (c tinyConfig) ActiveToken(host string) (string, string) {
return c[fmt.Sprintf("%s:%s", host, "oauth_token")], c["_source"]
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/auth/logout/logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func hasNoToken(hostname string) tokenAssertion {
return func(t *testing.T, cfg config.Config) {
t.Helper()

token, _ := cfg.Authentication().Token(hostname)
token, _ := cfg.Authentication().ActiveToken(hostname)
require.Empty(t, token)
}
}
Expand All @@ -564,7 +564,7 @@ func hasActiveToken(hostname string, expectedToken string) tokenAssertion {
return func(t *testing.T, cfg config.Config) {
t.Helper()

token, _ := cfg.Authentication().Token(hostname)
token, _ := cfg.Authentication().ActiveToken(hostname)
require.Equal(t, expectedToken, token)
}
}
4 changes: 2 additions & 2 deletions pkg/cmd/auth/refresh/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func refreshRun(opts *RefreshOptions) error {
additionalScopes := set.NewStringSet()

if !opts.ResetScopes {
if oldToken, _ := authCfg.Token(hostname); oldToken != "" {
if oldToken, _ := authCfg.ActiveToken(hostname); oldToken != "" {
if oldScopes, err := shared.GetScopes(opts.HttpClient, hostname, oldToken); err == nil {
for _, s := range strings.Split(oldScopes, ",") {
s = strings.TrimSpace(s)
Expand Down Expand Up @@ -196,7 +196,7 @@ func refreshRun(opts *RefreshOptions) error {

if credentialFlow.ShouldSetup() {
username, _ := authCfg.ActiveUser(hostname)
password, _ := authCfg.Token(hostname)
password, _ := authCfg.ActiveToken(hostname)
if err := credentialFlow.Setup(hostname, username, password); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/shared/writeable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func AuthTokenWriteable(authCfg *config.AuthConfig, hostname string) (string, bool) {
token, src := authCfg.Token(hostname)
token, src := authCfg.ActiveToken(hostname)
return src, (token == "" || !strings.HasSuffix(src, "_TOKEN"))
}
2 changes: 1 addition & 1 deletion pkg/cmd/auth/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func statusRun(opts *StatusOptions) error {

var activeUser string
gitProtocol := cfg.GitProtocol(hostname)
activeUserToken, activeUserTokenSource := authCfg.Token(hostname)
activeUserToken, activeUserTokenSource := authCfg.ActiveToken(hostname)
if authTokenWriteable(activeUserTokenSource) {
activeUser, _ = authCfg.ActiveUser(hostname)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func tokenRun(opts *TokenOptions) error {
}
} else {
if opts.Username == "" {
val, _ = authCfg.Token(hostname)
val, _ = authCfg.ActiveToken(hostname)
} else {
val, _, _ = authCfg.TokenForUser(hostname, opts.Username)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/config/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCmdConfigGet(f *cmdutil.Factory, runF func(*GetOptions) error) *cobra.Co
func getRun(opts *GetOptions) error {
// search keyring storage when fetching the `oauth_token` value
if opts.Hostname != "" && opts.Key == "oauth_token" {
token, _ := opts.Config.Authentication().Token(opts.Hostname)
token, _ := opts.Config.Authentication().ActiveToken(opts.Hostname)
if token == "" {
return errors.New(`could not find key "oauth_token"`)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/factory/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Test_BaseRepo(t *testing.T) {
hosts = append([]string{tt.override}, hosts...)
}
authCfg.SetHosts(hosts)
authCfg.SetToken("", "")
authCfg.SetActiveToken("", "")
authCfg.SetDefaultHost("nonsense.com", "hosts")
if tt.override != "" {
authCfg.SetDefaultHost(tt.override, "GH_HOST")
Expand Down Expand Up @@ -216,7 +216,7 @@ func Test_SmartBaseRepo(t *testing.T) {
hosts = append([]string{tt.override}, hosts...)
}
authCfg.SetHosts(hosts)
authCfg.SetToken("", "")
authCfg.SetActiveToken("", "")
authCfg.SetDefaultHost("nonsense.com", "hosts")
if tt.override != "" {
authCfg.SetDefaultHost(tt.override, "GH_HOST")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/factory/remote_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func Test_remoteResolver(t *testing.T) {
cfg.AuthenticationFunc = func() *config.AuthConfig {
authCfg := &config.AuthConfig{}
authCfg.SetHosts([]string{"example.com"})
authCfg.SetToken("", "")
authCfg.SetActiveToken("", "")
authCfg.SetDefaultHost("example.com", "hosts")
return authCfg
}
Expand Down Expand Up @@ -151,7 +151,7 @@ func Test_remoteResolver(t *testing.T) {
cfg.AuthenticationFunc = func() *config.AuthConfig {
authCfg := &config.AuthConfig{}
authCfg.SetHosts([]string{"example.com", "github.com"})
authCfg.SetToken("", "")
authCfg.SetActiveToken("", "")
authCfg.SetDefaultHost("example.com", "default")
return authCfg
}
Expand Down

0 comments on commit 024cb93

Please sign in to comment.