Skip to main content

WebhookEvent

Struct WebhookEvent 

Source
pub struct WebhookEvent { /* private fields */ }
Expand description

A webhook payload whose signature Stripe vouches for.

The struct keeps the three values verification actually needs, so a handler never has to re-parse the header once this is built.

Read accessors are generated by #[derive(Getter)]. The digest is only meaningful together with the payload and timestamp it was computed over, so it is exposed read-only: a caller that could replace the signature independently of the other two could make a forged event verify against its own digest.

Implementations§

Source§

impl WebhookEvent

Source

pub fn new(payload: String, timestamp: i64, signature: String) -> Self

Build a verification target from the three webhook inputs.

The signature is the bare hex digest, not the whole Stripe-Signature header; parse_signature_header extracts it.

§Arguments
  • String - the exact request body Stripe signed.
  • i64 - the timestamp Stripe signed, in seconds.
  • String - the hex digest Stripe sent.
§Returns
  • Self - the assembled verification target.
Source

pub fn compute_signature(&self, secret: &str) -> Result<String, WebhookError>

Sign the payload the way Stripe does.

Stripe signs the timestamp and the payload joined by a dot, then HMAC-SHA256 hex-encodes the digest, so this builds that exact signed text before hashing.

§Arguments
  • &str - the endpoint’s webhook signing secret.
§Returns
  • Result<String, WebhookError> - the hex digest Stripe would have sent, or an error when the secret is empty.
Source

pub fn is_timestamp_fresh(&self, now: i64, tolerance: i64) -> bool

Return whether the timestamp sits within tolerance of now.

Stripe recommends rejecting anything outside a five-minute window so a captured request cannot be replayed later.

§Arguments
  • i64 - the current time in seconds since the epoch.
  • i64 - the accepted drift, in seconds.
§Returns
  • bool - true when the timestamp is inside the window.
Source

pub fn verify_signature(&self, secret: &str) -> Result<(), WebhookError>

Verify the digest against the secret.

§Arguments
  • &str - the endpoint’s webhook signing secret.
§Returns
  • Result<(), WebhookError> - Ok(()) when the digest matches.
Source

pub fn verify( &self, secret: &str, now: i64, tolerance: i64, ) -> Result<(), WebhookError>

Verify freshness and then the digest.

§Arguments
  • &str - the endpoint’s webhook signing secret.
  • i64 - the current time in seconds since the epoch.
  • i64 - the accepted drift, in seconds.
§Returns
  • Result<(), WebhookError> - Ok(()) when the event is genuine.
Source

pub fn parse_signature_header( header: &str, payload: &str, ) -> Result<WebhookEvent, WebhookError>

Split a Stripe-Signature header into its timestamp and digest.

Stripe sends comma-separated entries such as t=1614556800,v1=deadbeef,v0=ignored. The v0 entry is the legacy scheme and v1 is the current one, so only v1 is accepted; a header carrying no v1 entry is rejected rather than downgraded.

§Arguments
  • &str - the raw Stripe-Signature header value.
  • &str - the exact request body Stripe signed.
§Returns
  • Result<WebhookEvent, WebhookError> - the verification target, or MissingSignature when no timestamp or v1 digest was present.
Source

pub fn secret_from_env() -> Result<String, WebhookError>

Read the webhook signing secret from the process environment.

§Returns
  • Result<String, WebhookError> - the configured secret, or EmptySecret when the variable is unset or blank.
Source

pub fn verify_webhook( header: &str, payload: &str, secret: &str, now: i64, tolerance: i64, ) -> Result<WebhookEvent, WebhookError>

Verify a raw webhook request end to end.

This is the entry point a hyperlane handler calls: it parses the header, then checks freshness and the digest.

§Arguments
  • &str - the raw Stripe-Signature header value.
  • &str - the exact request body Stripe signed.
  • &str - the endpoint’s webhook signing secret.
  • i64 - the current time in seconds since the epoch.
  • i64 - the accepted drift, in seconds.
§Returns
  • Result<WebhookEvent, WebhookError> - the verified event.
Source§

impl WebhookEvent

Source

pub fn get_payload(&self) -> &String

Source

pub fn get_timestamp(&self) -> i64

Source

pub fn get_signature(&self) -> &String

Trait Implementations§

Source§

impl Clone for WebhookEvent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WebhookEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for WebhookEvent

Source§

impl PartialEq for WebhookEvent

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for WebhookEvent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.