|
|
|
@ -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::collections::HashMap; |
|
|
|
|
use std::fmt::{self, Display, Formatter}; |
|
|
|
@ -15,6 +5,16 @@ use std::marker::PhantomData; |
|
|
|
|
use std::ops::Add; |
|
|
|
|
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)
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
pub struct SessionStore<D> |
|
|
|
@ -114,29 +114,29 @@ where |
|
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
|
{ |
|
|
|
|
/// The shared state reference
|
|
|
|
|
store: State<'a, SessionStore<D>>, |
|
|
|
|
store: &'a State<SessionStore<D>>, |
|
|
|
|
/// Session ID
|
|
|
|
|
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 |
|
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
|
{ |
|
|
|
|
type Error = (); |
|
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
|
let store: State<SessionStore<D>> = request.guard().unwrap(); |
|
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, (Status, Self::Error), ()> { |
|
|
|
|
let store = request.guard::<&State<SessionStore<D>>>().await.unwrap(); |
|
|
|
|
Outcome::Success(Session { |
|
|
|
|
id: request.local_cache(|| { |
|
|
|
|
let store_ug = store.inner.upgradable_read(); |
|
|
|
|
|
|
|
|
|
// Resolve session ID
|
|
|
|
|
let id = if let Some(cookie) = request.cookies().get(&store.config.cookie_name) { |
|
|
|
|
Some(SessionID(cookie.value().to_string())) |
|
|
|
|
} else { |
|
|
|
|
None |
|
|
|
|
}; |
|
|
|
|
let id = request |
|
|
|
|
.cookies() |
|
|
|
|
.get(&store.config.cookie_name) |
|
|
|
|
.map(|cookie| SessionID(cookie.value().to_string())); |
|
|
|
|
|
|
|
|
|
let expires = Instant::now().add(store.config.lifespan); |
|
|
|
|
|
|
|
|
@ -295,6 +295,7 @@ where |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[rocket::async_trait] |
|
|
|
|
impl<D> Fairing for SessionFairing<D> |
|
|
|
|
where |
|
|
|
|
D: 'static + Sync + Send + Default, |
|
|
|
@ -302,11 +303,11 @@ where |
|
|
|
|
fn info(&self) -> Info { |
|
|
|
|
Info { |
|
|
|
|
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
|
|
|
|
|
Ok(rocket.manage(SessionStore::<D> { |
|
|
|
|
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
|
|
|
|
|
let session = request.local_cache(|| SessionID("".to_string())); |
|
|
|
|
|
|
|
|
|