|
|
@ -1,11 +1,12 @@ |
|
|
|
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; |
|
|
|
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard}; |
|
|
|
use rand::{Rng, rngs::OsRng}; |
|
|
|
use rand::{rngs::OsRng, Rng}; |
|
|
|
|
|
|
|
|
|
|
|
use rocket::{ |
|
|
|
use rocket::{ |
|
|
|
fairing::{self, Fairing, Info}, |
|
|
|
fairing::{self, Fairing, Info}, |
|
|
|
http::{Cookie, Status}, |
|
|
|
http::{Cookie, Status}, |
|
|
|
|
|
|
|
outcome::Outcome, |
|
|
|
request::FromRequest, |
|
|
|
request::FromRequest, |
|
|
|
Outcome, Request, Response, Rocket, State, |
|
|
|
Build, Request, Response, Rocket, State, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
use std::borrow::Cow; |
|
|
|
use std::borrow::Cow; |
|
|
@ -114,19 +115,20 @@ 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(); |
|
|
@ -198,7 +200,7 @@ where |
|
|
|
new_id |
|
|
|
new_id |
|
|
|
} |
|
|
|
} |
|
|
|
}), |
|
|
|
}), |
|
|
|
store, |
|
|
|
store: store, |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -295,6 +297,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 +305,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 +317,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())); |
|
|
|
|
|
|
|
|
|
|
|