Skip to content

Commit

Permalink
feat(webhook_events): support construct_event with timestamp
Browse files Browse the repository at this point in the history
This is a testing utility constructor which allows for replaying
requests with an older timestamp.

Signed-off-by: Sean Pianka <pianka@eml.cc>
  • Loading branch information
seanpianka committed May 7, 2023
1 parent 4546fd9 commit aade528
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/resources/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<Event, WebhookError> {
Self { current_timestamp: timestamp }.do_construct_event(payload, sig, secret)
}

fn do_construct_event(
self,
payload: &str,
Expand Down

0 comments on commit aade528

Please sign in to comment.