|
|
@ -1,13 +1,3 @@ |
|
|
|
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; |
|
|
|
|
|
|
|
use rand::{Rng, rngs::OsRng}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
|
|
|
|
fairing::{self, Fairing, Info}, |
|
|
|
|
|
|
|
http::{Cookie, Status}, |
|
|
|
|
|
|
|
request::FromRequest, |
|
|
|
|
|
|
|
Outcome, Request, Response, Rocket, State, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::borrow::Cow; |
|
|
|
use std::borrow::Cow; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::fmt::{self, Display, Formatter}; |
|
|
|
use std::fmt::{self, Display, Formatter}; |
|
|
@ -15,6 +5,16 @@ use std::marker::PhantomData; |
|
|
|
use std::ops::Add; |
|
|
|
use std::ops::Add; |
|
|
|
use std::time::{Duration, Instant}; |
|
|
|
use std::time::{Duration, Instant}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; |
|
|
|
|
|
|
|
use rand::{rngs::OsRng, Rng}; |
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
|
|
|
|
fairing::{self, Fairing, Info}, |
|
|
|
|
|
|
|
http::{Cookie, Status}, |
|
|
|
|
|
|
|
outcome::Outcome, |
|
|
|
|
|
|
|
request::FromRequest, |
|
|
|
|
|
|
|
Build, Request, Response, Rocket, State, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/// Session store (shared state)
|
|
|
|
/// Session store (shared state)
|
|
|
|
#[derive(Debug)] |
|
|
|
#[derive(Debug)] |
|
|
|
pub struct SessionStore<D> |
|
|
|
pub struct SessionStore<D> |
|
|
@ -114,29 +114,29 @@ where |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
{ |
|
|
|
{ |
|
|
|
/// The shared state reference
|
|
|
|
/// The shared state reference
|
|
|
|
store: State<'a, SessionStore<D>>, |
|
|
|
store: &'a State<SessionStore<D>>, |
|
|
|
/// Session ID
|
|
|
|
/// Session ID
|
|
|
|
id: &'a SessionID, |
|
|
|
id: &'a SessionID, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<'a, 'r, D> FromRequest<'a, 'r> for Session<'a, D> |
|
|
|
#[rocket::async_trait] |
|
|
|
|
|
|
|
impl<'r, D> FromRequest<'r> for Session<'r, D> |
|
|
|
where |
|
|
|
where |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
{ |
|
|
|
{ |
|
|
|
type Error = (); |
|
|
|
type Error = (); |
|
|
|
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
let store: State<SessionStore<D>> = request.guard().unwrap(); |
|
|
|
let store = request.guard::<&State<SessionStore<D>>>().await.unwrap(); |
|
|
|
Outcome::Success(Session { |
|
|
|
Outcome::Success(Session { |
|
|
|
id: request.local_cache(|| { |
|
|
|
id: request.local_cache(|| { |
|
|
|
let store_ug = store.inner.upgradable_read(); |
|
|
|
let store_ug = store.inner.upgradable_read(); |
|
|
|
|
|
|
|
|
|
|
|
// Resolve session ID
|
|
|
|
// Resolve session ID
|
|
|
|
let id = if let Some(cookie) = request.cookies().get(&store.config.cookie_name) { |
|
|
|
let id = request |
|
|
|
Some(SessionID(cookie.value().to_string())) |
|
|
|
.cookies() |
|
|
|
} else { |
|
|
|
.get(&store.config.cookie_name) |
|
|
|
None |
|
|
|
.map(|cookie| SessionID(cookie.value().to_string())); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let expires = Instant::now().add(store.config.lifespan); |
|
|
|
let expires = Instant::now().add(store.config.lifespan); |
|
|
|
|
|
|
|
|
|
|
@ -295,6 +295,7 @@ where |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[rocket::async_trait] |
|
|
|
impl<D> Fairing for SessionFairing<D> |
|
|
|
impl<D> Fairing for SessionFairing<D> |
|
|
|
where |
|
|
|
where |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
D: 'static + Sync + Send + Default, |
|
|
@ -302,11 +303,11 @@ where |
|
|
|
fn info(&self) -> Info { |
|
|
|
fn info(&self) -> Info { |
|
|
|
Info { |
|
|
|
Info { |
|
|
|
name: "Session", |
|
|
|
name: "Session", |
|
|
|
kind: fairing::Kind::Attach | fairing::Kind::Response, |
|
|
|
kind: fairing::Kind::Ignite | fairing::Kind::Response, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn on_attach(&self, rocket: Rocket) -> Result<Rocket, Rocket> { |
|
|
|
async fn on_ignite(&self, rocket: Rocket<Build>) -> Result<Rocket<Build>, Rocket<Build>> { |
|
|
|
// install the store singleton
|
|
|
|
// install the store singleton
|
|
|
|
Ok(rocket.manage(SessionStore::<D> { |
|
|
|
Ok(rocket.manage(SessionStore::<D> { |
|
|
|
inner: Default::default(), |
|
|
|
inner: Default::default(), |
|
|
@ -314,7 +315,7 @@ where |
|
|
|
})) |
|
|
|
})) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn on_response<'r>(&self, request: &'r Request, response: &mut Response) { |
|
|
|
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response) { |
|
|
|
// send the session cookie, if session started
|
|
|
|
// send the session cookie, if session started
|
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
|
|
|
|
|
|
|
|