diff --git a/Cargo.toml b/Cargo.toml index 1108416..ec0117f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,10 +31,9 @@ log = "0.4.14" version = "1.0.126" features = ["rc", "derive"] -[dependencies.chrono] -version = "0.4.19" -default-features = false -features = ["clock", "serde", "std"] +[dependencies.time] +version = "0.3.10" +features = ["serde"] [dev-dependencies.async-std] version = "1.9.0" diff --git a/src/cookie_store.rs b/src/cookie_store.rs index 860085a..b8f7100 100644 --- a/src/cookie_store.rs +++ b/src/cookie_store.rs @@ -33,7 +33,7 @@ impl CookieStore { impl SessionStore for CookieStore { async fn load_session(&self, cookie_value: String) -> Result> { let serialized = base64::decode(&cookie_value)?; - let session: Session = bincode::deserialize(&serialized)?; + let session: Session = bincode::deserialize(&serialized[..])?; Ok(session.validate()) } diff --git a/src/lib.rs b/src/lib.rs index 5470b5a..4c1d222 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,9 +63,9 @@ pub use session_store::SessionStore; pub use async_trait::async_trait; pub use base64; pub use blake3; -pub use chrono; pub use hmac; pub use log; pub use serde; pub use serde_json; pub use sha2; +pub use time; diff --git a/src/session.rs b/src/session.rs index d0c31b2..85fdba8 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,13 +1,14 @@ -use chrono::{DateTime, Duration, Utc}; use rand::RngCore; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, + convert::TryFrom, sync::{ atomic::{AtomicBool, Ordering}, Arc, RwLock, }, }; +use time::OffsetDateTime as DateTime; /// # The main session type. /// @@ -56,7 +57,7 @@ use std::{ #[derive(Debug, Serialize, Deserialize)] pub struct Session { id: String, - expiry: Option>, + expiry: Option, data: Arc>>, #[serde(skip)] @@ -355,7 +356,7 @@ impl Session { /// assert!(session.expiry().is_some()); /// # Ok(()) }) } /// ``` - pub fn expiry(&self) -> Option<&DateTime> { + pub fn expiry(&self) -> Option<&DateTime> { self.expiry.as_ref() } @@ -368,11 +369,11 @@ impl Session { /// # fn main() -> async_session::Result { async_std::task::block_on(async { /// let mut session = Session::new(); /// assert_eq!(None, session.expiry()); - /// session.set_expiry(chrono::Utc::now()); + /// session.set_expiry(time::OffsetDateTime::now_utc()); /// assert!(session.expiry().is_some()); /// # Ok(()) }) } /// ``` - pub fn set_expiry(&mut self, expiry: DateTime) { + pub fn set_expiry(&mut self, expiry: DateTime) { self.expiry = Some(expiry); } @@ -390,7 +391,7 @@ impl Session { /// # Ok(()) }) } /// ``` pub fn expire_in(&mut self, ttl: std::time::Duration) { - self.expiry = Some(Utc::now() + Duration::from_std(ttl).unwrap()); + self.expiry = Some(DateTime::now_utc() + ttl); } /// predicate function to determine if this session is @@ -415,7 +416,7 @@ impl Session { /// ``` pub fn is_expired(&self) -> bool { match self.expiry { - Some(expiry) => expiry < Utc::now(), + Some(expiry) => expiry < DateTime::now_utc(), None => false, } } @@ -509,7 +510,12 @@ impl Session { /// ``` /// Duration from now to the expiry time of this session pub fn expires_in(&self) -> Option { - self.expiry?.signed_duration_since(Utc::now()).to_std().ok() + let dur = self.expiry? - DateTime::now_utc(); + if dur.is_negative() { + None + } else { + std::time::Duration::try_from(dur).ok() + } } /// takes the cookie value and consume this session.