Skip to content

Persist rotated refresh token after token exchange (AST-195266) - #1622

Merged
cx-sumit-morchhale merged 6 commits into
stagingfrom
feature/persist-rotated-refresh-token
Nov 26, 2026
Merged

cx-sumit-morchhale merged 6 commits into
stagingfrom
feature/persist-rotated-refresh-token

Conversation

@cx-rahul-pidde

@cx-rahul-pidde cx-rahul-pidde commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • getNewToken returned only the access token, so a refresh token rotated by IAM in the refresh_token grant response was discarded (found by Pedro Fernandes on DevAssist 2.3.70). The stored token then keeps its original expiry and can stop working.
  • New getNewTokenInfo returns the full token response. exchangeAPIKey saves a changed refresh token through the new Resolver.Rotate.
  • Rotate reports whether it wrote. It does nothing when the credential comes from --apikey or CX_APIKEY (those win on resolve). It writes only while the stored value is still the token that was exchanged, so a newer login is not replaced by a stale token.
    • Config-file storage (CX_KEYRING_MODE=disabled): the compare and write are atomic under the config file lock (configfile.SetKeyIfEqual).
    • Keyring: there is no compare-and-set, so this is best effort; a login landing between the read and the write could still be overwritten.
    • auto mode with the keyring unavailable: the rotated token is written to the config file, but only if the file already holds the old token (no new plaintext entry is ever created).
  • A failed save never fails the command. It prints a warning to stderr without --debug: "Warning: could not save the refreshed credential: ... You may need to run 'cx auth login' again."

Review feedback addressed

  • Failed save is now visible without --debug.
  • Compare-then-write is atomic for the config file; the doc comment now states the keyring limit. (Holding Resolver.mu was not used: Rotate calls explicitValue and resolve, which take it, and it only guards the in-process override map.)
  • Added tests: failing store, PolicyDisabled, PolicyAuto (working keyring and keyring unavailable), and the warning on a failed save.

Automated tests

  • go test ./internal/configfile/ ./internal/credentialstore/
  • go test -vet=off ./internal/wrappers/ -run ExchangeAPIKey (-vet=off only because go vet already fails on unrelated files)
  • golangci-lint run -c .golangci.yml --new-from-rev=origin/main: 0 new issues

Manual test results (live EU tenant, isolated config path, Windows)

Case Result
disabled: login, then 4 refreshes Pass. Token in the config file changed each run
required (Credential Manager): login, then 3 refreshes Pass. Keyring entry rotated each run; no token in YAML
auto (Credential Manager): login, then 3 refreshes Pass. Same as required
Token via CX_APIKEY / --apikey Pass. Not saved, stored value unchanged
Config file read-only (save fails) Pass. Command succeeds (run before the warning change; the warning itself is covered by a unit test)
Baseline main, 5 runs Never failed and never saved. This realm still accepts the old token after rotation

Not covered

  • A user-created API key; client ID and secret login; two real logins at once.
  • The VS Code extension and JetBrains plugin pass their own copy of the token with --apikey, so this fix cannot update their copy.
  • cx auth login still uses the ide-integration client.

Pairs with Checkmarx/ast-vscode-extension#1576 and Checkmarx/ast-jetbrains-plugin#475 (cx-mcp-client login).

🤖 Generated with Claude Code

When IAM returns a new refresh token in the refresh_token grant response,
the old one is invalidated. getNewToken dropped it, so the next exchange
failed and users had to sign in again. Save it back to the credential store
when the stored value is still the one that was exchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@cx-rahul-pidde
cx-rahul-pidde requested a review from a team November 23, 2026 12:24
cx-rahul-pidde and others added 2 commits November 23, 2026 20:00
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Rotate now returns whether it wrote the new value, so the verbose log no
longer claims a save when the credential came from a flag or environment
variable or was changed by another login.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Comment thread internal/wrappers/client.go Resolved
Comment thread internal/credentialstore/resolver.go Resolved
…e tests

- Print a non-verbose warning when the rotated token cannot be saved;
  the command still succeeds.
- Rotate replaces the config file entry atomically under the config file
  lock (SetKeyIfEqual) and, in auto mode with the keyring unavailable,
  falls back to the config file only when it already holds the old token.
- Soften the Rotate doc comment: the keyring has no compare-and-set.
- Add tests for PolicyDisabled and PolicyAuto, the keyring fallback, a
  failing store, and the warning on a failed save.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@cx-rahul-pidde cx-rahul-pidde changed the title Persist rotated refresh token after token exchange Persist rotated refresh token after token exchange (AST-195266) Nov 24, 2026
@cx-rahul-pidde
cx-rahul-pidde changed the base branch from main to staging November 25, 2026 05:14

@cx-prathmesh-borle cx-prathmesh-borle 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.

LGTM

@cx-sumit-morchhale
cx-sumit-morchhale merged commit 70a7eef into staging Nov 26, 2026
23 of 27 checks passed
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.

4 participants