Skip to content

Commit

Permalink
Revert "Remove dependency on chrono"
Browse files Browse the repository at this point in the history
This reverts commit e462ae4.
  • Loading branch information
arlyon committed Feb 19, 2021
1 parent b851de1 commit e816e3b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sigma = []
webhook-endpoints = []

# deserialize events from webhooks
webhook-events = ["events", "hmac", "sha2"]
webhook-events = ["events", "hmac", "sha2", "chrono"]
events = []

# runtimes
Expand All @@ -77,6 +77,7 @@ runtime-async-std-surf = ["async-std", "surf", "async"]

[dependencies]
async-std = { version = "1.9", optional = true }
chrono = { version = "0.4", features = ["serde"], optional = true }
http = "0.2"
hyper = { version = "0.14", default-features = false, features = ["http1", "http2", "client", "tcp"], optional = true }
hyper-tls = { version = "0.5", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub enum WebhookError {
/// The event signature could not be verified.
BadSignature,
/// The event signature's timestamp was too old.
BadTimestamp(u64),
BadTimestamp(i64),
/// An error deserializing an event received from stripe.
BadParse(serde_json::Error),
}
Expand Down
25 changes: 9 additions & 16 deletions src/resources/webhook_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::error::WebhookError;
use crate::ids::EventId;
use crate::resources::*;

use chrono::Utc;
#[cfg(feature = "webhook-events")]
use hmac::{Hmac, Mac, NewMac};
use serde_derive::{Deserialize, Serialize};
#[cfg(feature = "webhook-events")]
use sha2::Sha256;
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq, Hash)]
pub enum EventType {
Expand Down Expand Up @@ -239,7 +239,7 @@ pub enum EventObject {

#[cfg(feature = "webhook-events")]
pub struct Webhook {
current_timestamp: u64,
current_timestamp: i64,
}

#[cfg(feature = "webhook-events")]
Expand All @@ -249,8 +249,7 @@ impl Webhook {
sig: &str,
secret: &str,
) -> Result<WebhookEvent, WebhookError> {
Self { current_timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() }
.do_construct_event(payload, sig, secret)
Self { current_timestamp: Utc::now().timestamp() }.do_construct_event(payload, sig, secret)
}

fn do_construct_event(
Expand All @@ -275,17 +274,11 @@ impl Webhook {
}

// Get current timestamp to compare to signature timestamp
let diff = if self.current_timestamp > signature.t {
self.current_timestamp - signature.t
} else {
signature.t - self.current_timestamp
};

if diff > 300 {
Err(WebhookError::BadTimestamp(signature.t))
} else {
serde_json::from_str(&payload).map_err(WebhookError::BadParse)
if (self.current_timestamp - signature.t).abs() > 300 {
return Err(WebhookError::BadTimestamp(signature.t));
}

serde_json::from_str(&payload).map_err(WebhookError::BadParse)
}
}

Expand All @@ -301,7 +294,7 @@ fn to_hex(bytes: &[u8]) -> String {
#[cfg(feature = "webhook-events")]
#[derive(Debug)]
struct Signature<'r> {
t: u64,
t: i64,
v1: &'r str,
v0: Option<&'r str>,
}
Expand All @@ -326,7 +319,7 @@ impl<'r> Signature<'r> {
let t = headers.get("t").ok_or(WebhookError::BadSignature)?;
let v1 = headers.get("v1").ok_or(WebhookError::BadSignature)?;
let v0 = headers.get("v0").map(|r| *r);
Ok(Signature { t: t.parse::<u64>().map_err(WebhookError::BadHeader)?, v1, v0 })
Ok(Signature { t: t.parse::<i64>().map_err(WebhookError::BadHeader)?, v1, v0 })
}
}

Expand Down

0 comments on commit e816e3b

Please sign in to comment.