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)
| 415 | // re-read, token exchange, and write run as one unit under the cross-process refresh lock so concurrent gh processes |
| 416 | // cannot each spend the rotating refresh token. |
| 417 | func (c *AuthConfig) refreshCredentialForUser(hostname, username string, credential gh.Credential) (gh.Credential, gh.RefreshStatus, error) { |
| 418 | if !accessTokenNeedsRefresh(credential, timeNow()) { |
| 419 | return credential, gh.RefreshStatusUnnecessary, nil |
| 420 | } |
| 421 | if c.refresher == nil { |
| 422 | return credential, gh.RefreshStatusInapplicable, nil |
| 423 | } |
| 424 | |
| 425 | result := credential |
| 426 | status := gh.RefreshStatusFailed |
| 427 | |
| 428 | attempt := refreshAttempt{ |
| 429 | outcome: refreshOutcomeIncomplete, |
| 430 | lock: refreshLockStateUnknown, |
| 431 | storage: refreshStorageUnknown, |
| 432 | } |
| 433 | defer func() { c.recordRefreshAttempt(hostname, attempt) }() |
| 434 | started := time.Now() |
| 435 | |
| 436 | err := c.withFreshConfigForRefresh(func(lockErr error) error { |
| 437 | attempt.lockWait = time.Since(started) |
| 438 | attempt.lock = refreshLockState(lockErr) |
| 439 | lockHeld := lockErr == nil |
| 440 | // Re-read under the lock: another gh process may have refreshed the credential while we waited for it. |
| 441 | current, secure, err := c.refreshableForUser(hostname, username) |
| 442 | if err != nil { |
| 443 | attempt.outcome = refreshOutcomeReadFailed |
| 444 | return err |
| 445 | } |
| 446 | attempt.storage = refreshStorageType(secure) |
| 447 | if !accessTokenNeedsRefresh(current, timeNow()) { |
| 448 | attempt.outcome = refreshOutcomeAlreadyFresh |
| 449 | result = current |
| 450 | status = gh.RefreshStatusUnnecessary |
| 451 | return nil |
| 452 | } |
| 453 | // A lock file we are not allowed to create means the state directory is not writable, as in a sandbox that |
| 454 | // mounts it read-only. The refreshed credential could then most likely not be persisted either, yet the |
| 455 | // server would already have rotated the refresh token, leaving the stored credential unusable. So do not |
| 456 | // attempt the exchange and keep presenting the current access token. Even if it looks expired, the server |
| 457 | // may still accept it (for example under clock skew), so let the server decide. |
| 458 | if isRefreshLockInaccessible(lockErr) { |
| 459 | attempt.outcome = refreshOutcomeSkipped |
| 460 | result = current |
| 461 | status = gh.RefreshStatusSkipped |
| 462 | c.addWarning(fmt.Sprintf( |
| 463 | "Skipped refreshing the token of account %s on %s because gh's state directory (%s) is not writable. "+ |
| 464 | "The token has expired or is about to expire. To fix this, grant gh write access to that directory. "+ |
| 465 | "If gh is running in a sandbox, you may need to run `gh auth status` outside of it.", |
| 466 | username, hostname, ghConfig.StateDir())) |
| 467 | return fmt.Errorf("skipped token refresh because the state directory is not writable: %w", lockErr) |
| 468 | } |
| 469 | if simulateRefreshTokenExpired(current, timeNow()) { |
| 470 | // Simulate server rejection under the debug override, mirroring the ErrRefreshTokenInvalid |
| 471 | // handling below, including gating destructive cleanup on lockHeld. |
| 472 | result = gh.Credential{} |
| 473 | status = gh.RefreshStatusExpired |
| 474 | attempt.outcome = refreshOutcomeRejectedKept |
no test coverage detected