Security¶
Authentication schemes, credentials, and user types.
AuthCredentials¶
AuthCredentials
dataclass
¶
Extracted credentials from an HTTP request.
Attributes:
| Name | Type | Description |
|---|---|---|
scheme |
str
|
The authentication scheme (e.g. "bearer", "basic", "apikey"). |
credentials |
str
|
The raw credential value (token, key, or password). |
scopes |
tuple[str, ...]
|
Optional OAuth2 scopes associated with the credentials. |
Source code in src/agenticapi/security.py
AuthUser¶
AuthUser
dataclass
¶
An authenticated user.
Carried through the request lifecycle via AgentContext.metadata.
The user_id field is also set on AgentContext.user_id.
Attributes:
| Name | Type | Description |
|---|---|---|
user_id |
str
|
Unique identifier for the user. |
username |
str | None
|
Optional human-readable username. |
roles |
tuple[str, ...]
|
Roles assigned to the user (e.g. "admin", "operator"). |
scopes |
tuple[str, ...]
|
OAuth2 scopes granted to this user/token. |
metadata |
dict[str, Any]
|
Arbitrary additional user metadata. |
Source code in src/agenticapi/security.py
SecurityScheme¶
SecurityScheme ¶
Bases: Protocol
Protocol for authentication scheme implementations.
Each scheme is a callable that extracts credentials from
a Starlette Request. If auto_error is True and
credentials are missing, the scheme raises AuthenticationError.
Implementations must provide scheme_name and auto_error
attributes, and be callable with a Request argument.
Source code in src/agenticapi/security.py
__call__
async
¶
Extract credentials from the request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming Starlette request. |
required |
Returns:
| Type | Description |
|---|---|
AuthCredentials | None
|
Extracted credentials, or None if not found and auto_error is False. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If credentials are missing and auto_error is True. |
Source code in src/agenticapi/security.py
APIKeyHeader¶
APIKeyHeader ¶
Extract an API key from a request header.
Example
scheme = APIKeyHeader(name="X-API-Key") credentials = await scheme(request)
credentials.credentials == "the-key-value"¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Header name to read the API key from. |
'X-API-Key'
|
auto_error
|
bool
|
If True, raise AuthenticationError when the header is missing. |
True
|
Source code in src/agenticapi/security.py
__call__
async
¶
Extract API key from the configured header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming Starlette request. |
required |
Returns:
| Type | Description |
|---|---|
AuthCredentials | None
|
AuthCredentials with the API key, or None. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If the header is missing and auto_error is True. |
Source code in src/agenticapi/security.py
APIKeyQuery¶
APIKeyQuery ¶
Extract an API key from a query parameter.
Example
scheme = APIKeyQuery(name="api_key") credentials = await scheme(request) # from ?api_key=value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Query parameter name to read the API key from. |
'api_key'
|
auto_error
|
bool
|
If True, raise AuthenticationError when the parameter is missing. |
True
|
Source code in src/agenticapi/security.py
__call__
async
¶
Extract API key from the configured query parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming Starlette request. |
required |
Returns:
| Type | Description |
|---|---|
AuthCredentials | None
|
AuthCredentials with the API key, or None. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If the parameter is missing and auto_error is True. |
Source code in src/agenticapi/security.py
HTTPBearer¶
HTTPBearer ¶
Extract a Bearer token from the Authorization header.
Expects the format: Authorization: Bearer <token>
Example
scheme = HTTPBearer() credentials = await scheme(request)
credentials.credentials == "the-jwt-token"¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auto_error
|
bool
|
If True, raise AuthenticationError when the header is missing or malformed. |
True
|
Source code in src/agenticapi/security.py
__call__
async
¶
Extract Bearer token from the Authorization header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming Starlette request. |
required |
Returns:
| Type | Description |
|---|---|
AuthCredentials | None
|
AuthCredentials with the token, or None. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If the header is missing/malformed and auto_error is True. |
Source code in src/agenticapi/security.py
HTTPBasic¶
HTTPBasic ¶
Extract Basic credentials from the Authorization header.
Expects the format: Authorization: Basic <base64(username:password)>
The extracted credentials string contains the raw password.
The username is stored in AuthCredentials.scopes[0] for
convenience, but the primary use case is passing both to a
verify function.
Example
scheme = HTTPBasic() credentials = await scheme(request)
credentials.credentials == "username:password"¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auto_error
|
bool
|
If True, raise AuthenticationError when the header is missing or malformed. |
True
|
Source code in src/agenticapi/security.py
__call__
async
¶
Extract Basic credentials from the Authorization header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The incoming Starlette request. |
required |
Returns:
| Type | Description |
|---|---|
AuthCredentials | None
|
AuthCredentials with 'username:password' as credentials, or None. |
Raises:
| Type | Description |
|---|---|
AuthenticationError
|
If the header is missing/malformed and auto_error is True. |
Source code in src/agenticapi/security.py
Authenticator¶
Authenticator
dataclass
¶
Combines a security scheme with a verification function.
The scheme extracts raw credentials from the request.
The verify function validates those credentials and returns
an AuthUser (or None if invalid).
Used as the auth= parameter on endpoint decorators
and the AgenticApp constructor.
Example
api_key = APIKeyHeader(name="X-API-Key")
async def verify(creds: AuthCredentials) -> AuthUser | None: if creds.credentials == "secret-key": return AuthUser(user_id="u1", username="alice") return None
auth = Authenticator(scheme=api_key, verify=verify)
@app.agent_endpoint(name="orders", auth=auth) async def orders_agent(intent, context): print(context.user_id) # "u1"
Attributes:
| Name | Type | Description |
|---|---|---|
scheme |
SecurityScheme
|
The security scheme that extracts credentials. |
verify |
Callable[[AuthCredentials], Awaitable[AuthUser | None]]
|
Async function that validates credentials and returns an AuthUser. |