Skip to main content

rs_auth_core/
store.rs

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