acton-service 0.20.0

Production-ready Rust backend framework with type-enforced API versioning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Turso/libsql account storage backend

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::sync::Arc;

use super::AccountStorage;
use crate::accounts::types::{Account, AccountId, AccountStatus};
use crate::error::Error;

/// Turso-backed account storage
pub struct TursoAccountStorage {
    db: Arc<libsql::Database>,
}

impl TursoAccountStorage {
    /// Create a new Turso account storage and initialize the schema
    pub async fn new(db: Arc<libsql::Database>) -> Result<Self, Error> {
        let storage = Self { db };
        storage.initialize().await?;
        Ok(storage)
    }

    async fn initialize(&self) -> Result<(), Error> {
        let conn = self
            .db
            .connect()
            .map_err(|e| Error::Internal(format!("Failed to connect for accounts init: {}", e)))?;

        conn.execute(
            r#"
            CREATE TABLE IF NOT EXISTS accounts (
                id TEXT PRIMARY KEY,
                email TEXT NOT NULL UNIQUE,
                username TEXT,
                password_hash TEXT,
                status TEXT NOT NULL DEFAULT 'pending_verification',
                roles TEXT NOT NULL DEFAULT '[]',
                email_verified INTEGER NOT NULL DEFAULT 0,
                email_verified_at TEXT,
                last_login_at TEXT,
                locked_at TEXT,
                locked_reason TEXT,
                disabled_at TEXT,
                disabled_reason TEXT,
                expires_at TEXT,
                password_changed_at TEXT,
                failed_login_count INTEGER NOT NULL DEFAULT 0,
                metadata TEXT,
                created_at TEXT NOT NULL,
                updated_at TEXT NOT NULL
            )
            "#,
            (),
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to create accounts table: {}", e)))?;

        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_accounts_email ON accounts(email)",
            (),
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to create email index: {}", e)))?;

        conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_accounts_status ON accounts(status)",
            (),
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to create status index: {}", e)))?;

        Ok(())
    }

    fn conn(&self) -> Result<libsql::Connection, Error> {
        self.db
            .connect()
            .map_err(|e| Error::Internal(format!("Failed to connect: {}", e)))
    }
}

fn parse_datetime(s: &str) -> Option<DateTime<Utc>> {
    DateTime::parse_from_rfc3339(s)
        .ok()
        .map(|dt| dt.with_timezone(&Utc))
}

fn row_to_account(row: &libsql::Row) -> Result<Account, Error> {
    let map_err = |field: &str, e: libsql::Error| {
        Error::Internal(format!("Failed to read field '{}': {}", field, e))
    };

    let id_str: String = row.get(0).map_err(|e| map_err("id", e))?;
    let id = id_str.parse().unwrap_or_else(|_| AccountId::new());

    let email: String = row.get(1).map_err(|e| map_err("email", e))?;
    let username: Option<String> = row.get(2).map_err(|e| map_err("username", e))?;
    let password_hash: Option<String> = row.get(3).map_err(|e| map_err("password_hash", e))?;
    let status_str: String = row.get(4).map_err(|e| map_err("status", e))?;
    let status = status_str
        .parse()
        .unwrap_or(AccountStatus::PendingVerification);
    let roles_str: String = row.get(5).map_err(|e| map_err("roles", e))?;
    let roles: Vec<String> = serde_json::from_str(&roles_str).unwrap_or_default();
    let email_verified_int: i64 = row.get(6).map_err(|e| map_err("email_verified", e))?;
    let email_verified = email_verified_int != 0;
    let email_verified_at: Option<String> =
        row.get(7).map_err(|e| map_err("email_verified_at", e))?;
    let last_login_at: Option<String> = row.get(8).map_err(|e| map_err("last_login_at", e))?;
    let locked_at: Option<String> = row.get(9).map_err(|e| map_err("locked_at", e))?;
    let locked_reason: Option<String> = row.get(10).map_err(|e| map_err("locked_reason", e))?;
    let disabled_at: Option<String> = row.get(11).map_err(|e| map_err("disabled_at", e))?;
    let disabled_reason: Option<String> = row.get(12).map_err(|e| map_err("disabled_reason", e))?;
    let expires_at: Option<String> = row.get(13).map_err(|e| map_err("expires_at", e))?;
    let password_changed_at: Option<String> =
        row.get(14).map_err(|e| map_err("password_changed_at", e))?;
    let failed_login_count: i64 = row.get(15).map_err(|e| map_err("failed_login_count", e))?;
    let metadata_str: Option<String> = row.get(16).map_err(|e| map_err("metadata", e))?;
    let created_at_str: String = row.get(17).map_err(|e| map_err("created_at", e))?;
    let updated_at_str: String = row.get(18).map_err(|e| map_err("updated_at", e))?;

    Ok(Account {
        id,
        email,
        username,
        password_hash,
        status,
        roles,
        email_verified,
        email_verified_at: email_verified_at.and_then(|s| parse_datetime(&s)),
        last_login_at: last_login_at.and_then(|s| parse_datetime(&s)),
        locked_at: locked_at.and_then(|s| parse_datetime(&s)),
        locked_reason,
        disabled_at: disabled_at.and_then(|s| parse_datetime(&s)),
        disabled_reason,
        expires_at: expires_at.and_then(|s| parse_datetime(&s)),
        password_changed_at: password_changed_at.and_then(|s| parse_datetime(&s)),
        failed_login_count: failed_login_count as u32,
        metadata: metadata_str.and_then(|s| serde_json::from_str(&s).ok()),
        created_at: parse_datetime(&created_at_str).unwrap_or_else(Utc::now),
        updated_at: parse_datetime(&updated_at_str).unwrap_or_else(Utc::now),
    })
}

fn opt_dt(dt: &Option<DateTime<Utc>>) -> Option<String> {
    dt.map(|d| d.to_rfc3339())
}

#[async_trait]
impl AccountStorage for TursoAccountStorage {
    async fn create(&self, account: &Account) -> Result<(), Error> {
        let conn = self.conn()?;
        let roles_json = serde_json::to_string(&account.roles).unwrap_or_else(|_| "[]".into());
        let metadata_json = account
            .metadata
            .as_ref()
            .map(|m| serde_json::to_string(m).unwrap_or_default());

        conn.execute(
            r#"
            INSERT INTO accounts (
                id, email, username, password_hash, status, roles,
                email_verified, email_verified_at, last_login_at,
                locked_at, locked_reason, disabled_at, disabled_reason,
                expires_at, password_changed_at, failed_login_count,
                metadata, created_at, updated_at
            ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18, ?19)
            "#,
            libsql::params![
                account.id.as_str(),
                account.email.as_str(),
                account.username.as_deref(),
                account.password_hash.as_deref(),
                account.status.to_string(),
                roles_json,
                account.email_verified as i64,
                opt_dt(&account.email_verified_at),
                opt_dt(&account.last_login_at),
                opt_dt(&account.locked_at),
                account.locked_reason.as_deref(),
                opt_dt(&account.disabled_at),
                account.disabled_reason.as_deref(),
                opt_dt(&account.expires_at),
                opt_dt(&account.password_changed_at),
                account.failed_login_count as i64,
                metadata_json,
                account.created_at.to_rfc3339(),
                account.updated_at.to_rfc3339(),
            ],
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to create account: {}", e)))?;

        Ok(())
    }

    async fn get_by_id(&self, id: &str) -> Result<Option<Account>, Error> {
        let conn = self.conn()?;
        let mut rows = conn
            .query("SELECT * FROM accounts WHERE id = ?1", libsql::params![id])
            .await
            .map_err(|e| Error::Internal(format!("Failed to get account: {}", e)))?;

        match rows.next().await {
            Ok(Some(row)) => Ok(Some(row_to_account(&row)?)),
            Ok(None) => Ok(None),
            Err(e) => Err(Error::Internal(format!("Failed to read row: {}", e))),
        }
    }

    async fn get_by_email(&self, email: &str) -> Result<Option<Account>, Error> {
        let conn = self.conn()?;
        let mut rows = conn
            .query(
                "SELECT * FROM accounts WHERE email = ?1",
                libsql::params![email],
            )
            .await
            .map_err(|e| Error::Internal(format!("Failed to get account by email: {}", e)))?;

        match rows.next().await {
            Ok(Some(row)) => Ok(Some(row_to_account(&row)?)),
            Ok(None) => Ok(None),
            Err(e) => Err(Error::Internal(format!("Failed to read row: {}", e))),
        }
    }

    async fn get_by_username(&self, username: &str) -> Result<Option<Account>, Error> {
        let conn = self.conn()?;
        let mut rows = conn
            .query(
                "SELECT * FROM accounts WHERE username = ?1",
                libsql::params![username],
            )
            .await
            .map_err(|e| Error::Internal(format!("Failed to get account by username: {}", e)))?;

        match rows.next().await {
            Ok(Some(row)) => Ok(Some(row_to_account(&row)?)),
            Ok(None) => Ok(None),
            Err(e) => Err(Error::Internal(format!("Failed to read row: {}", e))),
        }
    }

    async fn update(&self, account: &Account) -> Result<(), Error> {
        let conn = self.conn()?;
        let roles_json = serde_json::to_string(&account.roles).unwrap_or_else(|_| "[]".into());
        let metadata_json = account
            .metadata
            .as_ref()
            .map(|m| serde_json::to_string(m).unwrap_or_default());

        conn.execute(
            r#"
            UPDATE accounts SET
                email = ?2, username = ?3, password_hash = ?4, status = ?5,
                roles = ?6, email_verified = ?7, email_verified_at = ?8,
                last_login_at = ?9, locked_at = ?10, locked_reason = ?11,
                disabled_at = ?12, disabled_reason = ?13, expires_at = ?14,
                password_changed_at = ?15, failed_login_count = ?16,
                metadata = ?17, updated_at = ?18
            WHERE id = ?1
            "#,
            libsql::params![
                account.id.as_str(),
                account.email.as_str(),
                account.username.as_deref(),
                account.password_hash.as_deref(),
                account.status.to_string(),
                roles_json,
                account.email_verified as i64,
                opt_dt(&account.email_verified_at),
                opt_dt(&account.last_login_at),
                opt_dt(&account.locked_at),
                account.locked_reason.as_deref(),
                opt_dt(&account.disabled_at),
                account.disabled_reason.as_deref(),
                opt_dt(&account.expires_at),
                opt_dt(&account.password_changed_at),
                account.failed_login_count as i64,
                metadata_json,
                Utc::now().to_rfc3339(),
            ],
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to update account: {}", e)))?;

        Ok(())
    }

    async fn update_status(
        &self,
        id: &str,
        status: AccountStatus,
        reason: Option<&str>,
    ) -> Result<(), Error> {
        let conn = self.conn()?;
        let now = Utc::now().to_rfc3339();
        let status_str = status.to_string();

        match status {
            AccountStatus::Disabled => {
                conn.execute(
                    "UPDATE accounts SET status = ?2, disabled_at = ?3, disabled_reason = ?4, updated_at = ?3 WHERE id = ?1",
                    libsql::params![id, status_str, now, reason],
                )
                .await
            }
            AccountStatus::Locked => {
                conn.execute(
                    "UPDATE accounts SET status = ?2, locked_at = ?3, locked_reason = ?4, updated_at = ?3 WHERE id = ?1",
                    libsql::params![id, status_str, now, reason],
                )
                .await
            }
            AccountStatus::Active => {
                conn.execute(
                    "UPDATE accounts SET status = ?2, locked_at = NULL, locked_reason = NULL, disabled_at = NULL, disabled_reason = NULL, updated_at = ?3 WHERE id = ?1",
                    libsql::params![id, status_str, now],
                )
                .await
            }
            _ => {
                conn.execute(
                    "UPDATE accounts SET status = ?2, updated_at = ?3 WHERE id = ?1",
                    libsql::params![id, status_str, now],
                )
                .await
            }
        }
        .map_err(|e| Error::Internal(format!("Failed to update status: {}", e)))?;

        Ok(())
    }

    async fn list(
        &self,
        status_filter: Option<AccountStatus>,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<Account>, Error> {
        let conn = self.conn()?;
        let mut accounts = Vec::new();

        let mut rows = if let Some(status) = status_filter {
            conn.query(
                "SELECT * FROM accounts WHERE status = ?1 ORDER BY created_at DESC LIMIT ?2 OFFSET ?3",
                libsql::params![status.to_string(), limit as i64, offset as i64],
            )
            .await
        } else {
            conn.query(
                "SELECT * FROM accounts ORDER BY created_at DESC LIMIT ?1 OFFSET ?2",
                libsql::params![limit as i64, offset as i64],
            )
            .await
        }
        .map_err(|e| Error::Internal(format!("Failed to list accounts: {}", e)))?;

        while let Ok(Some(row)) = rows.next().await {
            accounts.push(row_to_account(&row)?);
        }

        Ok(accounts)
    }

    async fn count(&self, status_filter: Option<AccountStatus>) -> Result<u64, Error> {
        let conn = self.conn()?;

        let mut rows = if let Some(status) = status_filter {
            conn.query(
                "SELECT COUNT(*) FROM accounts WHERE status = ?1",
                libsql::params![status.to_string()],
            )
            .await
        } else {
            conn.query("SELECT COUNT(*) FROM accounts", ()).await
        }
        .map_err(|e| Error::Internal(format!("Failed to count accounts: {}", e)))?;

        match rows.next().await {
            Ok(Some(row)) => {
                let count: i64 = row
                    .get(0)
                    .map_err(|e| Error::Internal(format!("Failed to read count: {}", e)))?;
                Ok(count as u64)
            }
            _ => Ok(0),
        }
    }

    async fn delete(&self, id: &str) -> Result<bool, Error> {
        let conn = self.conn()?;
        let affected = conn
            .execute("DELETE FROM accounts WHERE id = ?1", libsql::params![id])
            .await
            .map_err(|e| Error::Internal(format!("Failed to delete account: {}", e)))?;

        Ok(affected > 0)
    }

    async fn record_login(&self, id: &str) -> Result<(), Error> {
        let conn = self.conn()?;
        let now = Utc::now().to_rfc3339();

        conn.execute(
            "UPDATE accounts SET last_login_at = ?2, failed_login_count = 0, updated_at = ?2 WHERE id = ?1",
            libsql::params![id, now],
        )
        .await
        .map_err(|e| Error::Internal(format!("Failed to record login: {}", e)))?;

        Ok(())
    }

    async fn find_expired(&self, limit: usize) -> Result<Vec<Account>, Error> {
        let conn = self.conn()?;
        let now = Utc::now().to_rfc3339();
        let mut accounts = Vec::new();

        let mut rows = conn
            .query(
                "SELECT * FROM accounts WHERE expires_at IS NOT NULL AND expires_at < ?1 AND status != 'expired' ORDER BY expires_at ASC LIMIT ?2",
                libsql::params![now, limit as i64],
            )
            .await
            .map_err(|e| Error::Internal(format!("Failed to find expired: {}", e)))?;

        while let Ok(Some(row)) = rows.next().await {
            accounts.push(row_to_account(&row)?);
        }

        Ok(accounts)
    }

    async fn find_inactive(
        &self,
        cutoff: DateTime<Utc>,
        limit: usize,
    ) -> Result<Vec<Account>, Error> {
        let conn = self.conn()?;
        let cutoff_str = cutoff.to_rfc3339();
        let mut accounts = Vec::new();

        let mut rows = conn
            .query(
                "SELECT * FROM accounts WHERE status = 'active' AND (last_login_at IS NULL OR last_login_at < ?1) ORDER BY last_login_at ASC LIMIT ?2",
                libsql::params![cutoff_str, limit as i64],
            )
            .await
            .map_err(|e| Error::Internal(format!("Failed to find inactive: {}", e)))?;

        while let Ok(Some(row)) = rows.next().await {
            accounts.push(row_to_account(&row)?);
        }

        Ok(accounts)
    }
}