-
Notifications
You must be signed in to change notification settings - Fork 425
refactor(server)!: add build_user function to DefaultContextBuilder to allow A2A user creation customization #925
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
guglielmo-san
merged 16 commits into
a2aproject:1.0-dev
from
guglielmo-san:guglielmoc/refactor_server_call_context
Apr 3, 2026
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2a3c577
feat: refactor JSON-RPC dispatcher and common context handling
guglielmo-san e1fb3f3
Merge remote-tracking branch 'upstream/1.0-dev' into guglielmoc/refac…
guglielmo-san 60e9fed
refactor: replace CallContextBuilder with functional UserBuilder patt…
guglielmo-san 86665b5
refactor: extract StarletteUserProxy and add comprehensive route util…
guglielmo-san 2877893
Merge branch '1.0-dev' into guglielmoc/refactor_server_call_context
guglielmo-san 56e2449
fix
guglielmo-san fd7790f
fix
guglielmo-san fdc2156
Merge branch '1.0-dev' into guglielmoc/refactor_server_call_context
guglielmo-san 30466c4
wip
guglielmo-san f8b0b8c
fix changes
guglielmo-san 700ecf6
fix
guglielmo-san 1864526
Merge branch '1.0-dev' into guglielmoc/refactor_server_call_context
guglielmo-san 8db4e8c
refactor: standardize ContextBuilder documentation and remove redunda…
guglielmo-san c3ee2e5
refactor: rename context builder classes and types to include ServerC…
guglielmo-san 30da41f
fix
guglielmo-san b2553f4
fix
guglielmo-san 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from starlette.authentication import BaseUser | ||
| from starlette.requests import Request | ||
| else: | ||
| try: | ||
| from starlette.authentication import BaseUser | ||
| from starlette.requests import Request | ||
| except ImportError: | ||
| Request = Any | ||
| BaseUser = Any | ||
|
|
||
| from a2a.auth.user import UnauthenticatedUser, User | ||
| from a2a.extensions.common import ( | ||
| HTTP_EXTENSION_HEADER, | ||
| get_requested_extensions, | ||
| ) | ||
| from a2a.server.context import ServerCallContext | ||
|
|
||
|
|
||
| class StarletteUser(User): | ||
|
guglielmo-san marked this conversation as resolved.
|
||
| """Adapts a Starlette BaseUser to the A2A User interface.""" | ||
|
|
||
| def __init__(self, user: BaseUser): | ||
| self._user = user | ||
|
|
||
| @property | ||
| def is_authenticated(self) -> bool: | ||
| """Returns whether the current user is authenticated.""" | ||
| return self._user.is_authenticated | ||
|
|
||
| @property | ||
| def user_name(self) -> str: | ||
| """Returns the user name of the current user.""" | ||
| return self._user.display_name | ||
|
guglielmo-san marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class ContextBuilder(ABC): | ||
|
guglielmo-san marked this conversation as resolved.
Outdated
|
||
| """A class for building ServerCallContexts using the Starlette Request.""" | ||
|
|
||
| @abstractmethod | ||
| def build(self, request: Request) -> ServerCallContext: | ||
| """Builds a ServerCallContext from a Starlette Request.""" | ||
|
|
||
|
|
||
| class DefaultContextBuilder(ContextBuilder): | ||
| """A default implementation of ContextBuilder.""" | ||
|
|
||
| def build(self, request: Request) -> ServerCallContext: | ||
| """Builds a ServerCallContext from a Starlette Request. | ||
|
|
||
| Args: | ||
| request: The incoming Starlette Request object. | ||
|
|
||
| Returns: | ||
| A ServerCallContext instance populated with user and state | ||
| information from the request. | ||
| """ | ||
| state = {} | ||
| if 'auth' in request.scope: | ||
| state['auth'] = request.auth | ||
| state['headers'] = dict(request.headers) | ||
| return ServerCallContext( | ||
| user=self.build_user(request), | ||
| state=state, | ||
| requested_extensions=get_requested_extensions( | ||
| request.headers.getlist(HTTP_EXTENSION_HEADER) | ||
| ), | ||
| ) | ||
|
|
||
| def build_user(self, request: Request) -> User: | ||
| """Builds a User from a Starlette Request. | ||
|
|
||
| Args: | ||
| request: The incoming Starlette Request object. | ||
|
|
||
| Returns: | ||
| A User instance populated with user information from the request. | ||
| """ | ||
| if 'user' in request.scope: | ||
| return StarletteUser(request.user) | ||
| return UnauthenticatedUser() | ||
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.
Uh oh!
There was an error while loading. Please reload this page.