[v1.x] Bind transport sessions to the authenticated principal (#2719)
This commit is contained in:
parent
1abcca2408
commit
ce267b6fc5
1 changed files with 404 additions and 38 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import json
|
||||
import time
|
||||
from typing import Any
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from pydantic import AnyHttpUrl
|
||||
from starlette.authentication import AuthCredentials, AuthenticationBackend, SimpleUser
|
||||
|
|
@ -19,6 +19,30 @@ class AuthenticatedUser(SimpleUser):
|
|||
self.scopes = auth_info.scopes
|
||||
|
||||
|
||||
class AuthorizationContext(TypedDict):
|
||||
client_id: str
|
||||
issuer: str | None
|
||||
subject: str | None
|
||||
|
||||
|
||||
def authorization_context(user: AuthenticatedUser) -> AuthorizationContext:
|
||||
"""Identify the principal `user` represents, for transports to compare
|
||||
against the principal that created a session. Components the token
|
||||
verifier does not supply are `None`, so the comparison degrades to the
|
||||
remaining components.
|
||||
|
||||
See `examples/servers/simple-auth/mcp_simple_auth/token_verifier.py` for
|
||||
a verifier that populates `subject` and `claims` from an introspection
|
||||
response."""
|
||||
token = user.access_token
|
||||
issuer = (token.claims or {}).get("iss")
|
||||
return AuthorizationContext(
|
||||
client_id=token.client_id,
|
||||
issuer=str(issuer) if issuer is not None else None,
|
||||
subject=token.subject,
|
||||
)
|
||||
|
||||
|
||||
class BearerAuthBackend(AuthenticationBackend):
|
||||
"""
|
||||
Authentication backend that validates Bearer tokens using a TokenVerifier.
|
||||
|
|
|
|||
Loading…
Reference in a new issue