import hashlib import hmac import json import time def verify_webhook(body: bytes, signature: str, timestamp: str, secret: str) -> bool: # Reject requests older than 5 minutes try: ts = int(timestamp) except (ValueError, TypeError): return False if abs(time.time() - ts) > 300: return False payload = json.loads(body) message = f"{timestamp}.{json.dumps(payload, separators=(',', ':'), sort_keys=True)}" expected = hmac.new(secret.encode(), message.encode(), hashlib.sha256).hexdigest() return hmac.compare_digest(expected, signature)