Skip to content

Commit

Permalink
Rename authCfg.User to authCfg.ActiveUser
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe authored and williammartin committed Dec 6, 2023
1 parent 8cdbc1a commit 1a3392a
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 22 deletions.
18 changes: 9 additions & 9 deletions internal/config/auth_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestUserNotLoggedIn(t *testing.T) {
authCfg := newTestAuthConfig(t)

// When we get the user
_, err := authCfg.User("github.com")
_, err := authCfg.ActiveUser("github.com")

// Then it returns failure, bubbling the KeyNotFoundError
var keyNotFoundError *ghConfig.KeyNotFoundError
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestLoginSetsUserForProvidedHost(t *testing.T) {
// Then it returns success and the user is set
require.NoError(t, err)

user, err := authCfg.User("github.com")
user, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "test-user", user)
}
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestLogoutOfActiveUserSwitchesUserIfPossible(t *testing.T) {

// Then we return success and the inactive user is now active
require.NoError(t, err)
activeUser, err := authCfg.User("github.com")
activeUser, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "inactive-user", activeUser)

Expand Down Expand Up @@ -400,7 +400,7 @@ func TestLogoutOfInactiveUserDoesNotSwitchUser(t *testing.T) {

// Then we return success and the active user is still active
require.NoError(t, err)
activeUser, err := authCfg.User("github.com")
activeUser, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "active-user", activeUser)
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestSwitchUserUpdatesTheActiveUser(t *testing.T) {
require.NoError(t, authCfg.SwitchUser("github.com", "test-user-1"))

// Then the active user is updated
activeUser, err := authCfg.User("github.com")
activeUser, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "test-user-1", activeUser)
}
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestSwitchUserErrorsAndRestoresUserAndInsecureConfigUnderFailure(t *testing
require.EqualError(t, err, "no token found for test-user-1")

// And restores the previous state
activeUser, err := authCfg.User("github.com")
activeUser, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "test-user-2", activeUser)

Expand All @@ -535,7 +535,7 @@ func TestSwitchUserErrorsAndRestoresUserAndKeyringUnderFailure(t *testing.T) {
require.EqualError(t, err, "no token found for test-user-1")

// And restores the previous state
activeUser, err := authCfg.User("github.com")
activeUser, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "test-user-2", activeUser)

Expand Down Expand Up @@ -673,7 +673,7 @@ func TestUserWorksRightAfterMigration(t *testing.T) {
require.NoError(t, c.Migrate(m))

// Then we can still get the user correctly
user, err := authCfg.User("github.com")
user, err := authCfg.ActiveUser("github.com")
require.NoError(t, err)
require.Equal(t, "test-user", user)
}
Expand Down Expand Up @@ -817,7 +817,7 @@ func TestLoginPostMigrationSetsUser(t *testing.T) {
require.NoError(t, err)

// When we get the user
user, err := authCfg.User("github.com")
user, err := authCfg.ActiveUser("github.com")

// Then it returns success and the user we provided on login
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ func (c *AuthConfig) TokenFromKeyringForUser(hostname, username string) (string,
return keyring.Get(keyringServiceName(hostname), username)
}

// User will retrieve the username for the logged in user at the given hostname.
func (c *AuthConfig) User(hostname string) (string, error) {
// ActiveUser will retrieve the username for the active user at the given hostname.
// This will not be accurate if the oauth token is set from an environment variable.
func (c *AuthConfig) ActiveUser(hostname string) (string, error) {
return c.cfg.Get([]string{hostsKey, hostname, userKey})
}

Expand Down Expand Up @@ -352,7 +353,7 @@ func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secure
}

func (c *AuthConfig) SwitchUser(hostname, user string) error {
previouslyActiveUser, err := c.User(hostname)
previouslyActiveUser, err := c.ActiveUser(hostname)
if err != nil {
return fmt.Errorf("failed to get active user: %s", err)
}
Expand Down Expand Up @@ -403,7 +404,7 @@ func (c *AuthConfig) Logout(hostname, username string) error {
_ = c.cfg.Remove([]string{hostsKey, hostname, usersKey, username})

// This error is ignorable because we already know there is an active user for the host
activeUser, _ := c.User(hostname)
activeUser, _ := c.ActiveUser(hostname)

// If the user we're removing isn't active, then we just write the config
if activeUser != username {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/auth/gitcredential/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const tokenUser = "x-access-token"

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

type CredentialOptions struct {
Expand Down Expand Up @@ -122,7 +122,7 @@ func helperRun(opts *CredentialOptions) error {
if strings.HasSuffix(source, "_TOKEN") {
gotUser = tokenUser
} else {
gotUser, _ = cfg.User(lookupHost)
gotUser, _ = cfg.ActiveUser(lookupHost)
if gotUser == "" {
gotUser = tokenUser
}
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 @@ -14,7 +14,7 @@ func (c tinyConfig) Token(host string) (string, string) {
return c[fmt.Sprintf("%s:%s", host, "oauth_token")], c["_source"]
}

func (c tinyConfig) User(host string) (string, error) {
func (c tinyConfig) ActiveUser(host string) (string, error) {
return c[fmt.Sprintf("%s:%s", host, "user")], nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/auth/logout/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ func logoutRun(opts *LogoutOptions) error {
}

// We can ignore the error here because a host must always have an active user
preLogoutActiveUser, _ := authCfg.User(hostname)
preLogoutActiveUser, _ := authCfg.ActiveUser(hostname)

if err := authCfg.Logout(hostname, username); err != nil {
return err
}

postLogoutActiveUser, _ := authCfg.User(hostname)
postLogoutActiveUser, _ := authCfg.ActiveUser(hostname)
hasSwitchedToNewUser := preLogoutActiveUser != postLogoutActiveUser &&
postLogoutActiveUser != ""

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/refresh/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func refreshRun(opts *RefreshOptions) error {
fmt.Fprintf(opts.IO.ErrOut, "%s Authentication complete.\n", cs.SuccessIcon())

if credentialFlow.ShouldSetup() {
username, _ := authCfg.User(hostname)
username, _ := authCfg.ActiveUser(hostname)
password, _ := authCfg.Token(hostname)
if err := credentialFlow.Setup(hostname, username, password); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func statusRun(opts *StatusOptions) error {
gitProtocol := cfg.GitProtocol(hostname)
activeUserToken, activeUserTokenSource := authCfg.Token(hostname)
if authTokenWriteable(activeUserTokenSource) {
activeUser, _ = authCfg.User(hostname)
activeUser, _ = authCfg.ActiveUser(hostname)
}
entry := buildEntry(httpClient, buildEntryOptions{
active: true,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/switch/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func switchRun(opts *SwitchOptions) error {
if hostname != "" && host != hostname {
continue
}
hostActiveUser, err := authCfg.User(host)
hostActiveUser, err := authCfg.ActiveUser(host)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/auth/switch/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func TestSwitchRun(t *testing.T) {

require.NoError(t, err)

activeUser, err := cfg.Authentication().User(tt.expectedSuccess.switchedHost)
activeUser, err := cfg.Authentication().ActiveUser(tt.expectedSuccess.switchedHost)
require.NoError(t, err)
require.Equal(t, tt.expectedSuccess.activeUser, activeUser)

Expand Down

0 comments on commit 1a3392a

Please sign in to comment.