|
|
@ -1,172 +1,59 @@ |
|
|
|
use json_dotpath::DotPaths; |
|
|
|
use serde_json::Value; |
|
|
|
use parking_lot::RwLock; |
|
|
|
|
|
|
|
use rand::Rng; |
|
|
|
|
|
|
|
use rocket::fairing::{self, Fairing, Info}; |
|
|
|
|
|
|
|
use rocket::request::FromRequest; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
|
|
|
|
http::{Cookie, Status}, |
|
|
|
|
|
|
|
Outcome, Request, Response, Rocket, State, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
use serde::de::DeserializeOwned; |
|
|
|
use serde::Serialize; |
|
|
|
use serde::Serialize; |
|
|
|
use serde_json::{Map, Value}; |
|
|
|
use json_dotpath::DotPaths; |
|
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap; |
|
|
|
|
|
|
|
use std::time::{Instant, Duration}; |
|
|
|
|
|
|
|
use std::ops::Add; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SESSION_ID: &'static str = "SESSID"; |
|
|
|
pub type Session<'a> = rocket_session::Session<'a, serde_json::Map<String, Value>>; |
|
|
|
|
|
|
|
|
|
|
|
type SessionsMap = HashMap<String, SessionInstance>; |
|
|
|
pub trait SessionAccess { |
|
|
|
|
|
|
|
fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T>; |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T; |
|
|
|
struct SessionInstance { |
|
|
|
|
|
|
|
data: serde_json::Map<String, Value>, |
|
|
|
|
|
|
|
expires: Instant, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Debug)] |
|
|
|
fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>(&self, path: &str, def: F) -> T; |
|
|
|
pub struct SessionStore { |
|
|
|
|
|
|
|
inner: RwLock<SessionsMap>, |
|
|
|
|
|
|
|
lifespan: Duration, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(PartialEq, Hash, Clone, Debug)] |
|
|
|
fn get_or_default<T: DeserializeOwned + Default>(&self, path: &str) -> T; |
|
|
|
pub struct SessionID(String); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for &'a SessionID { |
|
|
|
|
|
|
|
type Error = (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
|
|
|
|
Outcome::Success(request.local_cache(|| { |
|
|
|
|
|
|
|
println!("get id"); |
|
|
|
|
|
|
|
if let Some(cookie) = request.cookies().get(SESSION_ID) { |
|
|
|
|
|
|
|
println!("from cookie"); |
|
|
|
|
|
|
|
SessionID(cookie.value().to_string()) // FIXME avoid cloning
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
println!("new id"); |
|
|
|
|
|
|
|
SessionID( |
|
|
|
|
|
|
|
rand::thread_rng() |
|
|
|
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
|
|
|
.take(16) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T>; |
|
|
|
pub struct Session<'a> { |
|
|
|
|
|
|
|
store: State<'a, SessionStore>, |
|
|
|
|
|
|
|
id: &'a SessionID, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Session<'a> { |
|
|
|
|
|
|
|
type Error = (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
|
|
|
|
Outcome::Success(Session { |
|
|
|
|
|
|
|
id: request.local_cache(|| { |
|
|
|
|
|
|
|
if let Some(cookie) = request.cookies().get(SESSION_ID) { |
|
|
|
|
|
|
|
SessionID(cookie.value().to_string()) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
SessionID( |
|
|
|
|
|
|
|
rand::thread_rng() |
|
|
|
|
|
|
|
.sample_iter(&rand::distributions::Alphanumeric) |
|
|
|
|
|
|
|
.take(16) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
store: request.guard().unwrap(), |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Session<'a> { |
|
|
|
|
|
|
|
pub fn fairing() -> impl Fairing { |
|
|
|
|
|
|
|
SessionFairing |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn tap<T>(&self, func : impl FnOnce(&mut serde_json::Map<String, Value>) -> T) -> T { |
|
|
|
fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O>; |
|
|
|
let mut wg = self.store.inner.write(); |
|
|
|
|
|
|
|
if let Some(instance) = wg.get_mut(&self.id.0) { |
|
|
|
|
|
|
|
instance.expires = Instant::now().add(self.store.lifespan); |
|
|
|
|
|
|
|
func(&mut instance.data) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let mut data = Map::new(); |
|
|
|
|
|
|
|
let rv = func(&mut data); |
|
|
|
|
|
|
|
wg.insert(self.id.0.clone(), SessionInstance { |
|
|
|
|
|
|
|
data: data, |
|
|
|
|
|
|
|
expires: Instant::now().add(self.store.lifespan), |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
rv |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn renew(&self) { |
|
|
|
fn set<T: Serialize>(&self, path: &str, value: T); |
|
|
|
self.tap(|_| ()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn reset(&self) { |
|
|
|
fn remove(&self, path: &str) -> bool; |
|
|
|
self.tap(|data| data.clear()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
impl<'a> SessionAccess for Session<'a> { |
|
|
|
self.tap(|data| data.dot_get(path)) |
|
|
|
fn get<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
|
|
|
|
self.tap(|data| data.dot_get(path).unwrap_or_default()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T { |
|
|
|
fn get_or<T: DeserializeOwned>(&self, path: &str, def: T) -> T { |
|
|
|
self.get(path).unwrap_or(def) |
|
|
|
self.get(path).unwrap_or(def) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>(&self, path: &str, def: F) -> T { |
|
|
|
fn get_or_else<T: DeserializeOwned, F: FnOnce() -> T>(&self, path: &str, def: F) -> T { |
|
|
|
self.get(path).unwrap_or_else(def) |
|
|
|
self.get(path).unwrap_or_else(def) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn get_or_default<T: DeserializeOwned + Default>(&self, path: &str) -> T { |
|
|
|
fn get_or_default<T: DeserializeOwned + Default>(&self, path: &str) -> T { |
|
|
|
self.get(path).unwrap_or_default() |
|
|
|
self.get(path).unwrap_or_default() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
fn take<T: DeserializeOwned>(&self, path: &str) -> Option<T> { |
|
|
|
self.tap(|data| data.dot_take(path)) |
|
|
|
self.tap(|data| data.dot_take(path).unwrap_or_default()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O> { |
|
|
|
fn replace<O: DeserializeOwned, N: Serialize>(&self, path: &str, new: N) -> Option<O> { |
|
|
|
self.tap(|data| data.dot_replace(path, new)) |
|
|
|
self.tap(|data| data.dot_replace(path, new).unwrap_or_default()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn set<T: Serialize>(&self, path: &str, value: T) { |
|
|
|
fn set<T: Serialize>(&self, path: &str, value: T) { |
|
|
|
self.tap(|data| data.dot_set(path, value)); |
|
|
|
self.tap(|data| data.dot_set(path, value)).unwrap(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn remove(&self, path: &str) -> bool { |
|
|
|
fn remove(&self, path: &str) -> bool { |
|
|
|
self.tap(|data| data.dot_remove(path)) |
|
|
|
self.tap(|data| { data.dot_remove(path).is_ok() }) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Fairing struct
|
|
|
|
|
|
|
|
struct SessionFairing; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Fairing for SessionFairing { |
|
|
|
|
|
|
|
fn info(&self) -> Info { |
|
|
|
|
|
|
|
Info { |
|
|
|
|
|
|
|
name: "Session Fairing", |
|
|
|
|
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> { |
|
|
|
|
|
|
|
Ok(rocket.manage(SessionStore::default())) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { |
|
|
|
|
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !session.0.is_empty() { |
|
|
|
|
|
|
|
response.adjoin_header(Cookie::build(SESSION_ID, session.0.clone()).finish()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|