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

Method refreshableForUser

internal/config/refreshable.go:233–258  ·  view source on GitHub ↗

refreshableForUser returns the refreshable credential stored for the user along with whether it came from secure storage. It prefers the secure keyring and falls back to the insecure configuration record, returning errRefreshableNotFound when neither exists.

(hostname, username string)

Source from the content-addressed store, hash-verified

231// storage. It prefers the secure keyring and falls back to the insecure configuration record, returning
232// errRefreshableNotFound when neither exists.
233func (c *AuthConfig) refreshableForUser(hostname, username string) (gh.Credential, bool, error) {
234 // Consult the secure keyring first, matching TokenForUser. The two storage forms are mutually exclusive, so this
235 // never changes which credential is returned in practice, but keeping the precedence identical avoids confusing
236 // bugs if a misconfiguration ever left records in both places. Preferring the keyring also means the refresh
237 // tokens survive a corrupted or tampered config file.
238 credential, err := refreshableFromKeyring(hostname, username)
239 if err == nil {
240 return credential, true, nil
241 }
242 if !errors.Is(err, errRefreshableNotFound) {
243 return gh.Credential{}, true, err
244 }
245
246 // The keyring holds no record, so fall back to the insecure configuration.
247 path := refreshablePath(hostname, username)
248 if _, err := c.cfg.Get(path); err != nil {
249 // A missing config file yields an empty config, so c.cfg.Get returns a KeyNotFoundError rather than a real
250 // read failure. Treat that as "no record anywhere" and report it as not found.
251 if isKeyNotFound(err) {
252 return gh.Credential{}, false, errRefreshableNotFound
253 }
254 return gh.Credential{}, false, err
255 }
256 credential, err = c.refreshableFromConfig(path)
257 return credential, false, err
258}
259
260func (c *AuthConfig) refreshableFromConfig(path []string) (gh.Credential, error) {
261 var record refreshableTokenRecord

Callers 7

setActiveCredentialMethod · 0.95
TokenForUserMethod · 0.95
TestRefreshableForUserFunction · 0.80
TestLoginRefreshableFunction · 0.80

Calls 5

refreshableFromConfigMethod · 0.95
refreshableFromKeyringFunction · 0.85
refreshablePathFunction · 0.85
isKeyNotFoundFunction · 0.85
GetMethod · 0.65

Tested by 3

TestRefreshableForUserFunction · 0.64
TestLoginRefreshableFunction · 0.64