cal_core/rest/
auth.rs

1// File: cal-core/src/rest/auth.rs
2
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "openapi")]
5use utoipa::ToSchema;
6use validator::Validate;
7
8/// Login request with email and password
9#[derive(Debug, Serialize, Deserialize, Validate)]
10#[cfg_attr(feature = "openapi", derive(ToSchema))]
11#[cfg_attr(feature = "openapi", schema(
12    title ="Authentication request with email and password",
13    example = json!({
14        "email": "user@example.com",
15        "password": "SecurePassword123!"
16    })
17))]
18pub struct LoginRequest {
19    /// User's email address
20    #[validate(email)]
21    #[cfg_attr(feature = "openapi", schema(example = "user@example.com", format = "email"))]
22    pub email: String,
23
24    /// User's password
25    #[validate(length(min = 1))]
26    #[cfg_attr(feature = "openapi", schema(example = "SecurePassword123!", min_length = 1))]
27    pub password: String,
28}
29
30/// Response after successful or failed login
31#[derive(Debug, Serialize, Deserialize)]
32#[cfg_attr(feature = "openapi", derive(ToSchema))]
33#[cfg_attr(feature = "openapi", schema(
34    title ="Login response containing authentication token and user information",
35    example = json!({
36        "success": true,
37        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
38        "user_id": "507f1f77bcf86cd799439011",
39        "agent_id": "agent_123",
40        "permissions": ["read:contacts", "write:contacts", "admin:account"],
41        "softphone_enabled": true
42    })
43))]
44pub struct LoginResponse {
45    /// Whether the login was successful
46    pub success: bool,
47
48    /// JWT authentication token (present on success)
49    #[cfg_attr(feature = "openapi", schema(example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."))]
50    pub token: Option<String>,
51
52    /// User's unique identifier
53    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
54    pub user_id: Option<String>,
55
56    /// Agent identifier if user is an agent
57    #[cfg_attr(feature = "openapi", schema(example = "agent_123"))]
58    pub agent_id: Option<String>,
59
60    /// List of user permissions
61    #[cfg_attr(feature = "openapi", schema(example = json!(["read:contacts", "write:contacts"])))]
62    pub permissions: Option<Vec<String>>,
63
64    /// Error message (present on failure)
65    #[cfg_attr(feature = "openapi", schema(example = "Invalid credentials"))]
66    pub error: Option<String>,
67
68    /// Whether softphone is enabled for this user
69    #[cfg_attr(feature = "openapi", schema(example = true))]
70    pub softphone_enabled: Option<bool>,
71}
72
73/// Logout request
74#[derive(Debug, Serialize, Deserialize)]
75#[cfg_attr(feature = "openapi", derive(ToSchema))]
76#[cfg_attr(feature = "openapi", schema(
77    title ="Logout request with token or session ID"
78))]
79pub struct LogoutRequest {
80    /// Authentication token to invalidate
81    #[cfg_attr(feature = "openapi", schema(example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."))]
82    pub token: Option<String>,
83
84    /// Session ID to terminate
85    #[cfg_attr(feature = "openapi", schema(example = "sess_1234567890"))]
86    pub session_id: Option<String>,
87}
88
89/// Password reset request
90#[derive(Debug, Serialize, Deserialize, Validate)]
91#[cfg_attr(feature = "openapi", derive(ToSchema))]
92#[cfg_attr(feature = "openapi", schema(
93    title ="Request to initiate password reset process",
94    example = json!({
95        "email": "user@example.com"
96    })
97))]
98pub struct PasswordResetRequest {
99    /// Email address to send reset instructions to
100    #[validate(email)]
101    #[cfg_attr(feature = "openapi", schema(example = "user@example.com", format = "email"))]
102    pub email: String,
103}
104
105/// Password reset confirmation
106#[derive(Debug, Serialize, Deserialize, Validate)]
107#[cfg_attr(feature = "openapi", derive(ToSchema))]
108#[cfg_attr(feature = "openapi", schema(
109    title ="Confirm password reset with token and new password",
110    example = json!({
111        "token": "reset_token_abc123",
112        "new_password": "NewSecurePassword123!"
113    })
114))]
115pub struct PasswordResetConfirm {
116    /// Password reset token from email
117    #[cfg_attr(feature = "openapi", schema(example = "reset_token_abc123"))]
118    pub token: String,
119
120    /// New password (minimum 8 characters)
121    #[validate(length(min = 8))]
122    #[cfg_attr(feature = "openapi", schema(example = "NewSecurePassword123!", min_length = 8))]
123    pub new_password: String,
124}
125
126/// User registration request
127#[derive(Debug, Serialize, Deserialize, Validate)]
128#[cfg_attr(feature = "openapi", derive(ToSchema))]
129#[cfg_attr(feature = "openapi", schema(
130    title ="New user registration request",
131    example = json!({
132        "email": "newuser@example.com",
133        "password": "SecurePassword123!",
134        "first_name": "John",
135        "last_name": "Doe",
136        "account_id": "507f1f77bcf86cd799439011"
137    })
138))]
139pub struct RegisterRequest {
140    /// Email address for the new user
141    #[validate(email)]
142    #[cfg_attr(feature = "openapi", schema(example = "newuser@example.com", format = "email"))]
143    pub email: String,
144
145    /// Password (minimum 8 characters)
146    #[validate(length(min = 8))]
147    #[cfg_attr(feature = "openapi", schema(example = "SecurePassword123!", min_length = 8))]
148    pub password: String,
149
150    /// User's first name
151    #[cfg_attr(feature = "openapi", schema(example = "John"))]
152    pub first_name: String,
153
154    /// User's last name
155    #[cfg_attr(feature = "openapi", schema(example = "Doe"))]
156    pub last_name: String,
157
158    /// Optional account ID to associate with
159    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
160    pub account_id: Option<String>,
161}
162
163/// Registration response
164#[derive(Debug, Serialize, Deserialize)]
165#[cfg_attr(feature = "openapi", derive(ToSchema))]
166#[cfg_attr(feature = "openapi", schema(
167    title ="Response after user registration attempt",
168    example = json!({
169        "success": true,
170        "user_id": "507f1f77bcf86cd799439011",
171        "message": "Registration successful. Please check your email to verify your account."
172    })
173))]
174pub struct RegisterResponse {
175    /// Whether registration was successful
176    pub success: bool,
177
178    /// New user's ID (present on success)
179    #[cfg_attr(feature = "openapi", schema(example = "507f1f77bcf86cd799439011"))]
180    pub user_id: Option<String>,
181
182    /// Success or informational message
183    #[cfg_attr(feature = "openapi", schema(example = "Registration successful"))]
184    pub message: Option<String>,
185
186    /// Error message (present on failure)
187    #[cfg_attr(feature = "openapi", schema(example = "Email already registered"))]
188    pub error: Option<String>,
189}