75 lines
1.8 KiB
Rust
75 lines
1.8 KiB
Rust
|
|
use axum_extra::extract::cookie::{Cookie, CookieJar};
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
use std::sync::{Arc, RwLock};
|
||
|
|
|
||
|
|
pub const COOKIE_NAME: &str = "lldap_session";
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub struct HttpSessionManager {
|
||
|
|
inner: Arc<RwLock<Vec<HttpSession>>>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl HttpSessionManager {
|
||
|
|
pub fn new() -> Self {
|
||
|
|
Self {
|
||
|
|
inner: Arc::new(RwLock::new(vec![])),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn add_session(&self, username: &str, is_admin: bool, cookies: CookieJar) -> CookieJar {
|
||
|
|
let uuid = Uuid::new_v4();
|
||
|
|
|
||
|
|
let mut cookie = Cookie::new(COOKIE_NAME, uuid.to_string());
|
||
|
|
cookie.set_path("/");
|
||
|
|
|
||
|
|
let session = HttpSession {
|
||
|
|
username: username.to_string(),
|
||
|
|
is_admin,
|
||
|
|
uuid,
|
||
|
|
};
|
||
|
|
|
||
|
|
{
|
||
|
|
self.inner.write().unwrap().push(session);
|
||
|
|
}
|
||
|
|
|
||
|
|
cookies.add(cookie)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn get_session(&self, cookies: &CookieJar) -> Option<HttpSession> {
|
||
|
|
let cookie = cookies.get(COOKIE_NAME)?;
|
||
|
|
let previous_uuid = Uuid::parse_str(cookie.value()).ok()?;
|
||
|
|
|
||
|
|
self.inner
|
||
|
|
.read()
|
||
|
|
.unwrap()
|
||
|
|
.iter()
|
||
|
|
.find(|session| session.uuid == previous_uuid)
|
||
|
|
.cloned()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn remove_session(&self, session: &HttpSession, cookies: CookieJar) -> CookieJar {
|
||
|
|
let mut cookie = Cookie::new(COOKIE_NAME, String::new());
|
||
|
|
cookie.set_path("/");
|
||
|
|
|
||
|
|
let idx = {
|
||
|
|
let Some(idx) = self.inner.read().unwrap().iter().position(|s| s == session) else {
|
||
|
|
return cookies;
|
||
|
|
};
|
||
|
|
idx
|
||
|
|
};
|
||
|
|
|
||
|
|
{
|
||
|
|
self.inner.write().unwrap().remove(idx);
|
||
|
|
}
|
||
|
|
|
||
|
|
cookies.remove(cookie)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Debug, PartialEq)]
|
||
|
|
pub struct HttpSession {
|
||
|
|
pub username: String,
|
||
|
|
pub is_admin: bool,
|
||
|
|
pub uuid: Uuid,
|
||
|
|
}
|