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)
| 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. |
| 714 | func (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. |
no test coverage detected