|
|
@ -1,16 +1,19 @@ |
|
|
|
use rocket::request::FromRequest; |
|
|
|
use json_dotpath::DotPaths; |
|
|
|
use rocket::{Outcome, Request, State, http::{Status, Cookies, Cookie}, Response, Data, Rocket}; |
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
|
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; |
|
|
|
use parking_lot::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; |
|
|
|
use serde_json::{Value, Map}; |
|
|
|
|
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
|
|
|
|
use rand::Rng; |
|
|
|
use rand::Rng; |
|
|
|
use std::borrow::Cow; |
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
use rocket::request::FromRequest; |
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
|
|
|
|
use json_dotpath::DotPaths; |
|
|
|
|
|
|
|
use rocket::response::ResponseBuilder; |
|
|
|
use rocket::response::ResponseBuilder; |
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
|
|
|
|
http::{Cookie, Cookies, Status}, |
|
|
|
|
|
|
|
Data, Outcome, Request, Response, Rocket, State, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize}; |
|
|
|
|
|
|
|
use serde_json::{Map, Value}; |
|
|
|
|
|
|
|
use std::borrow::Cow; |
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
|
use std::ops::{Deref, DerefMut}; |
|
|
|
|
|
|
|
|
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
|
|
|
|
|
|
|
@ -41,10 +44,12 @@ impl<'a, 'r> FromRequest<'a, 'r> for &'a SessionID { |
|
|
|
SessionID(cookie.value().to_string()) // FIXME avoid cloning
|
|
|
|
SessionID(cookie.value().to_string()) // FIXME avoid cloning
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
println!("new id"); |
|
|
|
println!("new id"); |
|
|
|
SessionID(rand::thread_rng() |
|
|
|
SessionID( |
|
|
|
|
|
|
|
rand::thread_rng() |
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
.take(16) |
|
|
|
.take(16) |
|
|
|
.collect()) |
|
|
|
.collect(), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
@ -65,13 +70,15 @@ impl<'a, 'r> FromRequest<'a, 'r> for Session<'a> { |
|
|
|
if let Some(cookie) = request.cookies().get(SESSION_ID) { |
|
|
|
if let Some(cookie) = request.cookies().get(SESSION_ID) { |
|
|
|
SessionID(cookie.value().to_string()) |
|
|
|
SessionID(cookie.value().to_string()) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
SessionID(rand::thread_rng() |
|
|
|
SessionID( |
|
|
|
|
|
|
|
rand::thread_rng() |
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
.take(16) |
|
|
|
.take(16) |
|
|
|
.collect()) |
|
|
|
.collect(), |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
store: request.guard().unwrap() |
|
|
|
store: request.guard().unwrap(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -127,9 +134,7 @@ impl<'a> Session<'a> { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
let mut map = Map::new(); |
|
|
|
let mut map = Map::new(); |
|
|
|
map.dot_set(path, value); |
|
|
|
map.dot_set(path, value); |
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { |
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { data: map }); |
|
|
|
data : map, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -148,7 +153,7 @@ impl Fairing for SessionFairing { |
|
|
|
fn info(&self) -> Info { |
|
|
|
fn info(&self) -> Info { |
|
|
|
Info { |
|
|
|
Info { |
|
|
|
name: "Session Fairing", |
|
|
|
name: "Session Fairing", |
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response |
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -157,9 +162,7 @@ impl Fairing for SessionFairing { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { |
|
|
|
fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { |
|
|
|
let session = request.local_cache(|| { |
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
SessionID("".to_string()) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !session.0.is_empty() { |
|
|
|
if !session.0.is_empty() { |
|
|
|
response.adjoin_header(Cookie::build(SESSION_ID, session.0.clone()).finish()); |
|
|
|
response.adjoin_header(Cookie::build(SESSION_ID, session.0.clone()).finish()); |
|
|
|