Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest version of cookie crate #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
26 changes: 14 additions & 12 deletions src/backends/signedcookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,40 @@ 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<Arc<Box<Fn(cookie::Cookie) -> 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<Option<String>> {
Ok(self.jar().find(key).map(|c| c.value))
fn get_raw(&mut self, key: &str) -> IronResult<Option<String>> {
Ok(self.signed_jar().get(key).map(|c| c.value().to_owned()))
}

fn set_raw(&mut self, key: &str, value: String) -> IronResult<()> {
let mut c = get_default_cookie(key.to_owned(), value);
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::<iron::headers::SetCookie>());
res.headers.set(iron::headers::SetCookie(
self.jar()
self.unsigned_jar
.delta()
.into_iter()
.map(|c| format!("{}", c))
Expand Down Expand Up @@ -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::<iron::headers::Cookie>() {
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(),
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>>;
fn get_raw(&mut self, key: &str) -> IronResult<Option<String>>;
fn set_raw(&mut self, key: &str, value: String) -> IronResult<()>;
fn clear(&mut self) -> IronResult<()>;
fn write(&self, response: &mut Response) -> IronResult<()>;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub trait Value: Sized + 'static {

impl Session {
/// Get a `Value` from the session.
pub fn get<T: Value + Sized + 'static>(&self) -> IronResult<Option<T>> {
pub fn get<T: Value + Sized + 'static>(&mut self) -> IronResult<Option<T>> {
Ok(try!(self.inner.get_raw(T::get_key())).and_then(T::from_raw))
}

Expand Down Expand Up @@ -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.
Expand Down