From 1d1e718264180aa4af49b5c1b75c8c78897687e7 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 16 Jun 2022 21:24:48 +0900 Subject: [PATCH 1/3] Replace `chrono` with `time` crate Signed-off-by: Yuki Okushi --- Cargo.toml | 7 +++---- src/cookie_store.rs | 2 +- src/lib.rs | 2 +- src/session.rs | 22 ++++++++++++++-------- 4 files changed, 19 insertions(+), 14 deletions(-) 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. From fc710b2a2ca4d4a4690948327d0275965c995e30 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Tue, 13 Dec 2022 11:41:08 -0800 Subject: [PATCH 2/3] update deps on the branch --- Cargo.toml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ec0117f..e8fa145 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,26 +15,26 @@ authors = [ ] [dependencies] -async-trait = "0.1.50" -rand = "0.8.3" -base64 = "0.13.0" -sha2 = "0.9.5" -hmac = "0.11.0" -serde_json = "1.0.64" +async-trait = "0.1.59" +rand = "0.8.5" +base64 = "0.20.0" +sha2 = "0.10.6" +hmac = "0.12.1" +serde_json = "1.0.89" bincode = "1.3.3" -anyhow = "1.0.40" -blake3 = "1.2.0" -async-lock = "2.4.0" -log = "0.4.14" +anyhow = "1.0.66" +blake3 = "1.3.3" +async-lock = "2.6.0" +log = "0.4.17" [dependencies.serde] -version = "1.0.126" +version = "1.0.150" features = ["rc", "derive"] [dependencies.time] -version = "0.3.10" +version = "0.3.17" features = ["serde"] [dev-dependencies.async-std] -version = "1.9.0" +version = "1.12.0" features = ["attributes"] From a4de37ffbc8b6de0c9b9f4e20bb3f550a1736127 Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Tue, 13 Dec 2022 11:31:44 -0800 Subject: [PATCH 3/3] incorporate clippy changes from main # Conflicts: # src/cookie_store.rs --- src/cookie_store.rs | 6 +++--- src/memory_store.rs | 6 ++---- src/session.rs | 19 ++++++++++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/cookie_store.rs b/src/cookie_store.rs index b8f7100..ff6eaa5 100644 --- a/src/cookie_store.rs +++ b/src/cookie_store.rs @@ -19,7 +19,7 @@ use crate::{async_trait, Result, Session, SessionStore}; /// CookieStore, and noop. Destroying a session must be done at the /// cookie setting level, which is outside of the scope of this crate. -#[derive(Debug, Clone, Copy)] +#[derive(Default, Debug, Clone, Copy)] pub struct CookieStore; impl CookieStore { @@ -32,8 +32,8 @@ impl CookieStore { #[async_trait] 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 serialized = base64::decode(cookie_value)?; + let session: Session = bincode::deserialize(&serialized)?; Ok(session.validate()) } diff --git a/src/memory_store.rs b/src/memory_store.rs index 51764ba..ce5fd10 100644 --- a/src/memory_store.rs +++ b/src/memory_store.rs @@ -26,7 +26,7 @@ use std::{collections::HashMap, sync::Arc}; /// - [async-redis-session](https://crates.io/crates/async-redis-session) /// - [async-mongodb-session](https://crates.io/crates/async-mongodb-session) /// -#[derive(Debug, Clone)] +#[derive(Default, Debug, Clone)] pub struct MemoryStore { inner: Arc>>, } @@ -72,9 +72,7 @@ impl SessionStore for MemoryStore { impl MemoryStore { /// Create a new instance of MemoryStore pub fn new() -> Self { - Self { - inner: Arc::new(RwLock::new(HashMap::new())), - } + Self::default() } /// Performs session cleanup. This should be run on an diff --git a/src/session.rs b/src/session.rs index 85fdba8..3814f30 100644 --- a/src/session.rs +++ b/src/session.rs @@ -140,7 +140,7 @@ impl Session { pub fn id_from_cookie_value(string: &str) -> Result { let decoded = base64::decode(string)?; let hash = blake3::hash(&decoded); - Ok(base64::encode(&hash.as_bytes())) + Ok(base64::encode(hash.as_bytes())) } /// mark this session for destruction. the actual session record @@ -297,8 +297,21 @@ impl Session { /// assert_eq!(session.len(), 1); /// ``` pub fn len(&self) -> usize { - let data = self.data.read().unwrap(); - data.len() + self.data.read().unwrap().len() + } + + /// returns a boolean indicating whether there are zero elements in the session hashmap + /// + /// # Example + /// + /// ```rust + /// # use async_session::Session; + /// let mut session = Session::new(); + /// assert!(session.is_empty()); + /// session.insert("key", 0); + /// assert!(!session.is_empty()); + pub fn is_empty(&self) -> bool { + return self.data.read().unwrap().is_empty(); } /// Generates a new id and cookie for this session