steid

@jamesgill /

steid/src/domain/repository/session_repo.rs
Rust1.6 KBCode·Blame·History·Raw
1use std::time::SystemTime;
2
3use super::RepositoryResult;
4use crate::domain::{
5 UserId,
6 session::{Session, SessionTokenHash},
7};
8
9/// Persistence for [`Session`].
10pub trait SessionRepository: Send + Sync {
11 /// Looks a session up by token hash.
12 ///
13 /// Returns whatever is stored, expired or not — expiry is the caller's decision so
14 /// that a stale row and a missing row stay distinguishable here.
15 fn find(
16 &self,
17 token_hash: &SessionTokenHash,
18 ) -> impl Future<Output = RepositoryResult<Option<Session>>> + Send;
19
20 fn save(&self, session: &Session) -> impl Future<Output = RepositoryResult<()>> + Send;
21
22 /// Removes a single session. Used on logout.
23 fn delete(
24 &self,
25 token_hash: &SessionTokenHash,
26 ) -> impl Future<Output = RepositoryResult<()>> + Send;
27
28 /// Removes every session belonging to a user, returning how many went.
29 ///
30 /// `except` keeps one session alive, which is what separates a password *change*
31 /// from a password *reset*: changing your own password signs out every other
32 /// device and leaves you where you are, while a reset — where the person may be
33 /// signing in from nowhere at all — ends the lot, including whoever else was
34 /// holding a session on that account.
35 fn delete_by_user(
36 &self,
37 user_id: &UserId,
38 except: Option<&SessionTokenHash>,
39 ) -> impl Future<Output = RepositoryResult<u64>> + Send;
40
41 /// Removes every session that expired before `now`, returning how many went.
42 fn delete_expired(&self, now: SystemTime)
43 -> impl Future<Output = RepositoryResult<u64>> + Send;
44}