From bc9f0130418af0189f746243240262682955626b Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon Date: Fri, 27 Sep 2024 18:18:35 -0400 Subject: [PATCH] list todos in readme --- Cargo.toml | 2 +- README.md | 9 +++++++++ src/service.rs | 34 +++++++++++++++++++++++++++++++--- src/session.rs | 13 ++++++++----- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5386681..ffdd01f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ tower-sessions-core = { workspace = true } # tower-sessions-memory-store = { workspace = true, optional = true } tracing = { version = "0.1.40", features = ["log"] } time = { version = "0.3.29", features = ["serde"] } -cookie = { version = "0.18.1", features = ["percent-encode"] } +cookie = "0.18.1" [dev-dependencies] async-trait = "0.1.74" diff --git a/README.md b/README.md index 8dc632c..04b525d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,15 @@ +## TODOs +- [ ] Make the `Store` trait take `R` instead of `&R`. +- [ ] Add `'static` bounds to `Store` trait. +- [ ] Spawn a task when dropping the `SessionData` to save the session data. +- [ ] Complete middleware implementation. +- [ ] Add examples everywhere. +- [ ] Rewrite the in memory store. +- [ ] Rewrite all the tests. + ## 🎨 Overview This crate provides sessions, key-value pairs associated with a site diff --git a/src/service.rs b/src/service.rs index c3fc193..3d081fe 100644 --- a/src/service.rs +++ b/src/service.rs @@ -159,7 +159,7 @@ where .into_iter() .filter_map(|value| value.to_str().ok()) .flat_map(|value| value.split(';')) - .filter_map(|cookie| Cookie::parse_encoded(cookie).ok()) + .filter_map(|cookie| Cookie::parse(cookie).ok()) .find(|cookie| cookie.name() == self.config.name); let id = session_cookie.and_then(|cookie| { @@ -229,8 +229,36 @@ where } }); match update { - Some(SessionUpdate::Delete) => todo!(), - Some(SessionUpdate::Set(id)) => todo!(), + Some(SessionUpdate::Delete) => { + if let Some(old_id) = this.old_id { + let cookie = this.config.build_cookie( + *old_id, + Some(Expiry::AtDateTime( + // The Year 2000. + time::OffsetDateTime::from_unix_timestamp(946684800) + .expect("year 2000 should be in range"), + )), + ); + resp.headers_mut().insert( + http::header::SET_COOKIE, + cookie + .to_string() + .try_into() + .expect("cookie should be valid"), + ); + }; + } + Some(SessionUpdate::Set(id)) => { + // TODO: This should also accept a user-provided expiry. + let cookie = this.config.build_cookie(id, None); + resp.headers_mut().insert( + http::header::SET_COOKIE, + cookie + .to_string() + .try_into() + .expect("cookie should be valid"), + ); + }, None => {} }; diff --git a/src/session.rs b/src/session.rs index 2945d60..c32fdd1 100644 --- a/src/session.rs +++ b/src/session.rs @@ -52,9 +52,9 @@ where { fn clone(&self) -> Self { Self { - id: self.id.clone(), + id: self.id, store: self.store.clone(), - data: self.data.clone(), + data: self.data, updater: self.updater.clone(), } } @@ -112,7 +112,9 @@ impl> LazySession { } #[derive(Debug, Clone, Copy)] -struct NoMiddleware; +/// A rejection that is returned from the [`Session`] extractor when the [`SessionManagerLayer`] +/// middleware is not set. +pub struct NoMiddleware; impl Display for NoMiddleware { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -161,6 +163,7 @@ where /// store used returned a "hard" error. For example, it could be a connection error, a protocol error, /// a timeout, etc. A counterexample would be the session not being found in the store, which is /// not considered an error by the `SessionStore` trait. +#[derive(Debug)] pub struct Session> { store: Store, id: Id, @@ -294,9 +297,9 @@ where Store: SessionStore, { fn drop(&mut self) { - let _ = self + let _ = tokio::task::spawn(self .session .store - .save(&self.session.id, &self.session.data); + .save(&self.session.id, &self.session.data)); } }