Skip to content

Commit

Permalink
feat: parse a webhook event without the secret
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed Sep 17, 2024
1 parent df8a348 commit 8b5a6ee
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions async-stripe-webhook/src/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ pub struct Webhook {
}

impl Webhook {
/// Construct an event from a webhook payload, **ignoring the secret**.
///
/// This method is considered insecure and intended for early-stage local testing only.
/// Use [construct_event](Self::construct_event) for production instead.
///
/// # Errors
///
/// This function will return a WebhookError if the payload could not be parsed
pub fn insecure(payload: &str) -> Result<Event, WebhookError> {
Self { current_timestamp: 0 }.parse_payload(payload)
}

/// Construct an event from a webhook payload and signature.
///
/// # Errors
Expand All @@ -60,6 +72,7 @@ impl Webhook {
/// - the provided signature is invalid
/// - the provided secret is invalid
/// - the signature timestamp is older than 5 minutes
/// - the payload could not be parsed
pub fn construct_event(payload: &str, sig: &str, secret: &str) -> Result<Event, WebhookError> {
Self { current_timestamp: Utc::now().timestamp() }.do_construct_event(payload, sig, secret)
}
Expand All @@ -76,6 +89,7 @@ impl Webhook {
/// - the provided signature is invalid
/// - the provided secret is invalid
/// - the signature timestamp is older than 5 minutes from the provided timestamp
/// - the payload could not be parsed
pub fn construct_event_with_timestamp(
payload: &str,
sig: &str,
Expand Down Expand Up @@ -109,6 +123,10 @@ impl Webhook {
return Err(WebhookError::BadTimestamp(signature.t));
}

self.parse_payload(payload)
}

fn parse_payload(self, payload: &str) -> Result<Event, WebhookError> {
let base_evt: stripe_shared::Event = miniserde::json::from_str(payload)
.map_err(|_| WebhookError::BadParse("could not deserialize webhook event".into()))?;

Expand Down

0 comments on commit 8b5a6ee

Please sign in to comment.