Skip to main content

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

  1. Extracts Agentity-Token, Agentity-Nonce, Agentity-Timestamp headers
  2. Validates nonce uniqueness (anti-replay)
  3. Checks timestamp is within tolerance window (default ±300s)
  4. Decodes and verifies the Ed25519 AID signature
  5. If registry_url is set, checks GET /did/{did}/status for active status
  6. Injects request.state.agent_did and request.state.agent_scopes
  7. Returns 400 (malformed), 401 (missing), or 403 (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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentity_middleware_python-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentity_middleware_python-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file agentity_middleware_python-0.1.1.tar.gz.

File metadata

File hashes

Hashes for agentity_middleware_python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5706e6fd8e1a4cad060d59508a38a890d81115d4deeae7f839b193f598c29918
MD5 59c16ddcee4eb9ed314c4c4cb1c0bda5
BLAKE2b-256 a8ebe26b2dcf27f0414b8a6dc7a72cb2c979fe7e1bafda5b1abfded416d0c74e

See more details on using hashes here.

File details

Details for the file agentity_middleware_python-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for agentity_middleware_python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27fe44be2aa9fc25cce1213ae1b6249441ac3027d0c01599bea7900e50e96339
MD5 53d9725b37926fe05e4f488438bc31c9
BLAKE2b-256 5e2eb826d78d260deb2cd7daaa95eea00a739f417b8f0a516c1a88f019ac1fa7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page