Delete expired sessions. Update session duration on access
This commit is contained in:
parent
4410eb4240
commit
5536104ca1
@ -1,4 +1,3 @@
|
|||||||
use rocket::response::Redirect;
|
|
||||||
use rocket::State;
|
use rocket::State;
|
||||||
use rocket::{
|
use rocket::{
|
||||||
http::Status,
|
http::Status,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::{collections::HashMap, sync::{Arc, Mutex}};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
use std::{collections::HashMap, sync::Mutex};
|
||||||
|
|
||||||
pub struct Sessions {
|
pub struct Sessions {
|
||||||
sessions: Mutex<HashMap<String, SessionData>>,
|
sessions: Mutex<HashMap<String, SessionData>>,
|
||||||
@ -19,22 +19,64 @@ impl Sessions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, session_id: &str) -> Option<SessionData> {
|
pub fn get(&self, session_id: &str) -> Option<SessionData> {
|
||||||
let map = self.sessions.lock().unwrap();
|
let mut map = self.sessions.lock().unwrap();
|
||||||
|
|
||||||
match map.get(session_id) {
|
// Remove expired sessions
|
||||||
Some(s) => Some(s.clone()),
|
|
||||||
None => None,
|
// current unix time in seconds
|
||||||
|
let current_time = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Time went backwards")
|
||||||
|
.as_secs();
|
||||||
|
|
||||||
|
let keys = map.keys().cloned().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
for key in keys {
|
||||||
|
if map.get(&key).unwrap().expires_at < current_time {
|
||||||
|
map.remove(&key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the session & extend its duration
|
||||||
|
let new_session = match map.get(session_id) {
|
||||||
|
Some(s) => s.extend_duration(current_time),
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let returned_session = new_session.clone();
|
||||||
|
|
||||||
|
// Update the session in the map
|
||||||
|
map.insert(session_id.to_string(), new_session);
|
||||||
|
|
||||||
|
Some(returned_session)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
pub struct SessionData {
|
pub struct SessionData {
|
||||||
user_id: i32,
|
user_id: i32,
|
||||||
|
expires_at: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SessionData {
|
impl SessionData {
|
||||||
|
// Create a new session with a duration set to 1 hour
|
||||||
pub fn new(user_id: i32) -> Self {
|
pub fn new(user_id: i32) -> Self {
|
||||||
SessionData { user_id }
|
let expires_at = SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("Time went backwards")
|
||||||
|
.as_secs()
|
||||||
|
+ 3600;
|
||||||
|
|
||||||
|
SessionData {
|
||||||
|
user_id,
|
||||||
|
expires_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extend_duration(&self, current_time: u64) -> Self {
|
||||||
|
SessionData {
|
||||||
|
user_id: self.user_id,
|
||||||
|
expires_at: current_time + 3600,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,7 @@ pub struct LoginData {
|
|||||||
|
|
||||||
#[get("/login")]
|
#[get("/login")]
|
||||||
pub fn login(sessions: &State<Sessions>) -> &'static str {
|
pub fn login(sessions: &State<Sessions>) -> &'static str {
|
||||||
sessions.insert(
|
sessions.insert("123456".into(), SessionData::new(1));
|
||||||
"123456".into(),
|
|
||||||
SessionData::new(1),
|
|
||||||
);
|
|
||||||
|
|
||||||
":D"
|
":D"
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@ fn rocket() -> _ {
|
|||||||
.manage(auth::session::Sessions::new())
|
.manage(auth::session::Sessions::new())
|
||||||
.register("/", catchers![view::not_authorized])
|
.register("/", catchers![view::not_authorized])
|
||||||
.mount("/", routes![controller::index,])
|
.mount("/", routes![controller::index,])
|
||||||
.mount("/f", routes![
|
.mount(
|
||||||
controller::user::create_user,
|
"/f",
|
||||||
controller::login::login,
|
routes![controller::user::create_user, controller::login::login,],
|
||||||
])
|
)
|
||||||
.mount("/static", FileServer::from("static"))
|
.mount("/static", FileServer::from("static"))
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ pub fn default_skeleton(content: Markup) -> Markup {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[catch(401)]
|
#[catch(401)]
|
||||||
pub fn not_authorized() -> Markup {
|
pub fn not_authorized() -> Markup {
|
||||||
html! {
|
html! {
|
||||||
|
Loading…
Reference in New Issue
Block a user