Skip to main content

rs_auth_core/
store.rs

1use async_trait::async_trait;
2
3use crate::error::AuthError;
4use crate::types::{
5    Account, NewAccount, NewOAuthState, NewSession, NewVerification, OAuthState, Session, User,
6    Verification,
7};
8
9/// Storage backend for user records.
10#[async_trait]
11pub trait UserStore: Send + Sync {
12    async fn create_user(
13        &self,
14        email: &str,
15        name: Option<&str>,
16        password_hash: Option<&str>,
17    ) -> Result<User, AuthError>;
18
19    async fn find_by_email(&self, email: &str) -> Result<Option<User>, AuthError>;
20
21    async fn find_by_id(&self, id: i64) -> Result<Option<User>, AuthError>;
22
23    async fn set_email_verified(&self, user_id: i64) -> Result<(), AuthError>;
24
25    async fn update_password(&self, user_id: i64, password_hash: &str) -> Result<(), AuthError>;
26
27    async fn delete_user(&self, user_id: i64) -> Result<(), AuthError>;
28}
29
30/// Storage backend for session records.
31#[async_trait]
32pub trait SessionStore: Send + Sync {
33    async fn create_session(&self, session: NewSession) -> Result<Session, AuthError>;
34
35    async fn find_by_token_hash(&self, token_hash: &str) -> Result<Option<Session>, AuthError>;
36
37    async fn find_by_user_id(&self, user_id: i64) -> Result<Vec<Session>, AuthError>;
38
39    async fn delete_session(&self, id: i64) -> Result<(), AuthError>;
40
41    async fn delete_by_user_id(&self, user_id: i64) -> Result<(), AuthError>;
42
43    async fn delete_expired(&self) -> Result<u64, AuthError>;
44}
45
46/// Storage backend for verification token records.
47#[async_trait]
48pub trait VerificationStore: Send + Sync {
49    async fn create_verification(
50        &self,
51        verification: NewVerification,
52    ) -> Result<Verification, AuthError>;
53
54    async fn find_by_identifier(&self, identifier: &str)
55    -> Result<Option<Verification>, AuthError>;
56
57    async fn find_by_token_hash(&self, token_hash: &str)
58    -> Result<Option<Verification>, AuthError>;
59
60    async fn delete_verification(&self, id: i64) -> Result<(), AuthError>;
61
62    async fn delete_by_identifier(&self, identifier: &str) -> Result<(), AuthError>;
63
64    async fn delete_expired(&self) -> Result<u64, AuthError>;
65}
66
67/// Storage backend for OAuth account records.
68#[async_trait]
69pub trait AccountStore: Send + Sync {
70    async fn create_account(&self, account: NewAccount) -> Result<Account, AuthError>;
71    async fn find_by_provider(
72        &self,
73        provider_id: &str,
74        account_id: &str,
75    ) -> Result<Option<Account>, AuthError>;
76    async fn find_by_user_id(&self, user_id: i64) -> Result<Vec<Account>, AuthError>;
77    async fn delete_account(&self, id: i64) -> Result<(), AuthError>;
78}
79
80/// Storage backend for transient OAuth state records.
81#[async_trait]
82pub trait OAuthStateStore: Send + Sync {
83    async fn create_oauth_state(&self, state: NewOAuthState) -> Result<OAuthState, AuthError>;
84    async fn find_by_csrf_state(&self, csrf_state: &str) -> Result<Option<OAuthState>, AuthError>;
85    async fn delete_oauth_state(&self, id: i64) -> Result<(), AuthError>;
86    async fn delete_expired_oauth_states(&self) -> Result<u64, AuthError>;
87}