AuthContextProtocol
Protocol defining the contract for authentication context.
This is the primary port for accessing user identity in the hexagonal
architecture. Implementations extract user context from various sources:
- JWT tokens (production)
- Request headers (microservice communication)
- Test fixtures (testing)
Example usage:
auth: AuthContextProtocol = container.get(AuthContextProtocol)
user = await auth.get_current_user()
if user.has_role("Admin"):
# Allow admin action
pass
Source: auth_context.py
Methods
get_current_user
async def get_current_user(self) -> UserContext
Get the current authenticated user from request context.
Returns:
UserContext for the authenticated user
Raises:
AuthenticationError: If no valid authentication is present
get_user_by_id
async def get_user_by_id(self, user_id: str) -> UserContext | None
Look up a user by their ID.
Used for permission checks on document ownership, etc.
Args:
user_id: The user's unique identifier
Returns:
UserContext if user found, None otherwise