import hashlib, hmac, time
def verify(secret: str, body: bytes, header: str, tolerance: int = 300) -> bool:
parts = dict(p.split("=", 1) for p in header.split(","))
timestamp, signature = parts.get("t", "0"), parts.get("v1", "")
if abs(time.time() - int(timestamp)) > tolerance:
return False
expected = hmac.new(
secret.encode(), f"{timestamp}.".encode() + body, hashlib.sha256
).hexdigest()
return hmac.compare_digest(expected, signature)
# In your handler (e.g. FastAPI):
# verify(WEBHOOK_SECRET, await request.body(), request.headers["X-CompliAPI-Signature"])