-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add subject and claims to AccessToken #2686
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c4fe4d2
Add subject and claims to AccessToken
maxisbey 7e542e9
Clarify subject docstrings and add Context.claims
maxisbey 4185d1c
Scope down to model fields and add subject to auth integration test
maxisbey a857cfc
Address review: iss in introspection, tighten field comments
maxisbey 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
|
maxisbey marked this conversation as resolved.
|
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
|
maxisbey marked this conversation as resolved.
Outdated
|
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,66 @@ | ||
| """Context.subject and Context.claims read from the request's access token.""" | ||
|
|
||
| from mcp.server.auth.middleware.auth_context import auth_context_var | ||
| from mcp.server.auth.middleware.bearer_auth import AuthenticatedUser | ||
| from mcp.server.auth.provider import AccessToken | ||
| from mcp.server.mcpserver import Context | ||
|
|
||
|
|
||
| def test_subject_is_none_when_unauthenticated(): | ||
| assert Context().subject is None | ||
|
|
||
|
|
||
| def test_subject_is_none_when_token_has_no_subject(): | ||
| user = AuthenticatedUser(AccessToken(token="t", client_id="c", scopes=[])) | ||
| cv_token = auth_context_var.set(user) | ||
| try: | ||
| assert Context().subject is None | ||
| finally: | ||
| auth_context_var.reset(cv_token) | ||
|
|
||
|
|
||
| def test_subject_reads_from_access_token(): | ||
| user = AuthenticatedUser(AccessToken(token="t", client_id="c", scopes=[], subject="user-123")) | ||
| cv_token = auth_context_var.set(user) | ||
| try: | ||
| assert Context().subject == "user-123" | ||
| finally: | ||
| auth_context_var.reset(cv_token) | ||
|
|
||
|
|
||
| def test_subject_tracks_current_auth_context(): | ||
| ctx = Context() | ||
| assert ctx.subject is None | ||
|
|
||
| alice = AuthenticatedUser(AccessToken(token="a", client_id="c", scopes=[], subject="alice")) | ||
| cv_token = auth_context_var.set(alice) | ||
| try: | ||
| assert ctx.subject == "alice" | ||
| finally: | ||
| auth_context_var.reset(cv_token) | ||
|
|
||
| assert ctx.subject is None | ||
|
|
||
|
|
||
| def test_claims_is_none_when_unauthenticated(): | ||
| assert Context().claims is None | ||
|
|
||
|
|
||
| def test_claims_is_none_when_token_has_no_claims(): | ||
| user = AuthenticatedUser(AccessToken(token="t", client_id="c", scopes=[])) | ||
| cv_token = auth_context_var.set(user) | ||
| try: | ||
| assert Context().claims is None | ||
| finally: | ||
| auth_context_var.reset(cv_token) | ||
|
|
||
|
|
||
| def test_claims_reads_from_access_token(): | ||
| user = AuthenticatedUser( | ||
| AccessToken(token="t", client_id="c", scopes=[], claims={"iss": "https://auth.example.com"}) | ||
| ) | ||
| cv_token = auth_context_var.set(user) | ||
| try: | ||
| assert Context().claims == {"iss": "https://auth.example.com"} | ||
| finally: | ||
| auth_context_var.reset(cv_token) |
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.