Skip to main content

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

Project description

philiprehberger-webhook-signature

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)

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.1.tar.gz (3.5 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.1.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.1.tar.gz
Algorithm Hash digest
SHA256 39449447a02a35ef26cbf98e5d9ab4f17fe1ad7c31560f340adf7ae91fcedcf9
MD5 4c9912f5fd5c455810da2397f8335dcb
BLAKE2b-256 c3c5f2028a2264d696d1314e656be5ecbdf61adc8565abcbcf84b35c22acf473

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_webhook_signature-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ebe0d25f7cade566262bf4403e66102b135f262500ad4132c73ad36abc18a22f
MD5 7e07ba4c51d23b66cdd7aa45861980a2
BLAKE2b-256 922beeaaa9fd5d79e05e2752bd1dc4032a9d41454accab4c0f74c4bfc65d5691

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