Skip to content

Warn when an expiring token cannot be refreshed - #15213

Merged
babakks merged 6 commits into
trunkfrom
babakks/refresh-reject-warning
Dec 3, 2026
Merged

babakks merged 6 commits into
trunkfrom
babakks/refresh-reject-warning

Conversation

@babakks

@babakks babakks commented Dec 2, 2026

Copy link
Copy Markdown
Member

Description

When gh is signed in with an expiring OAuth token, it refreshes the token before using it if the token has expired or is about to. Callers of the refreshing token methods (the HTTP transport, the git credential helper, gh auth token, gh auth status, and others) drop the error a failed refresh returns, so the failure itself never reaches the user. All they see is what happens next: usually a generic HTTP 401: Bad credentials from the stale token, or, when the refresh token was rejected, requests that go out without a token at all.

#15178 added a way to record warnings during a refresh, which ghcmd prints to stderr at the end of every command, and used it for the skipped refresh, persist failure, and failed removal cases. This PR records a warning for the remaining failure outcomes, so the user (or a calling agent) can see why authentication is failing and what to do about it:

  • The refresh token was rejected and gh removed the stored credential. The warning asks the user to sign in again.
  • The token exchange failed for any reason other than a rejected refresh token. The warning includes the error, which carries the server's error message when there is one.
  • The stored credential could not be re-read under the refresh lock, for example because the stored record is corrupt.
  • The refresh token was rejected while gh did not hold the refresh lock, so the stored credential was kept because another gh process may have renewed it at the same time.
  • The configuration could not be reloaded under the refresh lock.
  • The server's response did not contain a usable credential.

These are added for better discovery and debugging on the user's side. The refresh behavior itself does not change: the same credential is returned, kept, or removed as before.

How did you test this change?

I built gh from trunk and from this branch, and ran each against its own throwaway GH_CONFIG_DIR and XDG_STATE_HOME, with telemetry off and no token environment variables set. Each config held a made-up refreshable credential for a made-up account fakeuser on github.com, stored in hosts.yml with an expires_at in the past, so the first request had to refresh it. The made-up refresh token was rejected by github.com's token endpoint.

Given that config, when I ran gh api user with the trunk build, I saw:

{
  "message": "Requires authentication",
  "documentation_url": "https://docs.github.com/rest",
  "status": "401"
}gh: Requires authentication (HTTP 401)

and hosts.yml afterwards had an empty entry for fakeuser, so the credential had been removed without any explanation.

Given the same config, when I ran gh api user with this branch's build, I saw the same output followed by:

! The refresh token of account fakeuser on github.com was rejected, so gh removed the stored credential. Run `gh auth login -h github.com` to sign in again.

and hosts.yml afterwards was the same as with trunk.

I did not exercise the other outcomes by hand, since they need a failing keyring, config reload, or token endpoint. They are covered by unit tests only.

Key points

  • Each warning is recorded once per process, because addWarning already drops duplicate messages. A command that makes many requests with a failing refresh prints the warning once.
  • When the re-read under the lock finds no credential, no warning is added. That means the credential was removed in the meantime, for example by a rejected refresh in another goroutine or process, which reports the removal itself. An unavailable keyring also reads as a missing credential at that point, so that case stays silent, but it would already have hidden the credential from the caller before the refresh started.
  • The warning for a rejected token kept without the lock is phrased conditionally ("If this persists"), since the rejection may only mean that a peer spent the same refresh token first.
  • The exchange failure, reload failure, and invalid response cases share one message shape through warnRefreshFailed.
  • Warnings are printed after the command returns, so for commands that use a pager they appear on stderr once the pager has exited, below the paged output.

Notes for reviewers

Start at refreshCredentialForUser in internal/config/refreshable.go; each outcome is handled there or in a helper next to it. The commits are split per outcome and can be reviewed one at a time. The tests are cases in the existing TestRefreshTelemetry table in internal/config/refreshable_telemetry_test.go, which asserts the recorded warning for each outcome, plus an assertion in TestRefreshTelemetryRecordsUnfinishedAttempts for the reload failure.

Related: #15178 introduced the warnings mechanism and the first refresh warnings.

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @babakks will read and reply directly.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

babakks and others added 6 commits December 2, 2026 10:30
…refresh

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
…esh lock

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
…resh

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
@babakks
babakks requested a review from a team as a code owner December 2, 2026 11:06
@babakks
babakks requested review from tidy-dev and a balanced review from Copilot December 2, 2026 11:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the expanded token-refresh warnings.

No open issues 🎉

What changed in this PR

Extends the existing token-refresh warning mechanism so users can understand authentication failures and recovery options.

Changes:

  • Adds warnings for rejected tokens, failed exchanges, credential read/reload failures, and invalid responses.
  • Adds test assertions for warnings and for silence when a credential disappears while waiting for the lock.
File Description
internal/​config/​refreshable.go Records warnings for additional token-refresh failure outcomes.
internal/​config/​refreshable_telemetry_test.go Covers new warnings and the missing-credential case.

Review effort: Balanced

@babakks
babakks requested a review from BagToad December 2, 2026 14:04
@babakks
babakks merged commit fb00869 into trunk Dec 3, 2026
19 checks passed
@babakks
babakks deleted the babakks/refresh-reject-warning branch December 3, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants