Skip to content

Commit 16dc5f8

Browse files
committed
docs(python): sign canonical JSON in webhook verification example
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
1 parent 7ae31ec commit 16dc5f8

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

examples/python.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ For the specific event names for your product, see the [product guides](../READM
4545
import rtms
4646
import hmac
4747
import hashlib
48+
import json
4849
import os
4950

50-
def verify_signature(body: str, timestamp: str, signature: str) -> bool:
51+
def verify_signature(payload: dict, timestamp: str, signature: str) -> bool:
52+
body = json.dumps(payload, separators=(",", ":"), ensure_ascii=False)
5153
message = f"v0:{timestamp}:{body}"
5254
expected = "v0=" + hmac.new(
5355
os.environ["ZM_RTMS_WEBHOOK_SECRET"].encode(),
@@ -61,7 +63,7 @@ def handle_webhook(payload, request, response):
6163
signature = request.headers.get('x-zm-signature', '')
6264
timestamp = request.headers.get('x-zm-request-timestamp', '')
6365

64-
if not verify_signature(str(payload), timestamp, signature):
66+
if not verify_signature(payload, timestamp, signature):
6567
response.set_status(401)
6668
response.send({'error': 'Unauthorized'})
6769
return
@@ -78,6 +80,8 @@ def handle_webhook(payload, request, response):
7880
rtms.run()
7981
```
8082

83+
Zoom signs the canonical JSON body, the same string JavaScript produces with `JSON.stringify(payload)`. The two `json.dumps` flags reproduce that exact string: `separators=(",", ":")` drops the whitespace Python adds after `:` and `,`, and `ensure_ascii=False` keeps non-ASCII characters as literals instead of `\uXXXX` escapes. Passing `str(payload)` instead hashes Python's `dict` repr (single quotes), so the HMAC never matches and every webhook is rejected with a 401.
84+
8185
## Context Manager
8286

8387
Use `with rtms.Client() as client:` to ensure `leave()` is always called — even if an exception occurs:

0 commit comments

Comments
 (0)