Skip to main content

Python server-side middleware for BPC (Bound Pair Credentials) — FastAPI and Flask request verification

Project description

bpc-server

Python server-side middleware for the BPC (Bound Pair Credentials) protocol. Verifies BPC-signed requests in FastAPI and Flask applications using the full 12-step pipeline from the BPC spec v1.0.

Install

pip install bpc-server[fastapi]   # FastAPI + Starlette
pip install bpc-server[flask]     # Flask
pip install bpc-server[all]       # Both

FastAPI

from fastapi import FastAPI, Request
from bpc_server import BPCFastAPIMiddleware, InMemoryPairRegistry, PairRecord

app = FastAPI()
registry = InMemoryPairRegistry()

# Register a pair (in production, load from your database)
registry.register(PairRecord(
    pair_id="pair_abc123",
    name="my-agent",
    scope="read-write",
    mode="development",
    public_key_jwk={...},  # from bpc-client registration
    secret_hash="argon2id_hash_here",
))

app.add_middleware(BPCFastAPIMiddleware, registry=registry)

@app.get("/api/data")
async def get_data(request: Request):
    pair = request.state.bpc_pair  # BPCVerificationResult
    return {"pair_id": pair.pair_id, "scope": pair.pair.scope}

Flask

from flask import Flask, g
from bpc_server import BPCFlaskMiddleware, InMemoryPairRegistry

app = Flask(__name__)
registry = InMemoryPairRegistry()
BPCFlaskMiddleware(app, registry=registry)

@app.route("/api/data")
def get_data():
    return {"pair_id": g.bpc_pair_id}

Standalone Verifier

from bpc_server import BPCVerifier, InMemoryPairRegistry, InMemoryNonceStore

verifier = BPCVerifier(registry=registry, nonce_store=InMemoryNonceStore())
result = verifier.verify(headers=request.headers, method="GET", path="/api/data")
if not result.ok:
    return 401, result.error_code

12-Step Verification Pipeline

  1. Headers present (X-BPC-Pair-ID, X-BPC-Signature, X-BPC-Signed-Data, X-BPC-Version)
  2. Pair exists and is active (not revoked, not expired)
  3. Pair not locked out
  4. Decode and parse canonical payload
  5. Protocol version check ("1.0")
  6. Timestamp within ±60s window
  7. Nonce not seen before (replay prevention)
  8. Method and path match payload
  9. Body hash match (SHA-256)
  10. ECDSA-SHA-256 signature valid
  11. Scope enforcement (read / read-write / admin)

See the full spec.

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

bpc_server-1.0.0.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

bpc_server-1.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file bpc_server-1.0.0.tar.gz.

File metadata

  • Download URL: bpc_server-1.0.0.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc1

File hashes

Hashes for bpc_server-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5d24acaf62466c8692be471030dee74079304c89a7e428bb735f584dccbc1caf
MD5 1f931707d3c39f1ac4ff225ce9fd91dc
BLAKE2b-256 3dbf646f46fae3060b5c2073979d058aa6ae8704ae89d9068166081e827c8b38

See more details on using hashes here.

File details

Details for the file bpc_server-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bpc_server-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc1

File hashes

Hashes for bpc_server-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9dba93cba902bee3b67cc9d9887a250c870642d840d78cd0684ab124f8b645a6
MD5 56c6f4eba53259921152233a74abb7bf
BLAKE2b-256 9eab7458524e1bdc6389b64e202d5ad2d5e920156a4d7bbe9e5f6621e5019908

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