Skip to content

Commit

Permalink
Merge pull request #512 from arlyon/origin/feat/construct-event-times…
Browse files Browse the repository at this point in the history
…tamp

feat(webhook_events): support construct_event with timestamp
  • Loading branch information
arlyon committed Feb 27, 2024
2 parents df73ac2 + 88fe501 commit ee601fd
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 @@ -493,6 +493,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 @@ -503,6 +505,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 ee601fd

Please sign in to comment.