Install
Quick Start
One dependency protects your entire API:grant is a fully typed VerifiedGrant dataclass with the principal, agent, scopes, and timestamps.
How It Works
GrantexAuthextracts the Bearer token from theAuthorizationheader- It verifies the RS256 signature against the Grantex JWKS endpoint
- On success, a
VerifiedGrantis returned and injected into your handler .scopes()optionally checks that the grant contains all required scopes- Errors are converted to JSON responses by
grantex_exception_handler
Token Verification Only
UseDepends(grantex) to verify the token without checking scopes:
Scope Enforcement
Via dependency (recommended)
UseDepends(grantex.scopes(...)) to verify the token AND check scopes in one step:
Via function call
Check scopes inside the route handler withrequire_scopes():
Custom Token Extraction
By default, the middleware reads theAuthorization: Bearer <token> header. Override this with token_extractor:
Error Handling
Register the built-in exception handler to return JSON error responses:Error Codes
| Code | HTTP Status | When |
|---|---|---|
TOKEN_MISSING | 401 | No token found in the request |
TOKEN_INVALID | 401 | JWT signature or format is invalid |
TOKEN_EXPIRED | 401 | JWT exp claim is in the past |
SCOPE_INSUFFICIENT | 403 | Token is missing one or more required scopes |
grant Reference
The VerifiedGrant dataclass contains:
| Field | Type | Description |
|---|---|---|
token_id | str | JWT jti claim — unique token identifier |
grant_id | str | Grantex grant record ID |
principal_id | str | End-user who authorized the agent |
agent_did | str | Agent’s decentralized identifier |
developer_id | str | Developer organization ID |
scopes | tuple[str, ...] | Scopes the agent was granted |
issued_at | int | Token issued-at (seconds since epoch) |
expires_at | int | Token expiry (seconds since epoch) |
parent_agent_did | str | None | Parent agent DID (delegated grants only) |
parent_grant_id | str | None | Parent grant ID (delegated grants only) |
delegation_depth | int | None | Delegation depth (0 = root grant) |
Full Example
A complete FastAPI application protected by Grantex:API Reference
GrantexAuth(jwks_uri, *, clock_tolerance=0, audience=None, token_extractor=None)
Creates a Grantex authentication dependency.
| Parameter | Type | Default | Description |
|---|---|---|---|
jwks_uri | str | required | JWKS endpoint URL for token verification |
clock_tolerance | int | 0 | Seconds of clock skew tolerance |
audience | str | None | None | Expected JWT aud claim |
token_extractor | Callable[[Request], str | None] | None | None | Custom function to extract the token |
grantex.scopes(*required_scopes)
Returns a dependency that verifies the token AND checks all required scopes.
require_scopes(grant, *scopes)
Standalone function that checks scopes on an already-verified grant. Raises GrantexFastAPIError with SCOPE_INSUFFICIENT if any scope is missing.
grantex_exception_handler(request, exc)
Starlette exception handler that converts GrantexFastAPIError to a JSON response.
Requirements
- Python 3.9+
- FastAPI >= 0.100.0
grantex>= 0.1.0