-
Notifications
You must be signed in to change notification settings - Fork 3.8k
OAuth client: refresh before re-authorizing, and discover before refreshing #3328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxisbey
wants to merge
1
commit into
main
Choose a base branch
from
oauth-refresh-on-401
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 report_expired_on_load stands in for "already expired" with expires_in=0, but that makes _initialize set token_expiry_time = time.time() and is_token_valid() uses the inclusive test
time.time() <= token_expiry_timemicroseconds later - on Windows CPython 3.10-3.12, where time.time() is backed by GetSystemTimeAsFileTime with ~15.6 ms granularity (the precise clock was only adopted in 3.13), both calls return the same value, so the token is judged still valid and the cold-start refresh path never runs.Extended reasoning...
On the windows-latest x {3.10, 3.11, 3.12} CI entries (matrix at .github/workflows/shared.yml line 75), test_a_refresh_before_the_first_request_discovers_metadata_and_posts_to_the_advertised_token_endpoint and test_a_stored_registration_with_a_lapsed_secret_is_replaced_before_authorizing enter async_auth_flow, _initialize() derives expiry A from the copied token's expires_in=0, and the
not is_token_valid()gate evaluates time.time() (== A within the same 15.6 ms clock tick) <= A as True. The cold-start _reacquire_tokens(challenge=None) pass is skipped, the stale bearer (still valid server-side in both tests - neither calls expire_access_token) is sent to /mcp and succeeds, so recorded[0] is ("POST", "/mcp") instead of the discovery GET and the snapshot assertions fail - a near-deterministic failure on those matrix entries, not just a flake. Fix: return the copy with a strictly negative expires_in (e.g. -1), which OAuthToken permits (expires_in: int | None has no ge=0 constraint).Verification: normal. The chain is real and each link checks out. (1) /home/claude/python-sdk/tests/interaction/auth/_harness.py:131-134:
get_tokensreturnsself.tokens.model_copy(update={"expires_in": 0}). (2) /home/claude/python-sdk/src/mcp/client/auth/oauth2.py:584-585 (new in this PR):_initializederives expiry viaupdate_token_expiry->calculate_token_expiry(0)which istime.time() + int(0)