#webhook #axum #standard #validation

axum-standardwebhooks

Axum extractor that verifies signature for Standard Webhooks

1 stable release

Uses new Rust 2024

new 1.0.0 Mar 14, 2025

#1361 in Web programming

MIT/Apache

15KB
147 lines

axum-standardwebhooks

Crates.io Documentation MIT/Apache-2.0 licensed

A library providing Axum extractors for validating webhooks according to the Standard Webhooks specification.

Features

  • Simple, ergonomic extractor for validating Standard Webhooks signatures
  • Rejects requests with invalid or missing signatures
  • Works with Axum's state management for shared webhook verifier
  • Works with any existing body extractor that implements FromRequest

Installation

Add the dependency to your Cargo.toml:

[dependencies]
axum-standardwebhooks = "1"

Usage

Basic example

use axum::{Router, routing::post, Json};
use axum_standardwebhooks::{StandardWebhook, SharedWebhook, Webhook};
use serde_json::Value;
use std::sync::Arc;
use axum::extract::FromRef;

async fn webhook_handler(StandardWebhook(Json(payload)): StandardWebhook<Json<Value>>) -> String {
    // The webhook signature has been verified, and we can safely use the payload
    format!("Received webhook: {}", payload)
}

#[derive(Clone)]
struct AppState {
    webhook: SharedWebhook,
}

impl FromRef<AppState> for SharedWebhook {
    fn from_ref(state: &AppState) -> Self {
        state.webhook.clone()
    }
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/webhooks", post(webhook_handler))
        .with_state(AppState {
            webhook: SharedWebhook::new(Webhook::new("whsec_C2FVsBQIhrscChlQIMV+b5sSYspob7oD").unwrap()),
        });

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~6–13MB
~144K SLoC