-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[v1.x] Add AuthSettings.validate_token_resource to check a bearer token's resource #3451
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
Merged
+175
−9
Merged
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import warnings | ||
|
|
||
| import pytest | ||
| from pydantic import AnyHttpUrl, ValidationError | ||
|
|
||
| from mcp.server.auth.settings import AuthSettings | ||
|
|
||
| ISSUER = AnyHttpUrl("https://auth.example.com") | ||
| RESOURCE = AnyHttpUrl("https://mcp.example.com/mcp") | ||
|
|
||
|
|
||
| def test_validate_token_resource_requires_a_resource_server_url(): | ||
| """SDK-defined: asking the bearer gate to compare tokens against `resource_server_url` without | ||
| configuring one is refused at construction time rather than silently comparing nothing.""" | ||
| AuthSettings(issuer_url=ISSUER, resource_server_url=RESOURCE, validate_token_resource=True) | ||
| with pytest.raises(ValidationError, match="validate_token_resource requires resource_server_url"): | ||
| AuthSettings(issuer_url=ISSUER, resource_server_url=None, validate_token_resource=True) | ||
|
|
||
|
|
||
| def test_leaving_validate_token_resource_unset_warns_when_a_resource_server_url_is_configured(): | ||
| """Unset behaves as False but says so: a resource server that has not chosen gets a | ||
| `DeprecationWarning` pointing at its own `AuthSettings(...)` call (3.0 flips the default).""" | ||
| with pytest.warns(DeprecationWarning, match="validate_token_resource") as record: | ||
| settings = AuthSettings(issuer_url=ISSUER, resource_server_url=RESOURCE) | ||
| assert settings.validate_token_resource is None | ||
| assert record[0].filename == __file__ | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("kwargs", [{"validate_token_resource": False}, {"resource_server_url": None}]) | ||
| def test_an_explicit_choice_or_no_resource_server_url_does_not_warn(kwargs: dict[str, object]): | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter("error") | ||
| AuthSettings.model_validate({"issuer_url": ISSUER, "resource_server_url": RESOURCE, **kwargs}) |
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.
Uh oh!
There was an error while loading. Please reload this page.