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)
| 231 | // storage. It prefers the secure keyring and falls back to the insecure configuration record, returning |
| 232 | // errRefreshableNotFound when neither exists. |
| 233 | func (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 | |
| 260 | func (c *AuthConfig) refreshableFromConfig(path []string) (gh.Credential, error) { |
| 261 | var record refreshableTokenRecord |