Skip to main content

Cross-language HMAC-SHA256 request signing with a defined canonical string format.

Project description

hardenlabs-hmac

Cross-language HMAC-SHA256 request signing with a defined canonical string format. Guaranteed identical signatures across C#, Python, TypeScript, and Go.

Installation

pip install hardenlabs-hmac
pip install "hardenlabs-hmac[fastapi]"   # for FastAPI middleware
pip install "hardenlabs-hmac[requests]"  # for requests library support

Quick Start — Server (FastAPI)

from fastapi import Depends, FastAPI, Request
from hardenlabs_hmac import HmacValidate, install_hmac_exception_handler
from hardenlabs_hmac.config import HmacConfig, HmacClientIdentity, SignedHeadersConfig

config = HmacConfig(
    signed_headers=SignedHeadersConfig.default(),
    timestamp_tolerance_seconds=30,
    clients={
        "order-service": HmacClientIdentity(shared_secret="orders-base64-secret"),
    },
)

hmac_validate = HmacValidate(config)

app = FastAPI()
install_hmac_exception_handler(app)

# Protected — requires valid HMAC signature
@app.get("/api/hello")
async def hello(request: Request, _hmac: None = Depends(hmac_validate)):
    return {"message": "Authenticated!"}

# Unprotected — no dependency, no HMAC required
@app.get("/health")
async def health():
    return {"status": "healthy"}

Routes without Depends(hmac_validate) are not validated. Use the global HardenHmacMiddleware instead if you want all routes validated.

Public API — Server

Symbol Description
HmacValidate(config, secret_resolver=None) Per-route dependency for Depends()
install_hmac_exception_handler(app) Register error handler (call once per app)
HmacValidationHttpError Exception raised on validation failure
HardenHmacMiddleware Global middleware (validates all routes)

Quick Start — Client

from hardenlabs_hmac.client import HmacClientFactory
from hardenlabs_hmac.config import HmacConfig, HmacTargetConfig

config = HmacConfig(
    targets={
        "my-service": HmacTargetConfig(
            base_url="https://api.example.com",
            shared_secret="your-base64-encoded-secret",
        ),
    },
)

factory = HmacClientFactory(config)
with factory.create_sync_client("my-service") as client:  # requires [httpx]; or create_requests_session() with [requests]
    response = client.get("/api/hello")  # automatically signed

Documentation

Full documentation, canonical string specification, and cross-language compatibility details: github.com/HardenLabs/HardenHMAC

License

Apache License 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

hardenlabs_hmac-1.0.1.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

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

hardenlabs_hmac-1.0.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file hardenlabs_hmac-1.0.1.tar.gz.

File metadata

  • Download URL: hardenlabs_hmac-1.0.1.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for hardenlabs_hmac-1.0.1.tar.gz
Algorithm Hash digest
SHA256 dd61071191f3dd42504f78d76df5002ec0d4492ff6b64d17fd1f3541999dd6b4
MD5 e1b9537c83e505f81d6b61b98cc4e969
BLAKE2b-256 b3985c48b899592ece61b8c087aac41cffa89a8bf9bd0b5b829c918e10a5200d

See more details on using hashes here.

File details

Details for the file hardenlabs_hmac-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for hardenlabs_hmac-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8d5303995967b8172d99c92a026f0f8ef4a18cfa328a9c31ba11fcc478d8a12b
MD5 2675633f8e85c60ffdcde4e4fc986319
BLAKE2b-256 b15029a692072a68ec1ad7a7918ed7597fd265de7ebcdc885d36ca54b6be27c5

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