|
| 1 | +""" |
| 2 | +Customer webhook receiver that verifies qhook signatures. |
| 3 | +Demonstrates how your customers verify outbound webhook deliveries. |
| 4 | +No dependencies beyond Python stdlib. |
| 5 | +""" |
| 6 | +from http.server import HTTPServer, BaseHTTPRequestHandler |
| 7 | +import hashlib |
| 8 | +import hmac |
| 9 | +import json |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +# Set this to the signing_secret returned when creating the endpoint |
| 14 | +SIGNING_SECRET = sys.argv[1] if len(sys.argv) > 1 else "" |
| 15 | + |
| 16 | + |
| 17 | +class Handler(BaseHTTPRequestHandler): |
| 18 | + def do_POST(self): |
| 19 | + length = int(self.headers.get("Content-Length", 0)) |
| 20 | + body = self.rfile.read(length) |
| 21 | + |
| 22 | + # Extract signature headers |
| 23 | + signature = self.headers.get("X-Qhook-Signature", "") |
| 24 | + timestamp = self.headers.get("X-Qhook-Timestamp", "") |
| 25 | + event_type = self.headers.get("X-Qhook-Event-Type", "") |
| 26 | + event_id = self.headers.get("X-Qhook-Event-ID", "") |
| 27 | + delivery_id = self.headers.get("X-Qhook-Delivery-ID", "") |
| 28 | + |
| 29 | + # Verify signature |
| 30 | + verified = False |
| 31 | + if SIGNING_SECRET and signature.startswith("v1="): |
| 32 | + expected = hmac.new( |
| 33 | + SIGNING_SECRET.encode(), |
| 34 | + f"{timestamp}.".encode() + body, |
| 35 | + hashlib.sha256, |
| 36 | + ).hexdigest() |
| 37 | + verified = hmac.compare_digest(signature[3:], expected) |
| 38 | + |
| 39 | + payload = json.loads(body) if body else {} |
| 40 | + status = "VERIFIED" if verified else ("UNVERIFIED" if SIGNING_SECRET else "NO_SECRET") |
| 41 | + |
| 42 | + print(f"[{status}] event_type={event_type} event_id={event_id} " |
| 43 | + f"delivery_id={delivery_id} payload={json.dumps(payload)}") |
| 44 | + |
| 45 | + self.send_response(200) |
| 46 | + self.send_header("Content-Type", "application/json") |
| 47 | + self.end_headers() |
| 48 | + self.wfile.write(b'{"status":"ok"}') |
| 49 | + |
| 50 | + def log_message(self, format, *args): |
| 51 | + pass # suppress default access logs |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + port = 9000 |
| 56 | + server = HTTPServer(("0.0.0.0", port), Handler) |
| 57 | + print(f"Customer webhook receiver listening on :{port}") |
| 58 | + if SIGNING_SECRET: |
| 59 | + print(f"Verifying signatures with secret: {SIGNING_SECRET[:12]}...") |
| 60 | + else: |
| 61 | + print("No signing secret provided -- pass it as: python3 receiver.py whsec_...") |
| 62 | + server.serve_forever() |
0 commit comments