rocket-webhook 0.1.0-alpha.2

Webhook validation for Rocket applications
Documentation

Rocket Webhook

CI Crates.io Version

⚠️ This crate is in alpha and may not work as expected yet.

Streamlined webhook validation for Rocket applications, with built-in support for popular providers.

Features

  • Automatic signature validation for webhook requests
  • Easy Rocket integration using .manage() and data guards
  • Deserialize JSON payloads or work with the raw responses
  • Built-in support for popular webhook providers and signatures
  • Automatic timestamp validation for replay attack prevention

Supported Webhooks

  • GitHub, Stripe, Slack, Shopify, Discord, SendGrid, Svix

You can use another webhook by utilizing one of the generic implementations, or implementing one of the signature traits (WebhookHmac or WebhookPublicKey) along with the Webhook trait. See the src/webhooks/built_in folder for examples.

Quick Start

Add to your Cargo.toml:

[dependencies]
rocket-webhook = { version = "0.1.0-alpha", features = ["github", "slack"] } # Enable provider(s) you want to use
serde = { version = "1.0", features = ["derive"] }
use rocket::{post, routes, serde::{Deserialize, Serialize}};
use rocket_webhook::{RocketWebhook, WebhookPayload, webhooks::built_in::GitHubWebhook};

#[derive(Deserialize, Serialize)]
struct GitHubPayload {
    action: String,
}

#[post("/webhooks/github", data = "<payload>")]
async fn github_webhook(
    payload: WebhookPayload<'_, GitHubPayload, GitHubWebhook>,
) -> &'static str {
    println!("Received GitHub action: {}", payload.data.action);
    "OK"
}

#[rocket::launch]
fn rocket() -> _ {
    let github_webhook = RocketWebhook::builder()
        .webhook(GitHubWebhook::with_secret(b"your-webhook-secret"))
        .build();

    rocket::build()
        .manage(github_webhook)
        .mount("/", routes![github_webhook])
}