Skip to content

Auth And Security Reference

This page documents auth hook types, approval hooks, framework errors, and signed cookies.

Prerequisites

Read Security. Auth hooks are your application policy; Quater only defines when they run and what they receive.

python
from quater import (
    ActionApproval,
    ApprovalRequest,
    AuthContext,
    AuthRequest,
    HTTPError,
    ImproperlyConfigured,
    SignedCookieSigner,
)

AuthRequest

Added in 0.1.0a1.

Input passed to auth hooks.

python
AuthRequest(
    method: str,
    path: str,
    headers: Mapping[str, str] = <empty read-only mapping>,
    context: RequestContext = RequestContext(),
)
FieldTypeDefaultDescription
methodstrrequiredMethod being accessed.
pathstrrequiredPath being accessed.
headersMapping[str, str]empty read-only mappingNormalized headers. Header names are lowercase.
contextRequestContextRequestContext()Source, entrypoint, request id, tool, and action metadata.

MCP and CLI surface auth receive the surface request first. Route auth= receives a second AuthRequest for the actual route when the route declares auth.

AuthContext

Added in 0.1.0a1.

Authenticated identity returned by auth hooks.

python
AuthContext(subject: str, metadata: Mapping[str, object] = {})
FieldTypeDefaultDescription
subjectstrrequiredStable user, service, or agent id.
metadataMapping[str, object]empty read-only mappingSmall request-scoped values your app wants to carry.

Example:

python
from quater import AuthContext, AuthRequest


async def authenticate(ctx: AuthRequest) -> AuthContext | None:
    if ctx.headers.get("authorization") != "Bearer demo-token":
        return None
    return AuthContext(subject="user_123")

ApprovalRequest

Added in 0.1.0a1.

Input passed to action_approval for protected MCP tools and CLI actions.

python
ApprovalRequest(
    action: str,
    arguments_hash: str,
    token: str,
    auth: AuthContext | None = None,
    context: RequestContext = RequestContext(),
)
FieldTypeDefaultDescription
actionstrrequiredTool or CLI action name.
arguments_hashstrrequiredSHA-256 hash of action name and canonical JSON arguments.
tokenstrrequiredSubmitted approval token.
authAuthContext | NoneNoneAuthenticated caller when available.
contextRequestContextRequestContext()Source and entrypoint metadata.

ActionApproval

Added in 0.1.0a1.

Callable type for approval hooks.

python
ActionApproval = Callable[[ApprovalRequest], Awaitable[bool]]

Return True to allow execution. Return False to deny it.

Authenticate

Added in 0.1.0a1.

Callable type for auth hooks.

python
Authenticate = Callable[[AuthRequest], Awaitable[AuthContext | None]]

Return AuthContext to allow the request. Return None to deny it. Returning any other type is treated as unauthorized.

HTTPError

Added in 0.1.0a1.

Exception converted into an HTTP-style response.

python
HTTPError(detail: str | None = None, *, status_code: int | None = None)
ParameterTypeDefaultDescription
detailstr | NoneNoneClient-facing error text. Defaults to "Internal Server Error".
status_codeint | NoneNoneResponse status. Defaults to 500.

Example:

python
from quater import HTTPError


@app.get("/orders/{order_id}")
async def get_order(order_id: str) -> dict[str, str]:
    raise HTTPError("Order not found", status_code=404)

Expected response:

text
404 Not Found
Order not found

ImproperlyConfigured

Added in 0.1.0a1.

Framework setup error. Catch this when startup should fail loudly.

python
raise ImproperlyConfigured("bad setup")

ConfigurationError remains in quater.exceptions as a compatibility subclass, but new app code should use ImproperlyConfigured.

SignedCookieSigner

Added in 0.1.0a1.

HMAC signer for small cookie values. It signs values; it does not encrypt them.

python
SignedCookieSigner(
    secret: str | bytes,
    *,
    fallback_secrets: Iterable[str | bytes] = (),
    salt: str = "quater.cookie",
)
ParameterTypeDefaultDescription
secretstr | bytesrequiredCurrent signing secret.
fallback_secretsIterable[str | bytes]()Old secrets accepted during rotation.
saltstr"quater.cookie"Purpose-specific signing salt.

Methods:

MethodReturnDescription
sign(value)strReturns a signed cookie value.
verify(signed_value)str | NoneReturns original value or None.

Raises ImproperlyConfigured if secrets are empty.

What Can Go Wrong

Unauthorized : An auth hook returned None.

Approval required : A protected tool or action ran without a valid approval token.

Approval denied : action_approval returned False.

Signed cookie secrets must not be empty : Provide non-empty current and fallback secrets.

Also See

Released under the MIT License.