Skip to content

Commit e5913fe

Browse files
authored
Merge pull request cli#11038 from anuraaga/active-token-user
fix: get token for active user instead of blank if possible
2 parents 2fc1e54 + 0180c7f commit e5913fe

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

internal/config/auth_config_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,62 @@ func TestTokenWorksRightAfterMigration(t *testing.T) {
769769
require.Equal(t, oauthTokenKey, source)
770770
}
771771

772+
func TestTokenPrioritizesActiveUserToken(t *testing.T) {
773+
// Given a keyring where the active slot contains the token from a previous user
774+
authCfg := newTestAuthConfig(t)
775+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "test-token"))
776+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user1", "test-token"))
777+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user2", "test-token2"))
778+
779+
// When no active user is set
780+
authCfg.cfg.Remove([]string{hostsKey, "github.com", userKey})
781+
782+
// And get the token from the auth config
783+
token, source := authCfg.ActiveToken("github.com")
784+
785+
// Then it returns the token from the keyring active slot
786+
require.Equal(t, "keyring", source)
787+
require.Equal(t, "test-token", token)
788+
789+
// When we set the active user to test-user1
790+
authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user1")
791+
792+
// And get the token from the auth config
793+
token, source = authCfg.ActiveToken("github.com")
794+
795+
// Then it returns the token from the active user entry in the keyring
796+
require.Equal(t, "keyring", source)
797+
require.Equal(t, "test-token", token)
798+
799+
// When we set the active user to test-user2
800+
authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user2")
801+
802+
// And get the token from the auth config
803+
token, source = authCfg.ActiveToken("github.com")
804+
805+
// Then it returns the token from the active user entry in the keyring
806+
require.Equal(t, "keyring", source)
807+
require.Equal(t, "test-token2", token)
808+
}
809+
810+
func TestTokenWithActiveUserNotInKeyringFallsBackToBlank(t *testing.T) {
811+
// Given a keyring that contains a token for a host
812+
authCfg := newTestAuthConfig(t)
813+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "", "test-token"))
814+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user1", "test-token1"))
815+
require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user2", "test-token2"))
816+
817+
// When we set the active user to test-user3
818+
authCfg.cfg.Set([]string{hostsKey, "github.com", userKey}, "test-user3")
819+
820+
// And get the token from the auth config
821+
token, source := authCfg.ActiveToken("github.com")
822+
823+
// Then it returns successfully with the fallback token
824+
require.Equal(t, "keyring", source)
825+
require.Equal(t, "test-token", token)
826+
}
827+
772828
func TestLogoutRightAfterMigrationRemovesHost(t *testing.T) {
773829
// Given we have logged in before migration
774830
authCfg := newTestAuthConfig(t)

internal/config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,18 @@ func (c *AuthConfig) ActiveToken(hostname string) (string, string) {
234234
}
235235
token, source := ghauth.TokenFromEnvOrConfig(hostname)
236236
if token == "" {
237+
var user string
237238
var err error
238-
token, err = c.TokenFromKeyring(hostname)
239+
if user, err = c.ActiveUser(hostname); err == nil {
240+
token, err = c.TokenFromKeyringForUser(hostname, user)
241+
}
242+
if err != nil {
243+
// We should generally be able to find a token for the active user,
244+
// but in some cases such as if the keyring was set up in a very old
245+
// version of the CLI, it may only have a unkeyed token, so fallback
246+
// to it.
247+
token, err = c.TokenFromKeyring(hostname)
248+
}
239249
if err == nil {
240250
source = "keyring"
241251
}

pkg/cmd/api/api_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,12 @@ func Test_apiRun_cache(t *testing.T) {
13581358
Config: func() (gh.Config, error) {
13591359
return &ghmock.ConfigMock{
13601360
AuthenticationFunc: func() gh.AuthConfig {
1361-
return &config.AuthConfig{}
1361+
cfg := &config.AuthConfig{}
1362+
// Required because the http client tries to get the active token and otherwise
1363+
// this goes down to to go-gh config and panics. Pretty bad solution, it would
1364+
// be better if this were black box.
1365+
cfg.SetActiveToken("token", "stub")
1366+
return cfg
13621367
},
13631368
// Cached responses are stored in a tempdir that gets automatically cleaned up
13641369
CacheDirFunc: func() string {

0 commit comments

Comments
 (0)