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

Method refreshableForUser

internal/config/refreshable.go:234–259  ·  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

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

Callers 8

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