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

Method ActiveToken

internal/config/config.go:340–372  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

338// a refreshable credential, and never contacts the token endpoint to refresh it. Callers that need a token valid for
339// API calls should use ActiveTokenWithRefresh.
340func (c *AuthConfig) ActiveToken(hostname string) gh.Credential {
341 if c.tokenOverride != nil {
342 token, source := c.tokenOverride(hostname)
343 return gh.Credential{Source: source, Token: token}
344 }
345
346 token, source := ghauth.TokenFromEnvOrConfig(hostname)
347 if token != "" {
348 return gh.Credential{Source: source, Token: token}
349 }
350
351 // Intentional behavior change from older gh: the active-user lookup goes through TokenForUser, which also consults
352 // the per-user config oauth_token and the refreshable slot, rather than only the per-user keyring slot. This is
353 // groundwork for refreshable credentials and is the more correct result. The only observable difference is when a
354 // user has a per-user config oauth_token (or refreshable record) but no per-user keyring slot while an unkeyed
355 // keyring token also exists: older gh returned the unkeyed keyring token, whereas we now return the per-user
356 // credential. That state is rare because activateUser mirrors a per-user insecure token into the host-level
357 // oauth_token, which TokenFromEnvOrConfig resolves above before we reach here. This is accepted and pinned by
358 // TestActiveTokenPrefersPerUserConfigTokenOverUnkeyedKeyring.
359 if user, err := c.ActiveUser(hostname); err == nil {
360 if credential, err := c.TokenForUser(hostname, user); err == nil {
361 return credential
362 }
363 }
364
365 // We should generally be able to find a token for the active user, but in some cases such as if the keyring was
366 // set up in a very old version of the CLI, it may only have an unkeyed token, so fall back to it.
367 if token, err := c.TokenFromKeyring(hostname); err == nil {
368 return gh.Credential{Source: gh.TokenSourceKeyring, Token: token}
369 }
370
371 return gh.Credential{Source: source, Token: token}
372}
373
374// ActiveTokenWithRefresh retrieves the active credential for the given hostname and, when it is refreshable and its
375// access token is expiring, renews and persists it, returning the credential to use and the refresh outcome.

Callers 4

ActiveTokenTypeMethod · 0.95
HasActiveTokenMethod · 0.95
SwitchUserMethod · 0.95

Calls 3

ActiveUserMethod · 0.95
TokenForUserMethod · 0.95
TokenFromKeyringMethod · 0.95

Tested by

no test coverage detected