import hashlib
import hmac
import time
TOLERANCE_SECONDS = 300
def verify_marky_webhook(secret: str, raw_body: bytes, timestamp: str, signature: str) -> bool:
if not (timestamp.isascii() and timestamp.isdigit()) or abs(time.time() - int(timestamp)) > TOLERANCE_SECONDS:
return False
signed = f"{timestamp}.".encode() + raw_body
expected = "sha256=" + hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, signature)
# Flask example
# @app.post("/webhook")
# def webhook():
# ok = verify_marky_webhook(
# SECRET,
# request.get_data(), # raw bytes, not request.json
# request.headers["X-Marky-Timestamp"],
# request.headers["X-Marky-Signature"],
# )
# if not ok:
# abort(400)