Skip to content

Commit 6e6d782

Browse files
fix(sdk): webhooks-django × codex-baseline — The agent hand-rolled HMAC signature verification (parsing a… (#736)
Co-authored-by: workos-sdk-automation[bot] <255426317+workos-sdk-automation[bot]@users.noreply.github.com>
1 parent 6ec5526 commit 6e6d782

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,36 @@ The client exposes the WorkOS API through typed namespace properties:
9191
| `client.actions` | AuthKit Actions signature verification and response signing |
9292
| `client.pkce` | PKCE code verifier/challenge helpers |
9393

94+
## Webhook Signature Verification
95+
96+
Use `client.webhooks.verify_event()` rather than hand-rolling the HMAC check — it implements the exact scheme WorkOS signs with, and returns the deserialized event:
97+
98+
```python
99+
from django.http import HttpResponse
100+
101+
from workos import WorkOSClient
102+
103+
client = WorkOSClient(api_key="sk_1234", client_id="client_1234")
104+
105+
# In a Django view (Flask: request.get_data() / request.headers)
106+
def workos_webhook(request):
107+
try:
108+
event = client.webhooks.verify_event(
109+
event_body=request.body, # raw bytes, never a re-serialized dict
110+
event_signature=request.headers["WorkOS-Signature"],
111+
secret="wh_secret_1234", # endpoint secret from the WorkOS dashboard
112+
)
113+
except ValueError:
114+
return HttpResponse(status=400) # invalid signature or stale timestamp
115+
116+
print(event.event, event.id)
117+
return HttpResponse(status=200)
118+
```
119+
120+
`verify_header()` does the same check without deserializing, for payloads you want to handle yourself.
121+
122+
The `WorkOS-Signature` header is formatted `t=<unix-timestamp-ms>, v1=<hex-sha256>` — note the `, ` separator — and `v1` is the HMAC-SHA256 of `"{timestamp}.{raw body}"` keyed with the endpoint secret. Events older (or newer) than `tolerance` seconds are rejected; it defaults to 180. Verification is local and synchronous, so it works the same on `AsyncWorkOSClient`.
123+
94124
## Pagination
95125

96126
Paginated endpoints return `SyncPage[T]` (or `AsyncPage[T]`) with built-in auto-pagination:

0 commit comments

Comments
 (0)