Skip to content
Clone
main
71 lines 1.8 KB Rust
use std::collections::HashMap;
use std::future::Future;
use actix_web::cookie::time::Duration;
use crate::error::SessionError;
use crate::key::SessionKey;
pub type SessionState = HashMap<String, String>;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SessionDiff {
pub set: HashMap<String, String>,
pub removed: Vec<String>,
}
impl SessionDiff {
pub fn is_empty(&self) -> bool {
self.set.is_empty() && self.removed.is_empty()
}
}
pub trait SessionStore {
fn load(
&self,
session_key: &SessionKey,
) -> impl Future<Output = Result<Option<(SessionState, Duration)>, SessionError>>;
fn save(
&self,
session_state: SessionState,
ttl: &Duration,
) -> impl Future<Output = Result<SessionKey, SessionError>>;
fn update(
&self,
session_key: SessionKey,
diff: SessionDiff,
ttl: &Duration,
previous_user_id: Option<i64>,
user_id: Option<i64>,
) -> impl Future<Output = Result<SessionKey, SessionError>>;
fn rotate(
&self,
session_key: SessionKey,
diff: SessionDiff,
ttl: &Duration,
previous_user_id: Option<i64>,
user_id: Option<i64>,
) -> impl Future<Output = Result<Option<SessionKey>, SessionError>>;
fn update_ttl(
&self,
session_key: &SessionKey,
ttl: &Duration,
user_id: Option<i64>,
) -> impl Future<Output = Result<(), SessionError>>;
fn delete(
&self,
session_key: &SessionKey,
user_id: Option<i64>,
) -> impl Future<Output = Result<(), SessionError>>;
fn revoke_user(&self, user_id: i64) -> impl Future<Output = Result<usize, SessionError>>;
}
pub fn generate_session_key() -> SessionKey {
SessionKey::generate()
}