From 2637e9c4b18f5bc114b01cf5483493bab7cfe6f1 Mon Sep 17 00:00:00 2001 From: Jacob Haslehurst Date: Thu, 2 Nov 2017 18:39:45 +1100 Subject: [PATCH] update to latest version of cookie crate --- Cargo.toml | 2 +- src/backends/signedcookie.rs | 26 ++++++++++++++------------ src/lib.rs | 14 +++++++------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6f1246a..f521c0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ homepage = "https://github.com/iron/iron-sessionstorage" [dependencies] iron = "0.5" -cookie = { version = "0.5.0", features = ["secure"] } +cookie = { version = "0.10", features = ["secure"] } error-chain = "0.7" rand = "0.3" diff --git a/src/backends/signedcookie.rs b/src/backends/signedcookie.rs index bc4bf4e..a47b5b4 100644 --- a/src/backends/signedcookie.rs +++ b/src/backends/signedcookie.rs @@ -9,19 +9,20 @@ use SessionBackend; use get_default_cookie; pub struct SignedCookieSession { - unsigned_jar: cookie::CookieJar<'static>, + unsigned_jar: cookie::CookieJar, + signing_key: cookie::Key, cookie_modifier: Option cookie::Cookie + Send + Sync>>> } impl SignedCookieSession { - fn jar<'a>(&'a self) -> cookie::CookieJar<'a> { - self.unsigned_jar.signed() + fn signed_jar<'a>(&'a mut self) -> cookie::SignedJar<'a> { + self.unsigned_jar.signed(&self.signing_key) } } impl RawSession for SignedCookieSession { - fn get_raw(&self, key: &str) -> IronResult> { - Ok(self.jar().find(key).map(|c| c.value)) + fn get_raw(&mut self, key: &str) -> IronResult> { + Ok(self.signed_jar().get(key).map(|c| c.value().to_owned())) } fn set_raw(&mut self, key: &str, value: String) -> IronResult<()> { @@ -29,19 +30,19 @@ impl RawSession for SignedCookieSession { if let Some(ref modifier) = self.cookie_modifier { c = modifier(c); } - self.jar().add(c); + self.signed_jar().add(c); Ok(()) } fn clear(&mut self) -> IronResult<()> { - self.jar().clear(); + self.unsigned_jar.clear(); Ok(()) } fn write(&self, res: &mut Response) -> IronResult<()> { debug_assert!(!res.headers.has::()); res.headers.set(iron::headers::SetCookie( - self.jar() + self.unsigned_jar .delta() .into_iter() .map(|c| format!("{}", c)) @@ -81,18 +82,19 @@ impl SignedCookieBackend { impl SessionBackend for SignedCookieBackend { type S = SignedCookieSession; - fn from_request(&self, req: &mut Request) -> Self::S { - let mut jar = cookie::CookieJar::new(&self.signing_key); + fn from_request<'a, 'r>(&'a self, req: &'r mut Request) -> Self::S { + let mut jar = cookie::CookieJar::new(); if let Some(cookies) = req.headers.get::() { for cookie in cookies.iter() { - if let Ok(cookie) = cookie::Cookie::parse(&cookie) { - jar.add_original(cookie); + if let Ok(cookie) = cookie::Cookie::<'r>::parse(cookie.as_str()) { + jar.add_original(cookie.into_owned()); } } }; SignedCookieSession { unsigned_jar: jar, + signing_key: cookie::Key::from_master(&self.signing_key), cookie_modifier: self.cookie_modifier.clone(), } } diff --git a/src/lib.rs b/src/lib.rs index 16ff3de..b8b500b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub mod cookie { /// handling the `write` method is called where the session backend has the chance to e.g. set /// cookies or otherwise modify the response. pub trait RawSession { - fn get_raw(&self, key: &str) -> IronResult>; + fn get_raw(&mut self, key: &str) -> IronResult>; fn set_raw(&mut self, key: &str, value: String) -> IronResult<()>; fn clear(&mut self) -> IronResult<()>; fn write(&self, response: &mut Response) -> IronResult<()>; @@ -81,7 +81,7 @@ pub trait Value: Sized + 'static { impl Session { /// Get a `Value` from the session. - pub fn get(&self) -> IronResult> { + pub fn get(&mut self) -> IronResult> { Ok(try!(self.inner.get_raw(T::get_key())).and_then(T::from_raw)) } @@ -132,11 +132,11 @@ impl<'a, 'b> SessionRequestExt for Request<'a, 'b> { } } -fn get_default_cookie(key: String, value: String) -> cookie::Cookie { - let mut rv = cookie::Cookie::new(key, value); - rv.httponly = true; - rv.path = Some("/".to_owned()); - rv +fn get_default_cookie(key: String, value: String) -> cookie::Cookie<'static> { + cookie::Cookie::build(key, value) + .http_only(true) + .path("/") + .finish() } /// A module with some important traits to star-import.