pub struct SessionManager<DB: DatabaseAdapter> { /* private fields */ }Expand description
Session manager handles session creation, validation, and cleanup
Implementations§
Source§impl<DB: DatabaseAdapter> SessionManager<DB>
impl<DB: DatabaseAdapter> SessionManager<DB>
pub fn new(config: Arc<AuthConfig>, database: Arc<DB>) -> Self
Sourcepub async fn create_session(
&self,
user: &impl AuthUser,
ip_address: Option<String>,
user_agent: Option<String>,
) -> AuthResult<DB::Session>
pub async fn create_session( &self, user: &impl AuthUser, ip_address: Option<String>, user_agent: Option<String>, ) -> AuthResult<DB::Session>
Create a new session for a user
Sourcepub async fn get_session(&self, token: &str) -> AuthResult<Option<DB::Session>>
pub async fn get_session(&self, token: &str) -> AuthResult<Option<DB::Session>>
Get session by token
Sourcepub async fn delete_session(&self, token: &str) -> AuthResult<()>
pub async fn delete_session(&self, token: &str) -> AuthResult<()>
Delete a session
Sourcepub async fn delete_user_sessions(&self, user_id: &str) -> AuthResult<()>
pub async fn delete_user_sessions(&self, user_id: &str) -> AuthResult<()>
Delete all sessions for a user
Sourcepub async fn list_user_sessions(
&self,
user_id: &str,
) -> AuthResult<Vec<DB::Session>>
pub async fn list_user_sessions( &self, user_id: &str, ) -> AuthResult<Vec<DB::Session>>
Get all active sessions for a user
Sourcepub async fn revoke_session(&self, token: &str) -> AuthResult<bool>
pub async fn revoke_session(&self, token: &str) -> AuthResult<bool>
Revoke a specific session by token
Sourcepub async fn revoke_all_user_sessions(&self, user_id: &str) -> AuthResult<usize>
pub async fn revoke_all_user_sessions(&self, user_id: &str) -> AuthResult<usize>
Revoke all sessions for a user
Sourcepub async fn revoke_other_user_sessions(
&self,
user_id: &str,
current_token: &str,
) -> AuthResult<usize>
pub async fn revoke_other_user_sessions( &self, user_id: &str, current_token: &str, ) -> AuthResult<usize>
Revoke all sessions for a user except the current one
Sourcepub async fn cleanup_expired_sessions(&self) -> AuthResult<usize>
pub async fn cleanup_expired_sessions(&self) -> AuthResult<usize>
Cleanup expired sessions
Sourcepub fn is_session_fresh(&self, session: &impl AuthSession) -> bool
pub fn is_session_fresh(&self, session: &impl AuthSession) -> bool
Check whether a session is “fresh” (created recently enough for sensitive operations like password change or account deletion).
Returns true when fresh_age is set and
session.created_at() + fresh_age > now.
If fresh_age is None, the session is never considered fresh.
Sourcepub fn validate_token_format(&self, token: &str) -> bool
pub fn validate_token_format(&self, token: &str) -> bool
Validate session token format
Sourcepub fn extract_session_token(&self, req: &AuthRequest) -> Option<String>
pub fn extract_session_token(&self, req: &AuthRequest) -> Option<String>
Extract session token from a request.
Tries Bearer token from Authorization header first, then falls back to parsing the configured cookie from the Cookie header.