pub struct CookieSessionService { /* private fields */ }Expand description
Long-lived service for cookie-backed sessions.
CookieSessionService wraps a [SessionStore], a cookie signing Key,
and the full CookieSessionsConfig. It is constructed once at startup,
held in application state, and used by the session middleware and by
cross-transport management endpoints.
§Construction
let svc = CookieSessionService::new(db, config)?;Construction validates that the cookie secret meets the minimum length requirement and fails fast at startup if it does not.
Implementations§
Source§impl CookieSessionService
impl CookieSessionService
Sourcepub fn new(db: Database, config: CookieSessionsConfig) -> Result<Self>
pub fn new(db: Database, config: CookieSessionsConfig) -> Result<Self>
Construct a new CookieSessionService.
Derives the HMAC signing key from config.cookie.secret. Fails if the
secret is shorter than 64 characters.
§Errors
Returns Error::internal if the cookie secret is too short.
Sourcepub async fn list(&self, user_id: &str) -> Result<Vec<Session>>
pub async fn list(&self, user_id: &str) -> Result<Vec<Session>>
List all active (non-expired) sessions for the given user.
§Errors
Returns an error if the database query fails.
Sourcepub async fn revoke(&self, user_id: &str, id: &str) -> Result<()>
pub async fn revoke(&self, user_id: &str, id: &str) -> Result<()>
Revoke a specific session by its ULID identifier.
Looks up the session row by id, verifies that it belongs to user_id,
and destroys it. Returns 404 auth:session_not_found if the session does
not exist or belongs to a different user.
§Errors
Returns 404 auth:session_not_found on ownership mismatch, or an
internal error if the database operation fails.
Sourcepub async fn revoke_all(&self, user_id: &str) -> Result<()>
pub async fn revoke_all(&self, user_id: &str) -> Result<()>
Sourcepub async fn revoke_all_except(
&self,
user_id: &str,
keep_id: &str,
) -> Result<()>
pub async fn revoke_all_except( &self, user_id: &str, keep_id: &str, ) -> Result<()>
Revoke all sessions for the given user except the one with keep_id.
Used to implement “log out other devices” while keeping the caller’s current session active.
§Errors
Returns an error if the database delete fails.
Sourcepub async fn cleanup_expired(&self) -> Result<u64>
pub async fn cleanup_expired(&self) -> Result<u64>
Delete all expired sessions from the store.
Returns the number of rows deleted. Schedule this periodically (e.g.
via a cron job) to keep the authenticated_sessions table small.
§Errors
Returns an error if the database delete fails.
Sourcepub fn layer(&self) -> CookieSessionLayer
pub fn layer(&self) -> CookieSessionLayer
Build a CookieSessionLayer from this service.
Convenience method so callers can write service.layer() instead of
session::layer(service.clone()).
Trait Implementations§
Source§impl Clone for CookieSessionService
impl Clone for CookieSessionService
Source§fn clone(&self) -> CookieSessionService
fn clone(&self) -> CookieSessionService
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more