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 cookie #17

Open
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "iron-sessionstorage"
version = "0.6.6"
version = "0.7.0"

authors = ["Markus Unterwaditzer <[email protected]>"]
license = "MIT"
Expand All @@ -13,7 +13,7 @@ homepage = "https://github.com/iron/iron-sessionstorage"

[dependencies]
iron = "0.6"
cookie = { version = "0.5.0", features = ["secure"] }
cookie = { version = "0.12.0", features = ["secure"] }
error-chain = "0.11"
rand = "0.4"

Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn handler(req: &mut Request) -> IronResult<Response> {
}

fn main() {
let my_secret = b"verysecret".to_vec();
let my_secret = b"verysecretkeyatleast32byteslong.".to_vec();
let mut ch = Chain::new(handler);
ch.link_around(SessionStorage::new(SignedCookieBackend::new(my_secret)));
let _res = Iron::new(ch).http("localhost:8080");
Expand Down
2 changes: 1 addition & 1 deletion examples/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn main() {
logout: post "/logout" => logout,
);

let my_secret = b"verysecret".to_vec();
let my_secret = b"verysecretkeyatleast32byteslong.".to_vec();
let mut ch = Chain::new(router);
ch.link_around(SessionStorage::new(SignedCookieBackend::new(my_secret)));
let _res = Iron::new(ch).http("localhost:8080");
Expand Down
24 changes: 11 additions & 13 deletions src/backends/signedcookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,36 @@ use SessionBackend;
use get_default_cookie;

pub struct SignedCookieSession {
unsigned_jar: cookie::CookieJar<'static>,
unsigned_jar: std::cell::RefCell<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()
}
}

impl RawSession for SignedCookieSession {
fn get_raw(&self, key: &str) -> IronResult<Option<String>> {
Ok(self.jar().find(key).map(|c| c.value))
Ok(self.unsigned_jar.borrow_mut().signed(&self.signing_key).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.unsigned_jar.borrow_mut().signed(&self.signing_key).add(c);
Ok(())
}

fn clear(&mut self) -> IronResult<()> {
self.jar().clear();
self.unsigned_jar.borrow_mut().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
.borrow()
.delta()
.into_iter()
.map(|c| format!("{}", c))
Expand Down Expand Up @@ -82,17 +79,18 @@ 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);
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) {
if let Ok(cookie) = cookie::Cookie::parse(cookie.clone()) {
jar.add_original(cookie);
}
}
};

SignedCookieSession {
unsigned_jar: jar,
unsigned_jar: std::cell::RefCell::new(jar),
signing_key: cookie::Key::from_master(&self.signing_key),
cookie_modifier: self.cookie_modifier.clone(),
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
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