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)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_webhook_signature-0.1.5.tar.gz.
File metadata
- Download URL: philiprehberger_webhook_signature-0.1.5.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53d956262d2ae5bc4c0da03ab07eec754269a9afafe78d47b8fcf6cbf682a681
|
|
| MD5 |
d64a45eb616f49b2c451b1a3f9b8167c
|
|
| BLAKE2b-256 |
ceed3f74027193127b5e3f89e0fc4a4065f0e21cc0f34a8468701c63be44d3f2
|
File details
Details for the file philiprehberger_webhook_signature-0.1.5-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_webhook_signature-0.1.5-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cde42f6da3d5a6df3fa9c1ad4d159321faef33122ea27c4ab59256f2280a390d
|
|
| MD5 |
fc28eaa11131750787e8ecaecbbd7ca9
|
|
| BLAKE2b-256 |
0e1d7b3446fbf1bb8aa456258c80efd18800213993e867069e2dce808b05ad6f
|