cal-core 0.2.158

Callable core lib
Documentation
// File: cal-core/src/rest/auth.rs

use serde::{Deserialize, Serialize};
#[cfg(feature = "openapi")]
use utoipa::ToSchema;
use validator::Validate;

/// Login request with email and password
#[derive(Debug, Serialize, Deserialize, Validate)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Authentication request with email and password",
    example = json!({
        "email": "user@example.com",
        "password": "SecurePassword123!"
    })
))]
pub struct LoginRequest {
    /// User's email address
    #[validate(email)]
    #[cfg_attr(feature = "openapi", schema(example = "user@example.com", format = "email"))]
    pub email: String,

    /// User's password
    #[validate(length(min = 1))]
    #[cfg_attr(feature = "openapi", schema(example = "SecurePassword123!", min_length = 1))]
    pub password: String,
}

/// Response after successful or failed login
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Login response containing authentication token and user information",
    example = json!({
        "success": true,
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
        "user_id": "507f1f77bcf86cd799439011",
        "agent_id": "agent_123",
        "permissions": ["read:contacts", "write:contacts", "admin:account"],
        "softphone_enabled": true
    })
))]
pub struct LoginResponse {
    /// Whether the login was successful
    pub success: bool,

    /// JWT authentication token (present on success)
    #[cfg_attr(feature = "openapi", schema(example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."))]
    pub token: Option<String>,

    /// User's unique identifier
    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
    pub user_id: Option<String>,

    /// Agent identifier if user is an agent
    #[cfg_attr(feature = "openapi", schema(example = "agent_123"))]
    pub agent_id: Option<String>,

    /// List of user permissions
    #[cfg_attr(feature = "openapi", schema(example = json!(["read:contacts", "write:contacts"])))]
    pub permissions: Option<Vec<String>>,

    /// Error message (present on failure)
    #[cfg_attr(feature = "openapi", schema(example = "Invalid credentials"))]
    pub error: Option<String>,

    /// Whether softphone is enabled for this user
    #[cfg_attr(feature = "openapi", schema(example = true))]
    pub softphone_enabled: Option<bool>,
}

/// Logout request
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Logout request with token or session ID"
))]
pub struct LogoutRequest {
    /// Authentication token to invalidate
    #[cfg_attr(feature = "openapi", schema(example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."))]
    pub token: Option<String>,

    /// Session ID to terminate
    #[cfg_attr(feature = "openapi", schema(example = "sess_1234567890"))]
    pub session_id: Option<String>,
}

/// Password reset request
#[derive(Debug, Serialize, Deserialize, Validate)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Request to initiate password reset process",
    example = json!({
        "email": "user@example.com"
    })
))]
pub struct PasswordResetRequest {
    /// Email address to send reset instructions to
    #[validate(email)]
    #[cfg_attr(feature = "openapi", schema(example = "user@example.com", format = "email"))]
    pub email: String,
}

/// Password reset confirmation
#[derive(Debug, Serialize, Deserialize, Validate)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Confirm password reset with token and new password",
    example = json!({
        "token": "reset_token_abc123",
        "new_password": "NewSecurePassword123!"
    })
))]
pub struct PasswordResetConfirm {
    /// Password reset token from email
    #[cfg_attr(feature = "openapi", schema(example = "reset_token_abc123"))]
    pub token: String,

    /// New password (minimum 8 characters)
    #[validate(length(min = 8))]
    #[cfg_attr(feature = "openapi", schema(example = "NewSecurePassword123!", min_length = 8))]
    pub new_password: String,
}

/// User registration request
#[derive(Debug, Serialize, Deserialize, Validate)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="New user registration request",
    example = json!({
        "email": "newuser@example.com",
        "password": "SecurePassword123!",
        "first_name": "John",
        "last_name": "Doe",
        "account_id": "507f1f77bcf86cd799439011"
    })
))]
pub struct RegisterRequest {
    /// Email address for the new user
    #[validate(email)]
    #[cfg_attr(feature = "openapi", schema(example = "newuser@example.com", format = "email"))]
    pub email: String,

    /// Password (minimum 8 characters)
    #[validate(length(min = 8))]
    #[cfg_attr(feature = "openapi", schema(example = "SecurePassword123!", min_length = 8))]
    pub password: String,

    /// User's first name
    #[cfg_attr(feature = "openapi", schema(example = "John"))]
    pub first_name: String,

    /// User's last name
    #[cfg_attr(feature = "openapi", schema(example = "Doe"))]
    pub last_name: String,

    /// Optional account ID to associate with
    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
    pub account_id: Option<String>,
}

/// Registration response
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[cfg_attr(feature = "openapi", schema(
    title ="Response after user registration attempt",
    example = json!({
        "success": true,
        "user_id": "507f1f77bcf86cd799439011",
        "message": "Registration successful. Please check your email to verify your account."
    })
))]
pub struct RegisterResponse {
    /// Whether registration was successful
    pub success: bool,

    /// New user's ID (present on success)
    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
    pub user_id: Option<String>,

    /// Success or informational message
    #[cfg_attr(feature = "openapi", schema(example = "Registration successful"))]
    pub message: Option<String>,

    /// Error message (present on failure)
    #[cfg_attr(feature = "openapi", schema(example = "Email already registered"))]
    pub error: Option<String>,
}