Fix OIDC token refresh to handle missing refresh_token in response #384
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.
Closes: #296
Problem
The
_refresh_oidcmethod inkube_config.pyassumes that arefresh_tokenwill always be returned in OAuth2 token refresh responses. However, different token management strategies may not return a refresh token in every refresh response:This causes a
KeyError: 'refresh_token'at line 335 when the refresh response doesn't include a new refresh token.Solution
Modified
_refresh_oidcto conditionally update the refresh token only if it's present in the response:if "refresh_token" in resp:
provider["config"].value["refresh-token"] = resp["refresh_token"]. This preserves the existing refresh token when a new one isn't provided, which is the correct behavior for sliding token strategies.
Changes
_refresh_oidcmethod to check forrefresh_tokenbefore updatingtest_oidc_with_refresh_no_new_refresh_tokento verify behavior when refresh_token is not returnedTesting