Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webhook_events): support construct_event with timestamp #374

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading