MCPcopy Create free account
hub / github.com/cli/cli / setActiveCredential

Method setActiveCredential

internal/config/config.go:710–743  ·  view source on GitHub ↗

setActiveCredential clears every host level active credential representation and copies the user's stored credential into the active slot, preserving its storage form. It neither sets the active user nor writes the config, so it can also be used to restore state after a failed switch. It reports whe

(hostname, user string)

Source from the content-addressed store, hash-verified

708// config, so it can also be used to restore state after a failed switch. It reports whether a credential was found
709// for the user.
710func (c *AuthConfig) setActiveCredential(hostname, user string) (bool, error) {
711 // Idempotently clear the non-expiring active representations so that no stale token can outrank the credential we
712 // are about to activate. Refreshable credentials have no host-level active copy, so there is nothing to clear for
713 // them; they are resolved per-user through the active user key.
714 _ = keyring.Delete(keyringServiceName(hostname), "")
715 _ = c.cfg.Remove([]string{hostsKey, hostname, oauthTokenKey})
716
717 // The non-expiring keyring token is checked first so the common non-refreshable user does not pay an extra
718 // refreshable-slot keyring miss on every lookup. On a hit, copy it into the host active slot. This keyring-first
719 // order is kept identical to TokenForUser so activation and resolution never disagree about which credential is
720 // current, even in the mixed state that write paths keep unreachable.
721 if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil {
722 if err = keyring.Set(keyringServiceName(hostname), "", token); err != nil {
723 return false, fmt.Errorf("failed to move active token in keyring: %v", err)
724 }
725 return true, nil
726 }
727
728 // Then the managed refreshable credential. It lives only in the per-user slot and is surfaced as active through the
729 // active user key, so confirming it exists is all that activation needs.
730 if _, _, err := c.refreshableForUser(hostname, user); err == nil {
731 return true, nil
732 } else if !errors.Is(err, errRefreshableNotFound) {
733 return false, err
734 }
735
736 // Otherwise fall back to a non-expiring credential in the insecure config.
737 if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil {
738 c.cfg.Set([]string{hostsKey, hostname, oauthTokenKey}, token)
739 return true, nil
740 }
741
742 return false, nil
743}
744
745func (c *AuthConfig) UsersForHost(hostname string) []string {
746 users, err := c.cfg.Keys([]string{hostsKey, hostname, usersKey})

Callers 3

SwitchUserMethod · 0.95
activateUserMethod · 0.95
TestSetActiveCredentialFunction · 0.80

Calls 9

refreshableForUserMethod · 0.95
DeleteFunction · 0.92
GetFunction · 0.92
SetFunction · 0.92
keyringServiceNameFunction · 0.70
RemoveMethod · 0.65
ErrorfMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65

Tested by 1

TestSetActiveCredentialFunction · 0.64