Agentity middleware for FastAPI
Project description
agentity-middleware-python
FastAPI middleware for the Agentity Protocol.
Automatically verify Agentity-signed requests — extract agent identity, validate nonce and timestamp, check status via registry, and inject request.state.agent_did + request.state.agent_scopes.
Installation
pip install agentity-middleware-python
Usage
from fastapi import FastAPI, Request
from agentity_middleware_python import AgentityMiddleware
app = FastAPI()
# Add middleware (optional registry check)
app.add_middleware(
AgentityMiddleware,
registry_url="http://localhost:8000", # Optional: verify DID status
tolerance_seconds=300, # Default: 5 min
)
@app.get("/api/data")
async def get_data(request: Request):
# Verified agent info automatically injected
agent_did = request.state.agent_did # "did:agentity:agent:..."
agent_scopes = request.state.agent_scopes # ["stripe:payments:read", ...]
return {"agent": agent_did, "scopes": agent_scopes}
What it does
- Extracts
Agentity-Token,Agentity-Nonce,Agentity-Timestampheaders - Validates nonce uniqueness (anti-replay)
- Checks timestamp is within tolerance window (default ±300s)
- Decodes and verifies the Ed25519 AID signature
- If
registry_urlis set, checksGET /did/{did}/statusfor active status - Injects
request.state.agent_didandrequest.state.agent_scopes - Returns
400(malformed),401(missing), or403(invalid/revoked) on failure
Error responses
| Status | Condition |
|---|---|
| 400 | Missing or malformed headers |
| 401 | Token not provided |
| 403 | Invalid signature, expired, revoked, or scope violation |
Tests
pip install -e .[dev]
pytest ../tests/
License: Apache 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agentity_middleware_python-0.1.1.tar.gz.
File metadata
- Download URL: agentity_middleware_python-0.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5706e6fd8e1a4cad060d59508a38a890d81115d4deeae7f839b193f598c29918
|
|
| MD5 |
59c16ddcee4eb9ed314c4c4cb1c0bda5
|
|
| BLAKE2b-256 |
a8ebe26b2dcf27f0414b8a6dc7a72cb2c979fe7e1bafda5b1abfded416d0c74e
|
File details
Details for the file agentity_middleware_python-0.1.1-py3-none-any.whl.
File metadata
- Download URL: agentity_middleware_python-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27fe44be2aa9fc25cce1213ae1b6249441ac3027d0c01599bea7900e50e96339
|
|
| MD5 |
53d9725b37926fe05e4f488438bc31c9
|
|
| BLAKE2b-256 |
5e2eb826d78d260deb2cd7daaa95eea00a739f417b8f0a516c1a88f019ac1fa7
|