Skip to content

fix(auth): invalidate token cache when credentials change - #991

Closed
hallelx2 wants to merge 1 commit into
googleworkspace:mainfrom
hallelx2:fix/780-token-cache-invalidation
Closed

hallelx2 wants to merge 1 commit into
googleworkspace:mainfrom
hallelx2:fix/780-token-cache-invalidation

Conversation

@hallelx2

@hallelx2 hallelx2 commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Fixes #780. Fixes #572.

After gws auth login to another account, or to the same account with new scopes, gws keeps serving the access token cached for the previous credential. auth status shows the new account while API calls still run as the old one. yup-oauth2 stores the refresh token next to each cached access token and refreshes expired entries with that stored token, so the old grant outlives the cached access token's hour. Switching GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE between exported credentials hits the same shared cache (#572).

Two changes:

  1. auth login clears the caches. After saving the new encrypted credentials it removes token_cache.json, sa_token_cache.json and the account timezone cache, as auth logout already does.
  2. Cached tokens are bound to the credential that minted them. Cache keys are prefixed with a truncated SHA-256 fingerprint of the credential (client ID + refresh token for a user, the key for a service account). A cache written for one credential is never served to another, which covers the GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE case where no login happens. On write, entries for other credentials, including legacy unbound keys, are pruned so they don't accumulate. The fingerprint is a digest and reveals nothing about the secrets.

Existing caches are ignored once and rewritten in the new format, so the next call after upgrading mints one fresh token.

Testing

  • New unit tests: fingerprint stability and change on a new grant; a token cached for credential A is not served to credential B through the same file, and B's write prunes A's entry; legacy unbound entries are ignored; auth login's cache clearing.
  • cargo test: all pass except test_get_quota_project_reads_adc and test_get_quota_project_priority_adc_fallback, which also fail on main on a machine with gcloud ADC configured (they read the real well-known ADC file).
  • cargo clippy -- -D warnings: no new lints. On a recent toolchain main already fails a few lints in files this PR doesn't touch.

Dry Run Output:

// Not applicable: no new API command or request body.

Checklist:

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings (none new).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 684a1f5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-cla

google-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

After `gws auth login` with another account, or with the same account and
new scopes, gws kept serving the access token cached for the previous
credential. yup-oauth2 stores the refresh token next to each cached access
token and refreshes expired entries with it, so the stale grant survived
even after the cached token expired. Switching
GOOGLE_WORKSPACE_CLI_CREDENTIALS_FILE hit the same cache.

- `auth login` removes token_cache.json and sa_token_cache.json (and the
  account timezone cache) after saving the new credentials.
- Cache entries are keyed by a truncated SHA-256 fingerprint of the
  credential (client ID + refresh token, or the service account key), so a
  cache written for one credential is never served to another. Entries for
  other credentials, including legacy unbound ones, are pruned on write.

Fixes googleworkspace#780
Fixes googleworkspace#572
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been inactive for 72 hours. Closing to keep the queue clean.

@github-actions github-actions Bot added the Stale label Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 72 hours. Feel free to magically reopen it if you want to continue working on it!

@github-actions github-actions Bot closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant