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