|
| 1 | +--- |
| 2 | +title: "Verify webhook signatures" |
| 3 | +description: "Verify Turnkey webhook signatures using the JWKS key discovery endpoint and the @turnkey/crypto SDK helper." |
| 4 | +sidebarTitle: "Verify signatures" |
| 5 | +--- |
| 6 | + |
| 7 | +Turnkey signs webhook deliveries with Ed25519. Verify the signature before parsing or trusting the JSON payload. |
| 8 | + |
| 9 | +<Warning> |
| 10 | +Verification must use the exact raw request body bytes that Turnkey sent. Re-serializing parsed JSON, changing whitespace, or changing key order will cause verification to fail. |
| 11 | +</Warning> |
| 12 | + |
| 13 | +## Signed message format |
| 14 | + |
| 15 | +The signature covers the signature contract fields and the raw body: |
| 16 | + |
| 17 | +```text |
| 18 | +v1.ed25519.<signing_key_id>.<timestamp_ms>.<event_id>.<raw_body> |
| 19 | +``` |
| 20 | + |
| 21 | +The `event_id` segment is the value of the `X-Turnkey-Event-Id` header. Other delivery headers such as organization ID and event type are not part of the signed message. |
| 22 | + |
| 23 | +## Key discovery |
| 24 | + |
| 25 | +Turnkey publishes webhook signing keys at a public JWKS endpoint. Use this endpoint to fetch the Ed25519 public key needed to verify webhook signatures. Match the `kid` field in the response to the `X-Turnkey-Signature-Key-Id` header on each delivery: |
| 26 | + |
| 27 | +```text |
| 28 | +GET https://api.turnkey.com/public/v1/discovery/webhooks/jwks |
| 29 | +``` |
| 30 | + |
| 31 | +This endpoint requires no authentication. The response is a standard [JSON Web Key Set (RFC 7517)](https://datatracker.ietf.org/doc/html/rfc7517) containing Ed25519 public keys: |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "keys": [ |
| 36 | + { |
| 37 | + "kid": "<signing-key-id>", |
| 38 | + "kty": "OKP", |
| 39 | + "crv": "Ed25519", |
| 40 | + "alg": "EdDSA", |
| 41 | + "use": "sig", |
| 42 | + "x": "<base64url-encoded-public-key>", |
| 43 | + "turnkey_signature_algorithm": "ed25519", |
| 44 | + "turnkey_signature_version": "v1" |
| 45 | + } |
| 46 | + ] |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +| Field | Description | |
| 51 | +|---|---| |
| 52 | +| `kid` | Key identifier. Match this against the `X-Turnkey-Signature-Key-Id` delivery header. | |
| 53 | +| `kty` | Key type. `OKP` (Octet Key Pair) for Ed25519 keys. | |
| 54 | +| `crv` | Curve. `Ed25519`. | |
| 55 | +| `alg` | Algorithm. `EdDSA`. | |
| 56 | +| `use` | Key usage. `sig` (signature). | |
| 57 | +| `x` | Base64url-encoded 32-byte Ed25519 public key. | |
| 58 | +| `turnkey_signature_algorithm` | Turnkey-specific. Matches the `X-Turnkey-Signature-Algorithm` header value. | |
| 59 | +| `turnkey_signature_version` | Turnkey-specific. Matches the `X-Turnkey-Signature-Version` header value. | |
| 60 | + |
| 61 | +The JWKS endpoint returns standard `Cache-Control` headers. Cache the response according to those headers and refresh on expiry. If a delivery arrives with a `kid` that does not match any cached key, refetch the JWKS before rejecting the delivery. This avoids stale-cache failures during key rotation while still allowing efficient caching. |
| 62 | + |
| 63 | +## SDK verification helper |
| 64 | + |
| 65 | +The `@turnkey/crypto` package (v2.10.0+) exports `verifyTurnkeyWebhookSignature`, which handles signed-input reconstruction, timestamp freshness, and Ed25519 verification. The helper accepts caller-provided verification keys and returns a typed result instead of throwing. |
| 66 | + |
| 67 | +Fetch the JWKS endpoint, convert each key's base64url-encoded `x` field to hex, and pass the result as `verificationKeys`: |
| 68 | + |
| 69 | +```ts |
| 70 | +import { verifyTurnkeyWebhookSignature } from "@turnkey/crypto"; |
| 71 | + |
| 72 | +const JWKS_URL = |
| 73 | + "https://api.turnkey.com/public/v1/discovery/webhooks/jwks"; |
| 74 | + |
| 75 | +// Fetch and cache the JWKS. Convert base64url public keys to hex. |
| 76 | +async function fetchVerificationKeys() { |
| 77 | + const res = await fetch(JWKS_URL); |
| 78 | + const jwks = await res.json(); |
| 79 | + return jwks.keys.map((key: any) => ({ |
| 80 | + keyId: key.kid, |
| 81 | + publicKey: Buffer.from(key.x, "base64url").toString("hex"), |
| 82 | + algorithm: "ed25519" as const, |
| 83 | + })); |
| 84 | +} |
| 85 | + |
| 86 | +const verificationKeys = await fetchVerificationKeys(); |
| 87 | + |
| 88 | +// In your webhook handler: |
| 89 | +const rawBody = await request.text(); |
| 90 | + |
| 91 | +const result = verifyTurnkeyWebhookSignature({ |
| 92 | + headers: request.headers, |
| 93 | + body: rawBody, |
| 94 | + verificationKeys, |
| 95 | + maxTimestampAgeMs: 5 * 60 * 1000, // 5-minute replay window |
| 96 | +}); |
| 97 | + |
| 98 | +if (!result.ok) { |
| 99 | + // result.reason describes the failure (e.g. "stale_timestamp", "missing_key") |
| 100 | + return new Response("Invalid signature", { status: 401 }); |
| 101 | +} |
| 102 | + |
| 103 | +// Signature verified. Safe to parse. |
| 104 | +const payload = JSON.parse(rawBody); |
| 105 | +``` |
| 106 | + |
| 107 | +<Warning> |
| 108 | +Always read the raw request body before any middleware parses it. Frameworks like Express require `express.raw({ type: "application/json" })` to preserve the original bytes. If your framework has already parsed the body, the re-serialized output may differ from what Turnkey signed. |
| 109 | +</Warning> |
| 110 | + |
| 111 | +## Manual verification |
| 112 | + |
| 113 | +If you are not using the TypeScript SDK, verify signatures manually: |
| 114 | + |
| 115 | +1. Fetch the JWKS from `https://api.turnkey.com/public/v1/discovery/webhooks/jwks`. |
| 116 | +2. Find the key whose `kid` matches the `X-Turnkey-Signature-Key-Id` header. Base64url-decode the `x` field to obtain the 32-byte Ed25519 public key. |
| 117 | +3. Reconstruct the signed input by concatenating the header values and raw body in the format: `v1.ed25519.<key_id>.<timestamp_ms>.<event_id>.<raw_body>`. |
| 118 | +4. Hex-decode the `X-Turnkey-Signature` header to obtain the 64-byte Ed25519 signature. |
| 119 | +5. Verify the Ed25519 signature over the signed input bytes using the public key from step 2. |
| 120 | +6. Check that `X-Turnkey-Timestamp` is within an acceptable freshness window (e.g. 5 minutes) to guard against replay attacks. |
0 commit comments