Repository navigation
fix(auth): clear token caches after login so new scopes take effect - #959
Draft
phatmandrake wants to merge 3 commits into
Draft
phatmandrake wants to merge 3 commits into
phatmandrake wants to merge 3 commits into
Conversation
`gws auth login` saved the new credentials but left token_cache.json and sa_token_cache.json in place. The cache is keyed by scope set, so any method called before the re-login kept receiving the access token minted under the previous grant, which still lacked the new scopes, until it expired (up to an hour). Users saw a persistent 403 "Request had insufficient authentication scopes" while `auth status` showed the new scopes. Remove both cache files right after the new credentials are saved, and invalidate the cached timezone as logout already does. If a cache file exists but cannot be removed, login now fails with an explicit error instead of reporting success while stale tokens remain in use. Fixes googleworkspace#764 Fixes googleworkspace#876
🦋 Changeset detectedLatest commit: 616c10a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
phatmandrake
marked this pull request as draft
October 15, 2026 01:56
Logout and login now share one helper, clear_token_caches, which returns the paths it actually removed. This removes the check-then-delete race in logout. The decrypt-failure recovery path in auth.rs keeps its warn-and-continue loop on purpose.
The test encrypts and decrypts through config_dir() without the serial lock, so a test that switches GOOGLE_WORKSPACE_CLI_CONFIG_DIR under the lock can change the key file between its two reads. Seven neighbouring tests already carry the attribute.
Collaborator
|
/gemini review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
gws auth loginsaved the new credentials but never touchedtoken_cache.jsonorsa_token_cache.json. The token cache is keyed by scope set (token_storage.rscache_key), andauth::get_tokenserves a still-valid cached token without refreshing, so any method already called before the re-login kept receiving the access token minted under the previous grant, which lacked the newly added scopes, until it expired (up to an hour). Users saw a persistent403 Request had insufficient authentication scopeswhilegws auth statusshowed the new scopes andcredentials.enchad a fresh timestamp.This change removes both cache files right after the new credentials are saved in
handle_login_inner, and invalidates the cached timezone asauth logoutalready does. If a cache file exists but cannot be removed, login now fails with an explicit error instead of reporting success while stale tokens remain in use.handle_logoutis unchanged.Fixes #764
Fixes #876
Earlier attempts #796 and #808 implemented the same idea and were closed by the stale workflow without review.
Scope note:
handle_logoutand the undecryptable-credentials recovery path inauth.rseach remove the same two cache files with their own loop (logout checksexists()first, the recovery path is best-effort). This PR deliberately does not touch them, to keep the fix minimal; happy to follow up with a change that routes both throughclear_token_cachesif maintainers want that.Reproduction (before):
gws auth login --scopes <set without chat.messages.readonly>gws chat spaces messages list --params '{"parent":"spaces/<id>","pageSize":1}'→ 403 (expected), writestoken_cache.jsongws auth login --scopes <same set plus chat.messages.readonly>→ login succeeds,auth statuslists the new scopeVerification (after, this branch, macOS 26, keyring backend): same four steps with the patched binary. After step 3 the config directory no longer contains
token_cache.json, and step 4 returns messages immediately (credentials written 19:47:06, fresh cache minted by the next call at 19:50:48).Dry Run Output:
// Not applicable: this change affects the auth login flow, not a request body.Checklist:
AGENTS.mdguidelines (no generatedgoogle-*crates).cargo fmt --allto format the code perfectly.cargo clippy -- -D warningsand resolved all warnings. (The one remaining error,collapsible_matchinhelpers/script.rs:173, is present onmainwith rustc 1.95 and is outside this change.)clear_token_caches_removes_both_cache_files,clear_token_caches_is_noop_when_files_absent,clear_token_caches_errors_when_cache_cannot_be_removed; 704 tests pass.)pnpx changeset) to document my changes. (.changeset/clear-token-cache-after-login.md, patch.)