auth_dto.rs 138 lignes · 3804 octets
/// Authentication DTOs for login/logout
/// Feature 003: Authentication with Sessions and JWT
use serde::{Deserialize, Serialize};
use validator::Validate;

/// Login request body
#[derive(Debug, Deserialize, Serialize, Clone, Validate)]
pub struct LoginRequest {
    /// Username or email
    #[validate(length(min = 1))]
    pub username: String,
    /// Password
    #[validate(length(min = 1))]
    pub password: String,
}

/// User info in login response
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserInfo {
    /// User unique identifier.
    pub id: uuid::Uuid,
    /// User login name.
    pub username: String,
    /// User email address.
    pub email: String,
    /// Roles assigned to the user.
    pub roles: Vec<String>,
    /// Whether the user must change their password on next login.
    pub must_change_password: Option<bool>,
}

/// Login response with tokens
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct LoginResponse {
    /// Access token (JWT)
    pub access_token: String,
    /// Refresh token (optional)
    pub refresh_token: Option<String>,
    /// Token type (usually "Bearer")
    pub token_type: String,
    /// Expiration time in seconds
    pub expires_in: i64,
    /// User information
    pub user: UserInfo,
}

/// Refresh token request
#[derive(Debug, Deserialize, Serialize, Clone, Validate)]
pub struct RefreshTokenRequest {
    /// Refresh token
    #[validate(length(min = 1))]
    pub refresh_token: String,
}

/// Refresh token response
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RefreshTokenResponse {
    /// New access token
    pub access_token: String,
    /// New refresh token (optional)
    pub refresh_token: Option<String>,
    /// Token type
    pub token_type: String,
    /// Expiration time in seconds
    pub expires_in: i64,
}

/// Logout response
#[derive(Debug, Serialize, Deserialize)]
pub struct LogoutResponse {
    /// Success message
    pub message: String,
}

/// User info response (for /auth/me)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserInfoResponse {
    /// User ID
    pub id: String,
    /// Username
    pub username: String,
    /// Email
    pub email: String,
    /// User roles
    pub roles: Vec<String>,
}

/// Current user response (for GET /auth/me)
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CurrentUserResponse {
    /// User unique identifier.
    pub id: uuid::Uuid,
    /// User login name.
    pub username: String,
    /// User email address.
    pub email: String,
    /// List of roles assigned to the user.
    pub roles: Vec<String>,
    /// Timestamp when the account was created.
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Timestamp of the last account update.
    pub updated_at: chrono::DateTime<chrono::Utc>,
    /// Whether the user must change their password on next login.
    pub must_change_password: bool,
}

/// Alias for RefreshRequest (backward compatibility)
pub type RefreshRequest = RefreshTokenRequest;

/// Alias for RefreshResponse (backward compatibility)
pub type RefreshResponse = RefreshTokenResponse;

/// Register request body (public registration)
#[derive(Debug, Deserialize, Serialize, Clone, Validate)]
pub struct RegisterRequest {
    /// Username (must be unique)
    #[validate(length(min = 3, max = 50))]
    pub username: String,
    /// Email (must be unique and valid)
    #[validate(email)]
    pub email: String,
    /// Password
    #[validate(length(min = 8))]
    pub password: String,
}

/// Register response
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RegisterResponse {
    /// Success message
    pub message: String,
    /// User ID
    pub user_id: uuid::Uuid,
    /// Username
    pub username: String,
    /// Email
    pub email: String,
}