From aade5289455caaf9f706defbab913397d593a386 Mon Sep 17 00:00:00 2001 From: Sean Pianka Date: Sun, 7 May 2023 19:40:37 -0400 Subject: [PATCH] feat(webhook_events): support construct_event with timestamp This is a testing utility constructor which allows for replaying requests with an older timestamp. Signed-off-by: Sean Pianka --- src/resources/webhook_events.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/resources/webhook_events.rs b/src/resources/webhook_events.rs index 656ab9c7f..92725edd1 100644 --- a/src/resources/webhook_events.rs +++ b/src/resources/webhook_events.rs @@ -485,6 +485,8 @@ pub struct Webhook { #[cfg(feature = "webhook-events")] impl Webhook { + /// Construct an event from a webhook payload and signature. + /// /// # Errors /// /// This function will return a WebhookError if: @@ -495,6 +497,27 @@ impl Webhook { Self { current_timestamp: Utc::now().timestamp() }.do_construct_event(payload, sig, secret) } + /// Construct an event from a webhook payload and signature, verifying its signature + /// using the provided timestamp. + /// + /// This is helpful for replaying requests in tests and should be avoided otherwise + /// in production use. + /// + /// # Errors + /// + /// This function will return a WebhookError if: + /// - the provided signature is invalid + /// - the provided secret is invalid + /// - the signature timestamp is older than 5 minutes from the provided timestamp + pub fn construct_event_with_timestamp( + payload: &str, + sig: &str, + secret: &str, + timestamp: i64, + ) -> Result { + Self { current_timestamp: timestamp }.do_construct_event(payload, sig, secret) + } + fn do_construct_event( self, payload: &str,