fix(auth): don't delete cached credentials when token refresh fails#3443
Open
1fanwang wants to merge 1 commit into
Open
fix(auth): don't delete cached credentials when token refresh fails#34431fanwang wants to merge 1 commit into
1fanwang wants to merge 1 commit into
Conversation
The authenticators call KeyringStore.delete(endpoint) when a token refresh fails, then immediately fall back to a full authorization flow that calls KeyringStore.store(...). store() overwrites each key in place, so the delete is redundant -- and harmful: a transient refresh failure wipes the last-good cached tokens (including a still-valid refresh_token), forcing an unnecessary browser re-login. Remove the premature delete() from the PKCE and device-code refresh paths and from the identity-aware-proxy authenticator. At the device-flow poll site the surrounding try/except existed only to delete-and-re-raise, so it is dropped. Signed-off-by: 1fanwang <1fannnw@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #3443 +/- ##
==========================================
- Coverage 84.07% 78.47% -5.60%
==========================================
Files 396 311 -85
Lines 31231 26573 -4658
Branches 3029 3029
==========================================
- Hits 26257 20853 -5404
- Misses 4099 4897 +798
+ Partials 875 823 -52 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Why are the changes needed?
The authenticators delete the cached credentials whenever a token refresh fails, then fall back to a full re-auth. But
KeyringStore.store()overwrites in place, so thedelete()is redundant — and harmful: a transient refresh failure (a network blip, an IdP hiccup) wipes the last-good tokens, including a still-validrefresh_token, forcing the user through an unnecessary browser re-login.What changes were proposed in this pull request?
Drop the premature
KeyringStore.delete(self._endpoint)from the refresh-failure paths in the PKCE, device-code, and GCP identity-aware-proxy authenticators. Each fall-through still callsstore(), so a successful re-auth persists. At the device-flow poll site the surroundingtry/exceptexisted only to delete-and-re-raise, so it's removed and exceptions propagate unchanged.How was this patch tested?
Added
test_pkce_refresh_failure_keeps_cached_credentialsand tightenedtest_device_flow_authenticator, both assertingdeleteisn't called when a refresh fails. Reverting the fix makes both fail withExpected 'delete' to not have been called. Called 1 times.Check all the applicable boxes