Skip to main content

HMAC-based webhook signature generation and verification with timing-safe comparison

Project description

philiprehberger-webhook-signature

Tests PyPI version License

HMAC-based webhook signature generation and verification with timing-safe comparison.

Installation

pip install philiprehberger-webhook-signature

Usage

Signing a Payload

from philiprehberger_webhook_signature import sign

signed = sign(payload='{"event": "order.created"}', secret="whsec_abc123")

print(signed.signature)   # HMAC hex digest
print(signed.timestamp)   # Unix timestamp
print(signed.to_header()) # "t=1234567890,sha256=abc..."

Verifying a Signature

from philiprehberger_webhook_signature import verify, parse_header

# Parse the signature header
header = request.headers["X-Webhook-Signature"]
signature, timestamp = parse_header(header)

# Verify (raises on failure)
verify(
    payload=request.body,
    secret="whsec_abc123",
    signature=signature,
    timestamp=timestamp,
    max_age=300.0,  # reject signatures older than 5 minutes
)

Error Handling

from philiprehberger_webhook_signature import (
    verify,
    SignatureError,
    SignatureExpiredError,
    SignatureMismatchError,
)

try:
    verify(payload, secret, signature, timestamp)
except SignatureExpiredError as e:
    print(f"Signature too old: {e.age}s > {e.max_age}s")
except SignatureMismatchError:
    print("Invalid signature")
except SignatureError as e:
    print(f"Verification failed: {e}")

Custom Algorithm

signed = sign(payload="data", secret="secret", algorithm="sha512")
verify(payload="data", secret="secret", signature=sig, timestamp=ts, algorithm="sha512")

Disable Expiry Check

verify(payload, secret, signature, timestamp, max_age=None)

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

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

philiprehberger_webhook_signature-0.1.5.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file philiprehberger_webhook_signature-0.1.5.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.5.tar.gz
Algorithm Hash digest
SHA256 53d956262d2ae5bc4c0da03ab07eec754269a9afafe78d47b8fcf6cbf682a681
MD5 d64a45eb616f49b2c451b1a3f9b8167c
BLAKE2b-256 ceed3f74027193127b5e3f89e0fc4a4065f0e21cc0f34a8468701c63be44d3f2

See more details on using hashes here.

File details

Details for the file philiprehberger_webhook_signature-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cde42f6da3d5a6df3fa9c1ad4d159321faef33122ea27c4ab59256f2280a390d
MD5 fc28eaa11131750787e8ecaecbbd7ca9
BLAKE2b-256 0e1d7b3446fbf1bb8aa456258c80efd18800213993e867069e2dce808b05ad6f

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