ActiveToken retrieves the active credential for the given hostname, searching environment variables, plain text config, and lastly encrypted storage. It returns the last stored token as-is, including the current access token of a refreshable credential, and never contacts the token endpoint to refre
(hostname string)
| 314 | // a refreshable credential, and never contacts the token endpoint to refresh it. Callers that need a token valid for |
| 315 | // API calls should use ActiveTokenWithRefresh. |
| 316 | func (c *AuthConfig) ActiveToken(hostname string) gh.Credential { |
| 317 | if c.tokenOverride != nil { |
| 318 | token, source := c.tokenOverride(hostname) |
| 319 | return gh.Credential{Source: source, Token: token} |
| 320 | } |
| 321 | |
| 322 | token, source := ghauth.TokenFromEnvOrConfig(hostname) |
| 323 | if token != "" { |
| 324 | return gh.Credential{Source: source, Token: token} |
| 325 | } |
| 326 | |
| 327 | // Intentional behavior change from older gh: the active-user lookup goes through TokenForUser, which also consults |
| 328 | // the per-user config oauth_token and the refreshable slot, rather than only the per-user keyring slot. This is |
| 329 | // groundwork for refreshable credentials and is the more correct result. The only observable difference is when a |
| 330 | // user has a per-user config oauth_token (or refreshable record) but no per-user keyring slot while an unkeyed |
| 331 | // keyring token also exists: older gh returned the unkeyed keyring token, whereas we now return the per-user |
| 332 | // credential. That state is rare because activateUser mirrors a per-user insecure token into the host-level |
| 333 | // oauth_token, which TokenFromEnvOrConfig resolves above before we reach here. This is accepted and pinned by |
| 334 | // TestActiveTokenPrefersPerUserConfigTokenOverUnkeyedKeyring. |
| 335 | if user, err := c.ActiveUser(hostname); err == nil { |
| 336 | if credential, err := c.TokenForUser(hostname, user); err == nil { |
| 337 | return credential |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // We should generally be able to find a token for the active user, but in some cases such as if the keyring was |
| 342 | // set up in a very old version of the CLI, it may only have an unkeyed token, so fall back to it. |
| 343 | if token, err := c.TokenFromKeyring(hostname); err == nil { |
| 344 | return gh.Credential{Source: gh.TokenSourceKeyring, Token: token} |
| 345 | } |
| 346 | |
| 347 | return gh.Credential{Source: source, Token: token} |
| 348 | } |
| 349 | |
| 350 | // ActiveTokenWithRefresh retrieves the active credential for the given hostname and, when it is refreshable and its |
| 351 | // access token is expiring, renews and persists it, returning the credential to use and the refresh outcome. |
no test coverage detected