Skip to main content

construct_event

Function construct_event 

Source
pub fn construct_event<H: WebhookHeaders + ?Sized>(
    raw_body: impl AsRef<[u8]>,
    headers: &H,
    secret: &str,
    options: &WebhookVerifyOptions,
) -> Result<WebhookPayload>
Expand description

Verifies a webhook and reads it.

Order: signature, timestamp, JSON, payloadVersion, envelope. Returns WebhookPayload::Event, or WebhookPayload::WrongPayloadVersion for a correctly signed body in another payload version. Fails with Error::WebhookVerification when verification fails (answer 400) and with Error::Decode when a verified body cannot be read.

use finlight::webhooks::{self, WebhookPayload, WebhookVerifyOptions};

fn handle(body: &[u8], headers: &http::HeaderMap, secret: &str) -> u16 {
    match webhooks::construct_event(body, headers, secret, &WebhookVerifyOptions::new()) {
        Ok(WebhookPayload::Event(event)) => {
            println!("{} {}", event.event_id, event.data.title);
            200
        }
        Ok(_) => 200, // WrongPayloadVersion and future outcomes: answer 2xx
        Err(_) => 400, // never 410
    }
}