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)

API

Function / Class Description
sign(payload, secret, algorithm, timestamp) Generate an HMAC signature for a webhook payload
verify(payload, secret, signature, timestamp, algorithm, max_age) Verify a webhook signature with timing-safe comparison
parse_header(header, prefix) Parse a signature header string into (signature, timestamp) tuple
SignedPayload Signed payload with signature, timestamp, body, and to_header()
SignatureError Base exception for signature errors
SignatureExpiredError Raised when signature age exceeds max_age
SignatureMismatchError Raised when signature verification fails

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.6.tar.gz (4.1 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.6.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1c0590106b1178de9bcb60b8d5d6eac4d57366bcd94729a0efddbcf306b24eab
MD5 e1f52808a8854a8500e4a25a17aedc15
BLAKE2b-256 16b36fd4ff5e0d38a21ecd9fd2e5d084b9e86fcf22ada963d1ca96de44f85b01

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca52d91e02730d6ff628a2afe5f53b4e2e679f701f5b8fc0b5e5661dd0b3605
MD5 575cac7e1e10964920d7042327f6bcec
BLAKE2b-256 f38b1e1500558b154ff18e652912aaf068028ca5f5d7de50368ff94ca5f73158

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