lmrc_auth/
traits.rs

1//! Authentication trait definitions
2//!
3//! This module defines the core traits that enable flexible authentication strategies.
4
5use async_trait::async_trait;
6use crate::{error::AuthResult, models::{AuthUser, Session}};
7
8/// Port: Authentication provider
9///
10/// Implement this trait to add new authentication methods (database, LDAP, OAuth, etc.)
11///
12/// ## Example
13///
14/// ```rust
15/// use lmrc_auth::{AuthProvider, AuthUser, Session, AuthResult};
16/// use async_trait::async_trait;
17///
18/// struct CustomAuthProvider {
19///     // Your implementation details
20/// }
21///
22/// #[async_trait]
23/// impl AuthProvider for CustomAuthProvider {
24///     async fn authenticate(&self, email: &str, password: &str) -> AuthResult<AuthUser> {
25///         // Your authentication logic
26///         todo!()
27///     }
28///
29///     async fn create_session(&self, user_id: i64) -> AuthResult<Session> {
30///         // Your session creation logic
31///         todo!()
32///     }
33///
34///     async fn validate_session(&self, token: &str) -> AuthResult<Option<AuthUser>> {
35///         // Your session validation logic
36///         todo!()
37///     }
38///
39///     async fn destroy_session(&self, token: &str) -> AuthResult<()> {
40///         // Your session destruction logic
41///         todo!()
42///     }
43///
44///     async fn get_user(&self, user_id: i64) -> AuthResult<Option<AuthUser>> {
45///         // Your user lookup logic
46///         todo!()
47///     }
48/// }
49/// ```
50#[async_trait]
51pub trait AuthProvider: Send + Sync {
52    /// Authenticate user with credentials
53    ///
54    /// Returns the authenticated user if credentials are valid.
55    async fn authenticate(&self, email: &str, password: &str) -> AuthResult<AuthUser>;
56
57    /// Create a new session for authenticated user
58    ///
59    /// Generates a session token and stores the session.
60    async fn create_session(&self, user_id: i64) -> AuthResult<Session>;
61
62    /// Validate session token and return user
63    ///
64    /// Returns `None` if the session is invalid or expired.
65    async fn validate_session(&self, token: &str) -> AuthResult<Option<AuthUser>>;
66
67    /// Destroy session (logout)
68    ///
69    /// Removes the session from storage.
70    async fn destroy_session(&self, token: &str) -> AuthResult<()>;
71
72    /// Get user by ID
73    ///
74    /// Returns `None` if the user doesn't exist.
75    async fn get_user(&self, user_id: i64) -> AuthResult<Option<AuthUser>>;
76}
77
78/// Port: Session storage
79///
80/// Implement this trait to use different session backends (database, Redis, in-memory, etc.)
81///
82/// ## Example
83///
84/// ```rust
85/// use lmrc_auth::{SessionStore, Session, AuthResult};
86/// use async_trait::async_trait;
87///
88/// struct RedisSessionStore {
89///     // Redis connection pool
90/// }
91///
92/// #[async_trait]
93/// impl SessionStore for RedisSessionStore {
94///     async fn store(&self, session: &Session) -> AuthResult<()> {
95///         // Store in Redis
96///         todo!()
97///     }
98///
99///     async fn get(&self, token: &str) -> AuthResult<Option<Session>> {
100///         // Retrieve from Redis
101///         todo!()
102///     }
103///
104///     async fn delete(&self, token: &str) -> AuthResult<()> {
105///         // Delete from Redis
106///         todo!()
107///     }
108///
109///     async fn cleanup_expired(&self) -> AuthResult<usize> {
110///         // Clean up expired sessions
111///         todo!()
112///     }
113/// }
114/// ```
115#[async_trait]
116pub trait SessionStore: Send + Sync {
117    /// Store a session
118    ///
119    /// Persists the session to the backing storage.
120    async fn store(&self, session: &Session) -> AuthResult<()>;
121
122    /// Retrieve a session by token
123    ///
124    /// Returns `None` if the session doesn't exist.
125    async fn get(&self, token: &str) -> AuthResult<Option<Session>>;
126
127    /// Delete a session
128    ///
129    /// Removes the session from storage.
130    async fn delete(&self, token: &str) -> AuthResult<()>;
131
132    /// Clean up expired sessions
133    ///
134    /// Returns the number of sessions deleted.
135    async fn cleanup_expired(&self) -> AuthResult<usize>;
136}