auth-framework 0.4.2

A comprehensive, production-ready authentication and authorization framework for Rust applications
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use crate::errors::Result;
use crate::storage::{AuthStorage, SessionData};
use crate::tokens::AuthToken;
use async_trait::async_trait;
/// MySQL storage backend implementation for auth-framework.
#[cfg(feature = "mysql-storage")]
use sqlx::MySqlPool;

/// MySQL storage backend
#[cfg(feature = "mysql-storage")]
pub struct MySqlStorage {
    pool: MySqlPool,
}

#[cfg(feature = "mysql-storage")]
impl MySqlStorage {
    /// Create a new MySQL storage instance
    pub fn new(pool: MySqlPool) -> Self {
        Self { pool }
    }
}

#[cfg(feature = "mysql-storage")]
#[async_trait]
impl AuthStorage for MySqlStorage {
    async fn store_token(&self, token: &AuthToken) -> Result<()> {
        sqlx::query(
            r#"
            INSERT INTO auth_tokens (
                token_id, user_id, access_token, refresh_token, token_type,
                expires_at, scopes, issued_at, auth_method, subject, issuer,
                client_id, metadata
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE
                access_token = VALUES(access_token),
                refresh_token = VALUES(refresh_token),
                expires_at = VALUES(expires_at)
            "#,
        )
        .bind(&token.token_id)
        .bind(&token.user_id)
        .bind(&token.access_token)
        .bind(&token.refresh_token)
        .bind(&token.token_type)
        .bind(token.expires_at)
        .bind(serde_json::to_string(&token.scopes).unwrap_or_default())
        .bind(token.issued_at)
        .bind(&token.auth_method)
        .bind(&token.subject)
        .bind(&token.issuer)
        .bind(&token.client_id)
        .bind(serde_json::to_string(&token.metadata).unwrap_or_default())
        .execute(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to store token: {}", e),
            ))
        })?;
        Ok(())
    }

    async fn get_token(&self, token_id: &str) -> Result<Option<AuthToken>> {
        use sqlx::Row;
        let row = sqlx::query(
            r#"
            SELECT token_id, user_id, access_token, refresh_token, token_type,
                   expires_at, scopes, issued_at, auth_method, subject, issuer,
                   client_id, metadata
            FROM auth_tokens WHERE token_id = ?
            "#,
        )
        .bind(token_id)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to fetch token: {}", e),
            ))
        })?;

        if let Some(row) = row {
            let scopes: Vec<String> = row
                .try_get::<String, _>("scopes")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            let metadata = row
                .try_get::<String, _>("metadata")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            Ok(Some(AuthToken {
                token_id: row.try_get("token_id").unwrap_or_default(),
                user_id: row.try_get("user_id").unwrap_or_default(),
                access_token: row.try_get("access_token").unwrap_or_default(),
                refresh_token: row.try_get("refresh_token").ok(),
                token_type: row.try_get("token_type").ok(),
                expires_at: row.try_get("expires_at").unwrap(),
                scopes,
                issued_at: row.try_get("issued_at").unwrap(),
                auth_method: row.try_get("auth_method").unwrap_or_default(),
                subject: row.try_get("subject").ok(),
                issuer: row.try_get("issuer").ok(),
                client_id: row.try_get("client_id").ok(),
                user_profile: None,
                permissions: Vec::new(), // Note: Requires user_permissions table and additional query
                roles: Vec::new(),       // Note: Requires user_roles table and additional query
                metadata,
            }))
        } else {
            Ok(None)
        }
    }

    async fn get_token_by_access_token(&self, access_token: &str) -> Result<Option<AuthToken>> {
        use sqlx::Row;
        let row = sqlx::query(
            r#"
            SELECT token_id, user_id, access_token, refresh_token, token_type,
                   expires_at, scopes, issued_at, auth_method, subject, issuer,
                   client_id, metadata
            FROM auth_tokens WHERE access_token = ?
            "#,
        )
        .bind(access_token)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to fetch token: {}", e),
            ))
        })?;

        if let Some(row) = row {
            let scopes: Vec<String> = row
                .try_get::<String, _>("scopes")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            let metadata = row
                .try_get::<String, _>("metadata")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            Ok(Some(AuthToken {
                token_id: row.try_get("token_id").unwrap_or_default(),
                user_id: row.try_get("user_id").unwrap_or_default(),
                access_token: row.try_get("access_token").unwrap_or_default(),
                refresh_token: row.try_get("refresh_token").ok(),
                token_type: row.try_get("token_type").ok(),
                expires_at: row.try_get("expires_at").unwrap(),
                scopes,
                issued_at: row.try_get("issued_at").unwrap(),
                auth_method: row.try_get("auth_method").unwrap_or_default(),
                subject: row.try_get("subject").ok(),
                issuer: row.try_get("issuer").ok(),
                client_id: row.try_get("client_id").ok(),
                user_profile: None,
                permissions: Vec::new(), // Note: Requires user_permissions table and additional query
                roles: Vec::new(),       // Note: Requires user_roles table and additional query
                metadata,
            }))
        } else {
            Ok(None)
        }
    }

    async fn update_token(&self, _token: &AuthToken) -> Result<()> {
        sqlx::query(
            r#"
            UPDATE auth_tokens SET
                access_token = ?,
                refresh_token = ?,
                token_type = ?,
                expires_at = ?,
                scopes = ?,
                issued_at = ?,
                auth_method = ?,
                subject = ?,
                issuer = ?,
                client_id = ?,
                metadata = ?
            WHERE token_id = ?
            "#,
        )
        .bind(&_token.access_token)
        .bind(&_token.refresh_token)
        .bind(&_token.token_type)
        .bind(_token.expires_at)
        .bind(serde_json::to_string(&_token.scopes).unwrap_or_default())
        .bind(_token.issued_at)
        .bind(&_token.auth_method)
        .bind(&_token.subject)
        .bind(&_token.issuer)
        .bind(&_token.client_id)
        .bind(serde_json::to_string(&_token.metadata).unwrap_or_default())
        .bind(&_token.token_id)
        .execute(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to update token: {}", e),
            ))
        })?;
        Ok(())
    }
    async fn delete_token(&self, token_id: &str) -> Result<()> {
        sqlx::query("DELETE FROM auth_tokens WHERE token_id = ?")
            .bind(token_id)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to delete token: {}", e),
                ))
            })?;
        Ok(())
    }
    async fn list_user_tokens(&self, _user_id: &str) -> Result<Vec<AuthToken>> {
        use sqlx::Row;
        let rows = sqlx::query(
            r#"
            SELECT token_id, user_id, access_token, refresh_token, token_type,
                   expires_at, scopes, issued_at, auth_method, subject, issuer,
                   client_id, metadata
            FROM auth_tokens WHERE user_id = ?
            "#,
        )
        .bind(_user_id)
        .fetch_all(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to list user tokens: {}", e),
            ))
        })?;

        let mut tokens = Vec::new();
        for row in rows {
            let scopes: Vec<String> = row
                .try_get::<String, _>("scopes")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            let metadata = row
                .try_get::<String, _>("metadata")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            tokens.push(AuthToken {
                token_id: row.try_get("token_id").unwrap_or_default(),
                user_id: row.try_get("user_id").unwrap_or_default(),
                access_token: row.try_get("access_token").unwrap_or_default(),
                refresh_token: row.try_get("refresh_token").ok(),
                token_type: row.try_get("token_type").ok(),
                expires_at: row.try_get("expires_at").unwrap(),
                scopes,
                issued_at: row.try_get("issued_at").unwrap(),
                auth_method: row.try_get("auth_method").unwrap_or_default(),
                subject: row.try_get("subject").ok(),
                issuer: row.try_get("issuer").ok(),
                client_id: row.try_get("client_id").ok(),
                user_profile: None,
                permissions: Vec::new(), // Note: Requires user_permissions table and additional query
                roles: Vec::new(),       // Note: Requires user_roles table and additional query
                metadata,
            });
        }
        Ok(tokens)
    }
    async fn store_session(
        &self,
        session_id: &str,
        data: &crate::storage::SessionData,
    ) -> Result<()> {
        // Store session in DB
        sqlx::query(
            r#"
            INSERT INTO sessions (session_id, user_id, created_at, expires_at, data)
            VALUES (?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE data = VALUES(data), expires_at = VALUES(expires_at)
            "#,
        )
        .bind(session_id)
        .bind(&data.user_id)
        .bind(data.created_at)
        .bind(data.expires_at)
        .bind(serde_json::to_string(&data.data).unwrap_or_default())
        .execute(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to store session: {}", e),
            ))
        })?;
        Ok(())
    }
    async fn get_session(&self, _session_id: &str) -> Result<Option<crate::storage::SessionData>> {
        use sqlx::Row;
        let row = sqlx::query(
            r#"
            SELECT session_id, user_id, created_at, expires_at, data
            FROM sessions WHERE session_id = ?
            "#,
        )
        .bind(_session_id)
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to get session: {}", e),
            ))
        })?;
        if let Some(row) = row {
            let data = row
                .try_get::<String, _>("data")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();
            Ok(Some(crate::storage::SessionData {
                session_id: row.try_get("session_id").unwrap_or_default(),
                user_id: row.try_get("user_id").unwrap_or_default(),
                created_at: row.try_get("created_at").unwrap(),
                expires_at: row.try_get("expires_at").unwrap(),
                last_activity: row
                    .try_get("last_activity")
                    .unwrap_or_else(|_| row.try_get("created_at").unwrap()),
                ip_address: row.try_get("ip_address").ok(),
                user_agent: row.try_get("user_agent").ok(),
                data,
            }))
        } else {
            Ok(None)
        }
    }
    async fn delete_session(&self, _session_id: &str) -> Result<()> {
        sqlx::query("DELETE FROM sessions WHERE session_id = ?")
            .bind(_session_id)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to delete session: {}", e),
                ))
            })?;
        Ok(())
    }
    async fn store_kv(
        &self,
        key: &str,
        value: &[u8],
        _ttl: Option<std::time::Duration>,
    ) -> Result<()> {
        sqlx::query(
            r#"
            INSERT INTO kv_store (`key`, `value`)
            VALUES (?, ?)
            ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)
            "#,
        )
        .bind(key)
        .bind(value)
        .execute(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to store kv: {}", e),
            ))
        })?;
        Ok(())
    }
    async fn get_kv(&self, _key: &str) -> Result<Option<Vec<u8>>> {
        use sqlx::Row;
        let row = sqlx::query("SELECT `value` FROM kv_store WHERE `key` = ?")
            .bind(_key)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to get kv: {}", e),
                ))
            })?;
        Ok(row.and_then(|r| r.try_get("value").ok()))
    }
    async fn delete_kv(&self, _key: &str) -> Result<()> {
        sqlx::query("DELETE FROM kv_store WHERE `key` = ?")
            .bind(_key)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to delete kv: {}", e),
                ))
            })?;
        Ok(())
    }
    async fn cleanup_expired(&self) -> Result<()> {
        let now = chrono::Utc::now().naive_utc();
        let now_str = now.format("%Y-%m-%d %H:%M:%S").to_string();
        // Clean up expired tokens
        sqlx::query("DELETE FROM auth_tokens WHERE expires_at < ?")
            .bind(&now_str)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to cleanup expired tokens: {}", e),
                ))
            })?;
        // Clean up expired sessions
        sqlx::query("DELETE FROM sessions WHERE expires_at < ?")
            .bind(&now_str)
            .execute(&self.pool)
            .await
            .map_err(|e| {
                crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                    format!("Failed to cleanup expired sessions: {}", e),
                ))
            })?;
        Ok(())
    }

    async fn list_user_sessions(&self, user_id: &str) -> Result<Vec<SessionData>> {
        use sqlx::Row;
        let rows = sqlx::query(
            r#"
            SELECT session_id, user_id, data, expires_at, created_at, last_accessed
            FROM sessions
            WHERE user_id = ? AND (expires_at IS NULL OR expires_at > NOW())
            "#,
        )
        .bind(user_id)
        .fetch_all(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to list user sessions: {}", e),
            ))
        })?;

        let mut sessions = Vec::new();
        for row in rows {
            let data: std::collections::HashMap<String, serde_json::Value> = row
                .try_get::<String, _>("data")
                .ok()
                .and_then(|s| serde_json::from_str(&s).ok())
                .unwrap_or_default();

            sessions.push(SessionData {
                session_id: row.try_get("session_id").unwrap_or_default(),
                user_id: row.try_get("user_id").unwrap_or_default(),
                data,
                expires_at: row
                    .try_get("expires_at")
                    .unwrap_or_else(|_| chrono::Utc::now()),
                created_at: row.try_get("created_at").unwrap_or_default(),
                last_activity: row.try_get("last_activity").unwrap_or_default(),
                ip_address: row.try_get("ip_address").ok(),
                user_agent: row.try_get("user_agent").ok(),
            });
        }

        Ok(sessions)
    }

    async fn count_active_sessions(&self) -> Result<u64> {
        use sqlx::Row;
        let row = sqlx::query(
            "SELECT COUNT(*) as count FROM sessions WHERE expires_at IS NULL OR expires_at > NOW()",
        )
        .fetch_one(&self.pool)
        .await
        .map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to count active sessions: {}", e),
            ))
        })?;

        let count: i64 = row.try_get("count").map_err(|e| {
            crate::errors::AuthError::Storage(crate::errors::StorageError::operation_failed(
                format!("Failed to parse session count: {}", e),
            ))
        })?;

        Ok(count as u64)
    }
}