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

Method TokenForUser

internal/config/config.go:714–730  ·  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

712// setActiveCredential so activation and resolution always agree on which credential is current, even in the mixed
713// state that should never occur because logging in with one kind removes the other.
714func (c *AuthConfig) TokenForUser(hostname, user string) (gh.Credential, error) {
715 if token, err := keyring.Get(keyringServiceName(hostname), user); err == nil {
716 return gh.Credential{Source: gh.TokenSourceKeyring, Token: token}, nil
717 }
718
719 if credential, _, err := c.refreshableForUser(hostname, user); err == nil {
720 return credential, nil
721 } else if !errors.Is(err, errRefreshableNotFound) {
722 return gh.Credential{}, err
723 }
724
725 if token, err := c.cfg.Get([]string{hostsKey, hostname, usersKey, user, oauthTokenKey}); err == nil {
726 return gh.Credential{Source: gh.TokenSourceOAuthToken, Token: token}, nil
727 }
728
729 return gh.Credential{Source: gh.TokenSourceDefault}, fmt.Errorf("no token found for '%s'", user)
730}
731
732// TokenForUserWithRefresh retrieves the credential for a specified user and hostname and, when it is refreshable and
733// its access token is expiring, renews and persists it, returning the credential to use and the refresh outcome.

Callers 2

ActiveTokenMethod · 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