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)
| 414 | // re-read, token exchange, and write run as one unit under the cross-process refresh lock so concurrent gh processes |
| 415 | // cannot each spend the rotating refresh token. |
| 416 | func (c *AuthConfig) refreshCredentialForUser(hostname, username string, credential gh.Credential) (gh.Credential, gh.RefreshStatus, error) { |
| 417 | if !accessTokenNeedsRefresh(credential, timeNow()) { |
| 418 | return credential, gh.RefreshStatusUnnecessary, nil |
| 419 | } |
| 420 | if c.refresher == nil { |
| 421 | return credential, gh.RefreshStatusInapplicable, nil |
| 422 | } |
| 423 | |
| 424 | result := credential |
| 425 | status := gh.RefreshStatusFailed |
| 426 | err := c.withFreshConfigForRefresh(func(lockErr error) error { |
| 427 | lockHeld := lockErr == nil |
| 428 | // Re-read under the lock: another gh process may have refreshed the credential while we waited for it. |
| 429 | current, secure, err := c.refreshableForUser(hostname, username) |
| 430 | if err != nil { |
| 431 | return err |
| 432 | } |
| 433 | if !accessTokenNeedsRefresh(current, timeNow()) { |
| 434 | result = current |
| 435 | status = gh.RefreshStatusUnnecessary |
| 436 | return nil |
| 437 | } |
| 438 | // A lock file we are not allowed to create means the state directory is not writable, as in a sandbox that |
| 439 | // mounts it read-only. The refreshed credential could then most likely not be persisted either, yet the |
| 440 | // server would already have rotated the refresh token, leaving the stored credential unusable. So do not |
| 441 | // attempt the exchange and keep presenting the current access token. Even if it looks expired, the server |
| 442 | // may still accept it (for example under clock skew), so let the server decide. |
| 443 | if isRefreshLockInaccessible(lockErr) { |
| 444 | result = current |
| 445 | status = gh.RefreshStatusSkipped |
| 446 | c.addWarning(fmt.Sprintf( |
| 447 | "Skipped refreshing the token of account %s on %s because gh's state directory (%s) is not writable. "+ |
| 448 | "The token has expired or is about to expire. To fix this, grant gh write access to that directory. "+ |
| 449 | "If gh is running in a sandbox, you may need to run `gh auth status` outside of it.", |
| 450 | username, hostname, ghConfig.StateDir())) |
| 451 | return fmt.Errorf("skipped token refresh because the state directory is not writable: %w", lockErr) |
| 452 | } |
| 453 | if simulateRefreshTokenExpired(current, timeNow()) { |
| 454 | // Simulate server rejection under the debug override, mirroring the ErrRefreshTokenInvalid |
| 455 | // handling below, including gating destructive cleanup on lockHeld. |
| 456 | result = gh.Credential{} |
| 457 | status = gh.RefreshStatusExpired |
| 458 | if lockHeld { |
| 459 | _ = c.removeCredentialForUser(hostname, username) |
| 460 | } |
| 461 | return gh.ErrRefreshTokenInvalid |
| 462 | } |
| 463 | renewed, err := c.refresher.Refresh(current.RefreshToken, hostname) |
| 464 | if err != nil { |
| 465 | result = current |
| 466 | // A refresh token the server rejects can never succeed on retry, so classify it distinctly from a |
| 467 | // transient failure to let callers advise re-authentication instead of a pointless retry. |
| 468 | if errors.Is(err, gh.ErrRefreshTokenInvalid) { |
| 469 | status = gh.RefreshStatusExpired |
| 470 | // The credential is now useless: we only reach a refresh because the access token is expiring or |
| 471 | // already expired, and the refresh token that could renew it has just been rejected, so no future |
| 472 | // call can revive it. Remove this user's stored credential so subsequent *WithRefresh callers |
| 473 | // resolve no refresh token and stop re-sending a request the server will always reject, which could |
no test coverage detected