Available on crate feature
webhook only.Expand description
Webhook signature verification for common providers, gated behind the
webhook Cargo feature (hmac + sha2 — the same RustCrypto crates
already used by crate::auth and crate::crypto).
Every webhook-receiving handler needs to verify that a request actually
came from the provider, not an attacker who guessed (or found leaked) the
endpoint URL. All three schemes below are HMAC-SHA256-based, but the
header format, signed-payload construction, and encoding differ per
provider — verify_webhook_signature and its per-provider siblings
encode the exact convention each one uses, so handlers don’t have to
re-derive it from provider docs.
§Example
use rust_web_server::webhook::{verify_webhook_signature, WebhookProvider};
fn handle_github_webhook(body: &[u8], signature_header: &str, secret: &[u8]) -> bool {
verify_webhook_signature(WebhookProvider::GitHub, body, secret, signature_header)
}§Which header to read
| Provider | Header | Format |
|---|---|---|
| GitHub | X-Hub-Signature-256 | sha256=<hex> |
| Shopify | X-Shopify-Hmac-Sha256 | <base64> |
| Stripe | Stripe-Signature | t=<unix_ts>,v1=<hex>[,v1=<hex>...] |
Enums§
- Webhook
Provider - Which provider’s signature convention to verify a webhook request against.
Constants§
- STRIPE_
DEFAULT_ TOLERANCE_ SECS - Default timestamp tolerance for
verify_stripe_signature— 300 seconds (5 minutes), matching Stripe’s own recommended default. Requests signed further in the past (or future, guarding against clock skew abuse) than this are rejected as a replay-protection measure.
Functions§
- verify_
github_ signature - Verifies a GitHub webhook’s
X-Hub-Signature-256header value —"sha256=<hex-encoded-hmac-sha256-of-body>", keyed by the webhook’s configured secret. - verify_
shopify_ signature - Verifies a Shopify webhook’s
X-Shopify-Hmac-Sha256header value — the standard base64 encoding of an HMAC-SHA256 digest of the raw body, keyed by the app’s configured secret. - verify_
stripe_ signature - Verifies a Stripe webhook’s
Stripe-Signatureheader using the defaultSTRIPE_DEFAULT_TOLERANCE_SECStimestamp tolerance. Seeverify_stripe_signature_with_tolerancefor the full format and to customize the tolerance window. - verify_
stripe_ signature_ with_ tolerance - Verifies a Stripe webhook’s
Stripe-Signatureheader value:"t=<unix_timestamp>,v1=<hex_hmac_sha256>[,v1=<hex_hmac_sha256>...]". - verify_
webhook_ signature - Verifies
header_valueagainstbodyandsecretusingprovider’s signature convention. Returnsfalseon any failure: wrong secret, tampered body, malformed header, or (Stripe only) a timestamp outside the default tolerance window.