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

Method TokenForUser

internal/config/config.go:763–779  ·  view source on GitHub ↗

TokenForUser retrieves the credential for the given user and hostname. Like ActiveToken it returns the last stored token as-is, including the current access token of a refreshable credential, and never refreshes it; use TokenForUserWithRefresh when the token must be valid for API calls. It checks t

(hostname, user string)

Source from the content-addressed store, hash-verified

761// setActiveCredential so activation and resolution always agree on which credential is current, even in the mixed
762// state that should never occur because logging in with one kind removes the other.
763func (c *AuthConfig) TokenForUser(hostname, user string) (gh.Credential, error) {
764 if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil {
765 return gh.Credential{Source: gh.TokenSourceKeyring, Token: token}, nil
766 }
767
768 if credential, _, err := c.refreshableForUser(hostname, user); err == nil {
769 return credential, nil
770 } else if !errors.Is(err, errRefreshableNotFound) {
771 return gh.Credential{}, err
772 }
773
774 if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil {
775 return gh.Credential{Source: gh.TokenSourceOAuthToken, Token: token}, nil
776 }
777
778 return gh.Credential{Source: gh.TokenSourceDefault}, fmt.Errorf("no token found for '%s'", user)
779}
780
781// TokenForUserWithRefresh retrieves the credential for a specified user and hostname and, when it is refreshable and
782// its access token is expiring, renews and persists it, returning the credential to use and the refresh outcome.

Callers 3

ActiveTokenMethod · 0.95
LogoutWithCredentialMethod · 0.95

Calls 5

refreshableForUserMethod · 0.95
GetFunction · 0.92
keyringServiceNameFunction · 0.70
GetMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected