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::{
|
||||
http::Status,
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::{collections::HashMap, sync::{Arc, Mutex}};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
|
||||
pub struct Sessions {
|
||||
sessions: Mutex<HashMap<String, SessionData>>,
|
||||
@ -19,22 +19,64 @@ impl Sessions {
|
||||
}
|
||||
|
||||
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) {
|
||||
Some(s) => Some(s.clone()),
|
||||
None => None,
|
||||
// Remove expired sessions
|
||||
|
||||
// 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)]
|
||||
pub struct SessionData {
|
||||
user_id: i32,
|
||||
expires_at: u64,
|
||||
}
|
||||
|
||||
impl SessionData {
|
||||
// Create a new session with a duration set to 1 hour
|
||||
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")]
|
||||
pub fn login(sessions: &State<Sessions>) -> &'static str {
|
||||
sessions.insert(
|
||||
"123456".into(),
|
||||
SessionData::new(1),
|
||||
);
|
||||
sessions.insert("123456".into(), SessionData::new(1));
|
||||
|
||||
":D"
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ fn rocket() -> _ {
|
||||
.manage(auth::session::Sessions::new())
|
||||
.register("/", catchers![view::not_authorized])
|
||||
.mount("/", routes![controller::index,])
|
||||
.mount("/f", routes![
|
||||
controller::user::create_user,
|
||||
controller::login::login,
|
||||
])
|
||||
.mount(
|
||||
"/f",
|
||||
routes![controller::user::create_user, controller::login::login,],
|
||||
)
|
||||
.mount("/static", FileServer::from("static"))
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ pub fn default_skeleton(content: Markup) -> Markup {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[catch(401)]
|
||||
pub fn not_authorized() -> Markup {
|
||||
html! {
|
||||
|
Loading…
Reference in New Issue
Block a user