Metadata-Version: 2.4
Name: konklav-fastapi
Version: 1.7.0
Summary: Konklav route policies for FastAPI: declare requirements on routes and routers, decided by the relying core
License-Expression: MIT
License-File: LICENSE
Classifier: Framework :: FastAPI
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Classifier: Typing :: Typed
Requires-Dist: konklav-relying==1.7.0
Requires-Dist: fastapi>=0.143,<0.163
Requires-Python: >=3.12
Project-URL: Changelog, https://github.com/cstruct/konklav/releases
Project-URL: Documentation, https://github.com/cstruct/konklav/tree/main/items/py-fastapi
Project-URL: Issues, https://github.com/cstruct/konklav/issues
Project-URL: Repository, https://github.com/cstruct/konklav
Description-Content-Type: text/markdown

# konklav-fastapi

[Konklav](https://github.com/cstruct/konklav) route policies for FastAPI.

Declare an operation's requirements as a `RoutePolicy` on routes, routers
and `include_router`; install `KonklavRoutes` once with your started
`RelyingClient` from [`konklav-relying`](https://pypi.org/project/konklav-relying/),
and every request is decided by the relying core with one call.

```python
from fastapi import APIRouter
from konklav_fastapi import KonklavRoutes, RoutePolicy, TenantToken

router = APIRouter(dependencies=[RoutePolicy(tenant_actions={"project:read"})])


@router.get("/projects/{project}", dependencies=[RoutePolicy(scope_actions={"project": {"document:read"}})])
async def read_project(project: str, token: TenantToken) -> dict[str, str]:
    return {"project": project, "subject": token.sub}
```

Startup validation refuses routes without a policy, policies that can't be
decided, and token dependencies the policy doesn't match. Each operation's
merged policy is published in OpenAPI as `x-konklav-policy`.

`RoutePacing(pacing)` paces routes whose response time could reveal
whether a record exists, such as invites and password resets: declared
before the `RoutePolicy`, it answers no sooner than `floor ± jitter`, also
for denials and errors. `PacingMiddleware` paces every answer for chosen
paths, including those FastAPI gives before any dependency runs:

```python
from datetime import timedelta

from konklav_relying import Pacing
from konklav_fastapi import PacingMiddleware, RoutePacing

pacing = Pacing(floor=timedelta(milliseconds=800), jitter=timedelta(milliseconds=200))
router = APIRouter(dependencies=[RoutePacing(pacing), RoutePolicy(tenant_actions={"member:invite"})])
app.add_middleware(PacingMiddleware, pacing=pacing, paths={"/password-resets"})
```

`RouteRateLimit(limit, "{token:sub}/{path:project}")` limits routes with a
`konklav_relying.RateLimit`, keyed by path parameters, token claims or
`{client:address}` (honouring `trusted_proxies`). Refused requests, and
`RateLimited` raised in handlers, are answered with 429 and `Retry-After`.

## Versions

The package has the same version as the Konklav server it was released
with, and requires `konklav-relying` of that version. It is supported only
against a server of the same version.
