lmrc-cli 0.3.16

CLI tool for scaffolding LMRC Stack infrastructure projects
Documentation
//! Authentication ports (trait definitions)

use async_trait::async_trait;
use crate::error::Result;

/// User information returned after successful authentication
#[derive(Debug, Clone)]
pub struct AuthUser {
    pub id: i64,
    pub email: String,
    pub role: String,
}

/// Session data
#[derive(Debug, Clone)]
pub struct Session {
    pub token: String,
    pub user_id: i64,
    pub expires_at: chrono::NaiveDateTime,
}

/// Port: Authentication provider
///
/// Implement this trait to add new authentication methods
#[async_trait]
pub trait AuthProvider: Send + Sync {
    /// Authenticate user with credentials
    async fn authenticate(&self, email: &str, password: &str) -> Result<AuthUser>;

    /// Create a new session for authenticated user
    async fn create_session(&self, user_id: i64) -> Result<Session>;

    /// Validate session token and return user
    async fn validate_session(&self, token: &str) -> Result<Option<AuthUser>>;

    /// Destroy session (logout)
    async fn destroy_session(&self, token: &str) -> Result<()>;

    /// Get user by ID
    async fn get_user(&self, user_id: i64) -> Result<Option<AuthUser>>;
}

/// Port: Session storage
///
/// Implement this trait to use different session backends (Redis, etc.)
#[async_trait]
pub trait SessionStore: Send + Sync {
    /// Store a session
    async fn store(&self, session: &Session) -> Result<()>;

    /// Retrieve a session by token
    async fn get(&self, token: &str) -> Result<Option<Session>>;

    /// Delete a session
    async fn delete(&self, token: &str) -> Result<()>;

    /// Clean up expired sessions
    async fn cleanup_expired(&self) -> Result<usize>;
}