fix(auth): persist refreshed OAuth2 credentials to store#5350
Open
voidborne-d wants to merge 1 commit intogoogle:mainfrom
Open
fix(auth): persist refreshed OAuth2 credentials to store#5350voidborne-d wants to merge 1 commit intogoogle:mainfrom
voidborne-d wants to merge 1 commit intogoogle:mainfrom
Conversation
After `OAuth2CredentialRefresher.refresh()` rotates the tokens in memory, the updated credential was never written back to the credential store. On the next tool invocation, `get_credential()` deserialized the stale pre-refresh dict and returned expired tokens. For providers that rotate refresh_tokens on each refresh (Salesforce, many OIDC providers), this caused the subsequent refresh attempt to fail — the old refresh_token was already invalidated — forcing a full re-authorization flow. The fix calls `_store_credential()` immediately after a successful refresh so that the new access_token and refresh_token are persisted. Fixes google#5329
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.
Summary
After
OAuth2CredentialRefresher.refresh()rotates the tokens in memory, the updated credential was never written back to the credential store. On the next tool invocation,get_credential()deserializes the stale pre-refresh dict, getting expired tokens.For providers that rotate
refresh_tokenon each refresh (Salesforce, many OIDC providers), this causes the subsequent refresh attempt to fail — the oldrefresh_tokenhas already been invalidated — forcing a full re-authorization flow.Root cause
OAuth2CredentialRefresher.refresh()mutates theAuthCredentialobject in memory viaupdate_credential_with_tokens(). However,ToolContextCredentialStore.get_credential()deserializes a fresh Pydantic model from the stored dict on each call — the in-memory mutation is disconnected from the persisted state.The class already has a
_store_credential()method that serializes and writes totool_context.state. It is called after fetching new credentials from an auth response (line 332), but was missing after the refresh path.Fix
Call
self._store_credential(existing_credential)immediately after a successful refresh in_get_existing_credential()(1 line).Test
Added
test_refreshed_credential_is_persisted_to_store— verifies that after a token refresh, the credential store contains the newaccess_tokenandrefresh_token, not the stale ones.Fixes #5329