Authentication¶
Base Auth¶
agora_workbench.code_execution.auth.base
¶
Abstract authentication interfaces for MCP code execution servers.
This module defines the pluggable authentication contract that decouples the server from any specific identity provider (Azure Entra ID, Auth0, Keycloak, etc.).
Three interfaces form the abstraction:
- TokenValidator: Validates bearer tokens and returns claims.
- IdentityExtractor: Derives a unique user identity string from claims.
- CredentialProvider: Provides credentials for downstream resource access.
Implementations for Azure Entra ID live in auth.entra. A no-op
implementation suitable for local development lives in auth.noop.
AccessToken
¶
Bases: NamedTuple
A provider-agnostic access token.
Structurally compatible with azure.core.credentials.AccessToken.
TokenValidationError(message, status_code=401)
¶
TokenValidator
¶
Bases: ABC
Validates bearer tokens from incoming requests.
Implementations handle signature verification, expiry checks, audience/issuer validation, and any provider-specific logic.
Implementations do not need to publish OAuth discovery metadata. Set
AuthConfig.protected_resource_metadata instead — it is provider-agnostic
and part of the public contract.
.. deprecated::
Servers also probe validators for private _client_id / _tenant_id
attributes to compose Entra-shaped metadata. That fallback is retained for
validators written against the old convention; new implementations should
use AuthConfig.protected_resource_metadata.
validate(token, *, request_path='/mcp', request_method='POST')
abstractmethod
async
¶
Validate a bearer token and return decoded claims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The raw bearer token string. |
required |
request_path
|
str
|
Optional hint for validators that support path-based authorization. Not used by standard JWT validation. |
'/mcp'
|
request_method
|
str
|
Optional hint for validators that support method-based authorization. Not used by standard JWT validation. |
'POST'
|
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary of validated claims (e.g. |
Raises:
| Type | Description |
|---|---|
TokenValidationError
|
If the token is invalid, expired, or fails any validation check. |
Source code in src/agora_workbench/code_execution/auth/base.py
IdentityExtractor
¶
Bases: ABC
Extracts a unique user identity string from validated token claims.
The identity is used for session ownership and access control.
Different providers encode identity differently (Azure uses oid+tid,
generic OIDC uses sub, etc.).
extract(claims)
abstractmethod
¶
Derive a unique user identity from token claims.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claims
|
dict
|
Validated token claims dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
A unique string identifying the user (e.g. |
Optional[str]
|
or |
Source code in src/agora_workbench/code_execution/auth/base.py
CredentialProvider
¶
Bases: ABC
Provides credentials for accessing downstream resources on behalf of the authenticated user or the server's own identity.
Implementations may use managed identity or provide static/no-op credentials depending on deployment context.
get_token(scope)
abstractmethod
async
¶
Obtain an access token for the given resource scope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
str
|
The target resource scope
(e.g. |
required |
Returns:
| Type | Description |
|---|---|
AccessToken
|
An AccessToken with the token string and expiration. |
Raises:
| Type | Description |
|---|---|
CredentialError
|
If token acquisition fails. |
Source code in src/agora_workbench/code_execution/auth/base.py
CredentialError(message, scope, original_error=None)
¶
Bases: Exception
Raised when credential acquisition fails.
Source code in src/agora_workbench/code_execution/auth/base.py
AuthConfig(token_validator, identity_extractor, credential_provider_factory=None, www_authenticate_value='', require_authorization_header=True, protected_resource_metadata=None)
dataclass
¶
Composite auth configuration injected into CodeExecutionServer.
Bundles the three auth interfaces so servers can be constructed with
a single auth_config parameter instead of scattered provider args.
credential_provider_factory = None
class-attribute
instance-attribute
¶
Optional callable: (user_token: str) -> CredentialProvider.
Called per-session to create a credential provider scoped to the
authenticated user's token. If None, downstream resource access
is not available (suitable for servers that don't call external APIs).
www_authenticate_value = ''
class-attribute
instance-attribute
¶
Value for the WWW-Authenticate response header on 401.
require_authorization_header = True
class-attribute
instance-attribute
¶
Whether protected endpoints require an Authorization header.
protected_resource_metadata = None
class-attribute
instance-attribute
¶
RFC 9728 document served verbatim at /.well-known/oauth-protected-resource.
Lets any identity provider describe itself without the server needing
provider-specific knowledge. create_entra_auth_config() populates this with
an Entra-shaped document; custom validators should set it explicitly.
When set, it must be a non-empty mapping containing at least resource, the
only field RFC 9728 marks REQUIRED. This is validated on construction, so the
endpoint can serve any non-None value as-is rather than silently returning
a document that discovery clients cannot use.
When None, servers fall back to composing an Entra document from their
entra_client_id / entra_tenant_id attributes.
Entra ID¶
agora_workbench.code_execution.auth.entra
¶
Azure Entra ID authentication implementations.
Provides concrete implementations of the auth interfaces (TokenValidator, IdentityExtractor, CredentialProvider) for Microsoft Entra ID (Azure AD).
Uses PyJWT for JWT validation with JWKS key fetching from the Microsoft identity platform's OpenID configuration endpoint.
EntraTokenValidator(client_id, tenant_id)
¶
Bases: TokenValidator
Validates JWTs issued by Microsoft Entra ID using PyJWT.
Fetches signing keys from the Microsoft identity platform JWKS endpoint, verifies signature (RS256), and validates standard claims (exp, aud, iss).
Note: This implementation is single-tenant only. The JWKS URL and issuer
validation use the specific tenant ID. For multi-tenant support, use the
/common/ JWKS endpoint and validate the tid claim manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
str
|
The Entra ID application (client) ID — used as audience. |
required |
tenant_id
|
str
|
The Entra ID tenant ID. |
required |
Source code in src/agora_workbench/code_execution/auth/entra.py
EntraIdentityExtractor
¶
Bases: IdentityExtractor
Extracts user identity from Entra ID JWT claims as {oid}@{tid}.
This composite format ensures session isolation across both users and
tenants. Falls back to sub if oid is absent.
EntraCredentialProvider(client_id=None)
¶
Bases: CredentialProvider
Provides Azure credentials for downstream resource access via managed identity.
Uses Azure ManagedIdentityCredential (user-assigned if AZURE_CLIENT_ID is set, otherwise system-assigned) to obtain tokens for downstream resources such as Azure Storage and Azure AI Search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
Optional[str]
|
User-assigned managed identity client ID. Falls back to AZURE_CLIENT_ID env var. If None/unset, system-assigned managed identity is used. |
None
|
Source code in src/agora_workbench/code_execution/auth/entra.py
CredentialProviderTokenCredential(credential_provider)
¶
create_entra_auth_config(client_id=None, tenant_id=None)
¶
Create an AuthConfig using Azure Entra ID for all three auth concerns.
This is the default configuration for production and local-dev-with-Azure deployments. Reads from environment variables if arguments are not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_id
|
Optional[str]
|
Entra ID app client ID. Falls back to |
None
|
tenant_id
|
Optional[str]
|
Entra ID tenant ID. Falls back to |
None
|
Returns:
| Type | Description |
|---|---|
AuthConfig
|
A fully configured AuthConfig for Entra ID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required configuration is missing. |