Skip to main content

heldar_kernel/models/
auth.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4
5/// Operator account. `password_hash` is never serialized; use [`UserView`] for output.
6#[derive(Debug, Clone, FromRow)]
7pub struct User {
8    pub id: String,
9    pub username: String,
10    pub password_hash: String,
11    pub role: String,
12    pub display_name: Option<String>,
13    pub active: bool,
14    pub created_at: DateTime<Utc>,
15    pub updated_at: DateTime<Utc>,
16}
17
18#[derive(Debug, Clone, Serialize)]
19pub struct UserView {
20    pub id: String,
21    pub username: String,
22    pub role: String,
23    pub display_name: Option<String>,
24    pub active: bool,
25    pub created_at: DateTime<Utc>,
26    pub updated_at: DateTime<Utc>,
27}
28
29impl From<User> for UserView {
30    fn from(u: User) -> Self {
31        UserView {
32            id: u.id,
33            username: u.username,
34            role: u.role,
35            display_name: u.display_name,
36            active: u.active,
37            created_at: u.created_at,
38            updated_at: u.updated_at,
39        }
40    }
41}
42
43#[derive(Debug, Deserialize)]
44pub struct UserCreate {
45    pub username: String,
46    pub password: String,
47    pub role: Option<String>,
48    pub display_name: Option<String>,
49    pub active: Option<bool>,
50}
51
52#[derive(Debug, Deserialize, Default)]
53pub struct UserUpdate {
54    pub password: Option<String>,
55    pub role: Option<String>,
56    pub display_name: Option<String>,
57    pub active: Option<bool>,
58}
59
60#[derive(Debug, Deserialize)]
61pub struct LoginRequest {
62    pub username: String,
63    pub password: String,
64}
65
66#[derive(Debug, Clone, FromRow)]
67pub struct ApiKey {
68    pub id: String,
69    pub name: String,
70    /// Mapped from the row for completeness; never exposed (see [`ApiKeyView`]).
71    pub key_hash: String,
72    pub key_prefix: String,
73    pub role: String,
74    pub active: bool,
75    pub last_used_at: Option<DateTime<Utc>>,
76    pub created_at: DateTime<Utc>,
77}
78
79#[derive(Debug, Clone, Serialize)]
80pub struct ApiKeyView {
81    pub id: String,
82    pub name: String,
83    pub key_prefix: String,
84    pub role: String,
85    pub active: bool,
86    pub last_used_at: Option<DateTime<Utc>>,
87    pub created_at: DateTime<Utc>,
88}
89
90impl From<ApiKey> for ApiKeyView {
91    fn from(k: ApiKey) -> Self {
92        ApiKeyView {
93            id: k.id,
94            name: k.name,
95            key_prefix: k.key_prefix,
96            role: k.role,
97            active: k.active,
98            last_used_at: k.last_used_at,
99            created_at: k.created_at,
100        }
101    }
102}
103
104#[derive(Debug, Deserialize)]
105pub struct ApiKeyCreate {
106    pub name: String,
107    pub role: Option<String>,
108}