FastAPI-Topaz¶
Authorization framework for FastAPI using Topaz.
Add fine-grained authorization to your FastAPI application with native dependency injection, caching, circuit breakers, and observability built in.
flowchart LR
subgraph Your App
F[FastAPI<br/>Endpoints]
end
subgraph fastapi-topaz
D[Dependencies<br/>or Middleware]
C[Cache]
CB[Circuit<br/>Breaker]
end
subgraph Topaz
A[Authorizer<br/>Rego Policies]
DS[Directory<br/>Relationships]
end
F --> D
D <--> C
D <--> CB
CB --> A
A <--> DS
style D fill:#6366f1,color:#fff
style C fill:#818cf8,color:#fff
style CB fill:#818cf8,color:#fff
Quick Start¶
from fastapi import Depends, FastAPI, Request
from fastapi_topaz import (
AuthorizerOptions,
Identity,
IdentityType,
TopazConfig,
require_rebac_allowed,
)
config = TopazConfig(
authorizer_options=AuthorizerOptions(url="localhost:8282"),
policy_path_root="myapp",
identity_provider=lambda r: Identity(
type=IdentityType.IDENTITY_TYPE_SUB, value=r.state.user_id
),
policy_instance_name="myapp",
)
app = FastAPI()
@app.get("/documents/{id}")
async def get_document(
id: int,
request: Request,
_: None = Depends(require_rebac_allowed(config, "document", "can_read")),
):
return {"id": id, "title": "My Document"}
Installation¶
Features¶
-
RBAC, ABAC & ReBAC
Support for all authorization models: role-based, attribute-based, and relationship-based. Combine them in a single Rego policy for fine-grained control.
-
Dependencies & Middleware
Native FastAPI integration via
Depends()or globalTopazMiddleware. Auto-generate policy paths or specify them explicitly. -
Decision Caching
TTL-based caching reduces Topaz calls by 90%+. Configurable per policy path with automatic invalidation.
-
Circuit Breaker
Graceful degradation when Topaz is unavailable. Serve stale cache, fail-open, or fail-closed.
-
Observability
Prometheus metrics and OpenTelemetry tracing. Monitor latency, cache hit rates, and circuit state.
-
Audit Logging
Structured JSON logs for every authorization decision. Compliance-ready with request correlation.
Start Here¶
-
New to fastapi-topaz?
Start with the Getting Started tutorial to add authorization in 15 minutes.
-
Want a complete example?
Follow the Example App tutorials with real OIDC and database setup.
-
Already using it?
Check the How-to Guides for specific tasks and the API Reference.
Documentation Structure¶
This documentation follows the Diataxis framework:
| Section | Purpose | Start Here |
|---|---|---|
| Tutorials | Learning-oriented, step-by-step guides | Getting Started |
| How-to Guides | Task-oriented, solve specific problems | Identity Providers |
| Reference | Technical specifications | API Reference |
| Explanation | Understanding-oriented, concepts | Architecture |
Feature Status¶
| Feature | Status | Documentation |
|---|---|---|
| Policy-based authorization | API Reference | |
| ReBAC (relationship-based) | API Reference | |
| Auto policy path resolution | API Reference | |
| Decision caching | API Reference | |
| Circuit breaker | Circuit Breaker | |
| Connection pooling | Connection Pooling | |
| Authorization middleware | Middleware | |
| Audit logging | Audit Logging | |
| Prometheus metrics | Observability | |
| OpenTelemetry tracing | Observability | |
| Testing utilities | Testing | |
| CLI tools | CLI Reference | |
| Resource hierarchy | Resource Hierarchy | |
| Integration testing | Integration Testing |
Requirements¶
- Python 3.9+
- FastAPI 0.100+
- Running Topaz instance