Skip to main content

openserve/models/
auth.rs

1//! Authentication and authorization data models.
2
3use serde::{Deserialize, Serialize};
4use chrono::{DateTime, Utc};
5
6/// Login request payload
7#[derive(Debug, Clone, Deserialize)]
8pub struct LoginRequest {
9    /// Username for authentication
10    pub username: String,
11    /// User password
12    pub password: String,
13}
14
15/// Registration request payload
16#[derive(Debug, Clone, Deserialize)]
17pub struct RegisterRequest {
18    /// Desired username
19    pub username: String,
20    /// User email address
21    pub email: String,
22    /// User password
23    pub password: String,
24}
25
26/// User information for API responses
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct User {
29    /// Unique identifier for the user
30    pub id: String,
31    /// Username for the user
32    pub username: String,
33    /// Email address of the user
34    pub email: String,
35    /// Role assigned to the user
36    pub role: UserRole,
37    /// Timestamp when the user was created
38    pub created_at: DateTime<Utc>,
39    /// Last login timestamp
40    pub last_login: Option<DateTime<Utc>>,
41}
42
43/// Available user roles in the system
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub enum UserRole {
46    /// Administrator with full system access
47    Admin,
48    /// Regular user with standard access
49    User,
50    /// Read-only user with limited access
51    ReadOnly,
52}
53
54/// JWT authentication token
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct AuthToken {
57    /// JWT access token
58    pub token: String,
59    /// Token expiration timestamp
60    pub expires_at: DateTime<Utc>,
61}
62
63/// Claims embedded in JWT tokens
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct Claims {
66    /// Subject (user ID)
67    pub sub: String,
68    /// Token expiration timestamp
69    pub exp: usize,
70    /// Token issued at timestamp
71    pub iat: usize,
72    /// User role
73    pub role: UserRole,
74}