refreshCredentialForUser renews the given refreshable credential when its access token is expiring and persists the renewal to the same storage the credential already lives in. The caller supplies the already-resolved credential. It returns the credential to use and the outcome: RefreshStatusUnneces
(hostname, username string, credential gh.Credential)
| 416 | // re-read, token exchange, and write run as one unit under the cross-process refresh lock so concurrent gh processes |
| 417 | // cannot each spend the rotating refresh token. |
| 418 | func (c *AuthConfig) refreshCredentialForUser(hostname, username string, credential gh.Credential) (gh.Credential, gh.RefreshStatus, error) { |
| 419 | if !accessTokenNeedsRefresh(credential, timeNow()) { |
| 420 | return credential, gh.RefreshStatusUnnecessary, nil |
| 421 | } |
| 422 | if c.refresher == nil { |
| 423 | return credential, gh.RefreshStatusInapplicable, nil |
| 424 | } |
| 425 | |
| 426 | result := credential |
| 427 | status := gh.RefreshStatusFailed |
| 428 | |
| 429 | attempt := refreshAttempt{ |
| 430 | outcome: refreshOutcomeIncomplete, |
| 431 | lock: refreshLockStateUnknown, |
| 432 | storage: refreshStorageUnknown, |
| 433 | storageError: refreshStorageErrorNone, |
| 434 | } |
| 435 | defer func() { c.recordRefreshAttempt(hostname, attempt) }() |
| 436 | started := time.Now() |
| 437 | |
| 438 | err := c.withFreshConfigForRefresh(func(lockErr error) error { |
| 439 | attempt.lockWait = time.Since(started) |
| 440 | attempt.lock = refreshLockState(lockErr) |
| 441 | lockHeld := lockErr == nil |
| 442 | // Re-read under the lock: another gh process may have refreshed the credential while we waited for it. |
| 443 | current, secure, err := c.refreshableForUser(hostname, username) |
| 444 | if err != nil { |
| 445 | attempt.outcome = refreshOutcomeReadFailed |
| 446 | // A missing credential means it was removed in the meantime, for example after a rejected refresh token, |
| 447 | // and whatever removed it is responsible for telling the user. An unavailable keyring also reads as |
| 448 | // missing, but it would have hidden the credential from the caller too, so it rarely gets this far. |
| 449 | if !errors.Is(err, errRefreshableNotFound) { |
| 450 | c.addWarning(fmt.Sprintf( |
| 451 | "Could not read the stored token of account %s on %s to refresh it: %v. "+ |
| 452 | "The token has expired or is about to expire, so requests may fail. "+ |
| 453 | "If this persists, run `gh auth login -h %s` to sign in again.", |
| 454 | username, hostname, err, hostname)) |
| 455 | } |
| 456 | return err |
| 457 | } |
| 458 | attempt.storage = refreshStorageType(secure) |
| 459 | if !accessTokenNeedsRefresh(current, timeNow()) { |
| 460 | attempt.outcome = refreshOutcomeAlreadyFresh |
| 461 | result = current |
| 462 | status = gh.RefreshStatusUnnecessary |
| 463 | return nil |
| 464 | } |
| 465 | // A lock file we are not allowed to create means the state directory is not writable, as in a sandbox that |
| 466 | // mounts it read-only. The refreshed credential could then most likely not be persisted either, yet the |
| 467 | // server would already have rotated the refresh token, leaving the stored credential unusable. So do not |
| 468 | // attempt the exchange and keep presenting the current access token. Even if it looks expired, the server |
| 469 | // may still accept it (for example under clock skew), so let the server decide. |
| 470 | if isRefreshLockInaccessible(lockErr) { |
| 471 | attempt.outcome = refreshOutcomeSkipped |
| 472 | result = current |
| 473 | status = gh.RefreshStatusSkipped |
| 474 | c.addWarning(fmt.Sprintf( |
| 475 | "Skipped refreshing the token of account %s on %s because gh's state directory (%s) is not writable. "+ |
no test coverage detected