Skip to content

Stop presenting a rejected refresh token - #328

Merged
masnwilliams merged 4 commits into
mainfrom
hypeship/cli-dead-refresh-token
Nov 23, 2026
Merged

masnwilliams merged 4 commits into
mainfrom
hypeship/cli-dead-refresh-token

Conversation

@masnwilliams

@masnwilliams masnwilliams commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Rejected refresh tokens are cleared. When the auth server answers a refresh with 400 invalid_grant, the token is revoked or expired for good. Today the CLI keeps it, so every later command presents it again and fails the same way. RefreshTokens now returns errRefreshTokenRejected for exactly that response. While still holding the credentials lock, refreshStoredTokens removes the refresh token from the saved credentials. It does this only when the saved token is still the one that was rejected, so credentials another process saved in the meantime are left alone. With an expired access token and no refresh token, GetAuthenticatedClient and refreshStoredTokens now return session ended, please run 'kernel login' to sign in again without contacting the server. Previously the CLI sent the expired access token to the API. This also covers a command that was waiting on the lock while another cleared the token. kernel login already accepts expired credentials without --force.

Other failures still keep the refresh token and are retried by the next command: network errors, 5xx responses, 401, and other 400 codes.

The early-refresh window stays at 5 minutes. The CLI sets the access token once per command, so a shorter window would let long multi-request commands (for example kernel audit-logs download) start with less token lifetime left. Refreshing per request is a separate follow-up.

Tests

  • TestRejectedRefreshTokenIsNotPresentedAgain: an invalid_grant clears the refresh token and keeps the rest of the credentials. Three later commands return errSessionEnded (with "kernel login" in the message), and the server sees exactly one request.
  • TestConcurrentCommandsStopAfterRejection and TestRefreshAfterTheTokenWasClearedEndsTheSession: commands waiting on the lock stop instead of using the expired access token.
  • TestFailedRefreshKeepsTheRefreshToken: 500, 503, 400 invalid_request and 401 invalid_client keep the token, and the next command retries.
  • TestForgetRefreshTokenKeepsReplacedCredentials: a rejection doesn't clear credentials that were already replaced.
  • TestIsExpiredRefreshesFiveMinutesEarly.
  • go vet ./... passes (and on Windows for pkg/auth and cmd), as do go test ./... and go test -race ./pkg/auth.

Note

Medium Risk
Changes authentication and persisted credential handling; mistakes could log users out incorrectly or leave bad tokens in use, but scope is limited to CLI OAuth refresh paths and is well covered by new tests.

Overview
When OAuth refresh fails with 400 invalid_grant, RefreshTokens now surfaces errRefreshTokenRejected. While holding the credentials lock, refreshStoredTokens calls forgetRefreshToken to wipe only that refresh token from storage (other fields and credentials replaced by another process stay intact). Transient failures (5xx, other 4xx, network) still leave the refresh token in place for the next attempt.

If the access token is expired and there is no refresh token, GetAuthenticatedClient and refreshStoredTokens return errSessionEnded with a prompt to run kernel login, instead of sending an expired access token or hitting the token endpoint again.

A new pkg/auth/client_test.go covers rejection clearing, concurrent CLI processes on the credentials lock, retry vs non-retry failures, and forgetRefreshToken behavior.

Reviewed by Cursor Bugbot for commit 17aa56f. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit dbf9903. Configure here.

Comment thread pkg/auth/client.go Resolved

@yummybomb yummybomb 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.

no blockers, approving. go test -race ./pkg/auth passes on 52db437.

non-blocking (5m -> 1m refresh window):

  • pkg/auth/storage.go:42 — shortening refreshBefore from 5m to 1m drops the minimum remaining token lifetime at command start from 5m to 1m. because cmd/root.go:150 calls GetAuthenticatedClient once with a static Authorization header, multi-request commands like kernel audit-logs download are more likely to hit a 401 on a later chunk (retryableAuditLogsChunkError does not retry 401s, and the deferred cleanup deletes the partial file). this can already happen on main when a run outlives the token, so it is fine as a follow-up, but worth refreshing per request in middleware instead of relying on the startup buffer.

@masnwilliams masnwilliams changed the title Stop presenting a rejected refresh token and refresh one minute before expiry Stop presenting a rejected refresh token Nov 23, 2026
@masnwilliams

Copy link
Copy Markdown
Contributor Author

@yummybomb thanks. The refresh window is back to 5 minutes in 17aa56f (pkg/auth/storage.go now matches main, and the test checks the 5-minute boundary), so long multi-request commands start with the same token lifetime as before. Refreshing the token per request in SDK middleware is a separate follow-up, not part of this PR. The invalid_grant handling is unchanged.

@masnwilliams
masnwilliams merged commit 1012ebe into main Nov 23, 2026
8 checks passed
@masnwilliams
masnwilliams deleted the hypeship/cli-dead-refresh-token branch November 23, 2026 21:12
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.

2 participants