Skip to content

fix(gh auth status): explain expired expiring tokens with --no-refresh - #14815

Merged
BagToad merged 2 commits into
trunkfrom
babakks/auth-status-expired-hint
Oct 26, 2026
Merged

BagToad merged 2 commits into
trunkfrom
babakks/auth-status-expired-hint

Conversation

@babakks

@babakks babakks commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #14814

Description

gh auth status refreshes expiring OAuth tokens (gh auth login --expiring) automatically, unless --no-refresh is given. With --no-refresh, an access token past its expiry time is sent as is to the scope check, the API answers 401, and the entry falls into the generic error branch. The output then claims the token is invalid and suggests gh auth refresh, which runs the full interactive OAuth flow again. In reality the token has only expired, and dropping --no-refresh (or running any other gh command) renews it.

This change detects that case and explains it instead:

Before:

github.com
  X Failed to log in to github.com account monalisa (keyring)
  - Active account: true
  - The token in keyring is invalid.
  - To re-authenticate, run: gh auth refresh -h github.com
  - To forget about this account, run: gh auth logout -h github.com -u monalisa

After:

github.com
  X Token for github.com account monalisa (keyring) has expired
  - Active account: true
  - Expiring token that gh refreshes automatically
  - Token expires: 2026-10-24T17:50:55Z (expired about 1 hour ago)
  - Refresh token expires: 2027-04-24T09:50:54Z (in about 6 months)
  - To refresh it, run gh auth status (without --no-refresh)

If the refresh token has also expired, the last line suggests gh auth login -h <host> instead, since a refresh can no longer succeed.

How did you test this change?

With an expiring token whose access token had already expired, I ran gh auth status --no-refresh using a binary built from this branch and saw:

github.com
  X Token for github.com account monalisa (keyring) has expired
  - Active account: true
  - Expiring token that gh refreshes automatically
  - Token expires: 2026-10-25T17:50:58Z (expired about 41 minutes ago)
  - Refresh token expires: 2027-04-25T09:50:58Z (in about 6 months)
  - To refresh it, run gh auth status (without --no-refresh)

Key points

  • The expiry check only runs after the scope check has failed with HTTP 401. A token that the server still accepts (for example, under clock skew) keeps reporting success, a rejected token that has not expired (for example, a revoked one) keeps the existing "invalid" wording, and unrelated failures (for example, HTTP 503 or a network error) keep the generic error output.
  • The detection requires --no-refresh. Without it, an expired token is refreshed before the scope check, and a rejected refresh token already has its own refresh_expired state.
  • The JSON output is unchanged: the entry still has state: "error" and the raw API error. It already carries refreshable and tokenExpiresAt, which is enough for scripts to tell the cause apart.
  • The expiry lines are moved into a small writeExpiry helper so the success and expired outputs render them identically.

Notes for reviewers

All changes are in pkg/cmd/auth/status/status.go: start at the expiredWithoutRefresh assignment in buildEntry, then the new branch at the top of the authEntryStateError case in authEntry.String. The new test cases sit next to the existing --no-refresh cases in status_test.go.

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.

With --no-refresh, an expiring token past its expiry fails the scope check and was reported as invalid, with a hint to run gh auth refresh. Report it as expired instead, show its expiry details, and hint at running gh auth status without --no-refresh, or gh auth login if the refresh token has also expired.

Fixes #14814

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 October 24, 2026 18:51
@babakks
babakks requested review from BagToad and a balanced review from Copilot October 24, 2026 18:51
@babakks babakks changed the title fix(gh auth status): explain expired expiring tokens with --no-refresh fix(gh auth status): explain expired expiring tokens with --no-refresh Oct 24, 2026

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.

🟡 Changes recommended

Unrelated API and transport failures can incorrectly trigger expiry-specific recovery advice.

1 open issue
What changed in this PR

This PR improves gh auth status --no-refresh diagnostics for expired OAuth tokens, addressing #14814 while preserving JSON output and exit behavior.

Changes:

  • Show expiry details and recovery advice for expired tokens when refresh is disabled.
  • Reuse expiry formatting across successful and expired entries.
  • Add tests for expired access tokens, expired refresh tokens, and rejected unexpired tokens.
File Description
pkg/​cmd/​auth/​status/​status.go Adds expiry detection, recovery hints, and shared expiry formatting.
pkg/​cmd/​auth/​status/​status_test.go Covers the new diagnostics and existing invalid-token wording.

Review effort: Balanced

Comment thread pkg/cmd/auth/status/status.go Resolved
Unrelated failures such as HTTP 503 or non-timeout transport errors do not
indicate the token was rejected, so they keep the generic error output
instead of the expiry-specific refresh or login hint.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: ac178b5a-5b62-49bd-9e83-4796c7ddec46
@BagToad
BagToad merged commit 63837fb into trunk Oct 26, 2026
18 checks passed
@BagToad
BagToad deleted the babakks/auth-status-expired-hint branch October 26, 2026 18:02
@anthonioxzxeroz-cpu

Copy link
Copy Markdown

63837fb

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.

gh auth status --no-refresh shows a misleading error for expired tokens

4 participants