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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Storage adapters for role-system integration
//!
//! This module provides storage adapters that integrate role-system with
//! AuthFramework's existing storage infrastructure.

use async_trait::async_trait;
// use role_system::{
//     AuditEntry, Permission, Role, RoleAssignment, RoleStorage,
//     storage::{StorageError, StorageResult},
// };
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

/// Database-backed storage adapter for role-system
pub struct DatabaseStorage {
    /// Database connection (would be actual DB connection in real implementation)
    connection: Arc<dyn DatabaseConnection>,
    /// In-memory cache for frequently accessed data
    role_cache: Arc<RwLock<HashMap<String, Role>>>,
    permission_cache: Arc<RwLock<HashMap<String, Permission>>>,
    /// Cache TTL in seconds
    cache_ttl: u64,
}

/// Database connection trait (abstraction over actual database)
#[async_trait]
pub trait DatabaseConnection: Send + Sync {
    async fn execute_query(
        &self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<QueryResult, DatabaseError>;
    async fn fetch_one(
        &self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<Row, DatabaseError>;
    async fn fetch_all(
        &self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<Vec<Row>, DatabaseError>;
    async fn begin_transaction(&self) -> Result<Transaction, DatabaseError>;
}

/// Database value trait for query parameters
pub trait DatabaseValue: Send + Sync {
    fn as_str(&self) -> Option<&str>;
    fn as_i64(&self) -> Option<i64>;
    fn as_bool(&self) -> Option<bool>;
}

/// Database query result
#[derive(Debug)]
pub struct QueryResult {
    pub rows_affected: u64,
}

/// Database row
#[derive(Debug)]
pub struct Row {
    pub columns: HashMap<String, DatabaseColumnValue>,
}

/// Database column value
#[derive(Debug, Clone)]
pub enum DatabaseColumnValue {
    String(String),
    Integer(i64),
    Boolean(bool),
    Null,
}

/// Database error
#[derive(Debug, thiserror::Error)]
pub enum DatabaseError {
    #[error("Connection error: {0}")]
    Connection(String),
    #[error("Query error: {0}")]
    Query(String),
    #[error("Serialization error: {0}")]
    Serialization(String),
}

/// Database transaction
#[async_trait]
pub trait Transaction: Send {
    async fn commit(self: Box<Self>) -> Result<(), DatabaseError>;
    async fn rollback(self: Box<Self>) -> Result<(), DatabaseError>;
    async fn execute_query(
        &mut self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<QueryResult, DatabaseError>;
    async fn fetch_one(
        &mut self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<Row, DatabaseError>;
    async fn fetch_all(
        &mut self,
        query: &str,
        params: &[&dyn DatabaseValue],
    ) -> Result<Vec<Row>, DatabaseError>;
}

impl DatabaseStorage {
    /// Create new database storage adapter
    pub fn new(connection: Arc<dyn DatabaseConnection>) -> Self {
        Self {
            connection,
            role_cache: Arc::new(RwLock::new(HashMap::new())),
            permission_cache: Arc::new(RwLock::new(HashMap::new())),
            cache_ttl: 300, // 5 minutes
        }
    }

    /// Set cache TTL
    pub fn with_cache_ttl(mut self, ttl_seconds: u64) -> Self {
        self.cache_ttl = ttl_seconds;
        self
    }

    /// Initialize database schema
    pub async fn initialize_schema(&self) -> Result<(), DatabaseError> {
        // Create roles table
        self.connection
            .execute_query(
                r#"
            CREATE TABLE IF NOT EXISTS roles (
                id VARCHAR(255) PRIMARY KEY,
                name VARCHAR(255) NOT NULL UNIQUE,
                description TEXT,
                parent_id VARCHAR(255),
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (parent_id) REFERENCES roles(id) ON DELETE SET NULL
            )
            "#,
                &[],
            )
            .await?;

        // Create permissions table
        self.connection
            .execute_query(
                r#"
            CREATE TABLE IF NOT EXISTS permissions (
                id VARCHAR(255) PRIMARY KEY,
                action VARCHAR(255) NOT NULL,
                resource VARCHAR(255) NOT NULL,
                conditions TEXT, -- JSON
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                UNIQUE(action, resource)
            )
            "#,
                &[],
            )
            .await?;

        // Create role_permissions table
        self.connection
            .execute_query(
                r#"
            CREATE TABLE IF NOT EXISTS role_permissions (
                role_id VARCHAR(255),
                permission_id VARCHAR(255),
                granted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                granted_by VARCHAR(255),
                PRIMARY KEY (role_id, permission_id),
                FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE,
                FOREIGN KEY (permission_id) REFERENCES permissions(id) ON DELETE CASCADE
            )
            "#,
                &[],
            )
            .await?;

        // Create user_roles table
        self.connection
            .execute_query(
                r#"
            CREATE TABLE IF NOT EXISTS user_roles (
                user_id VARCHAR(255),
                role_id VARCHAR(255),
                assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                assigned_by VARCHAR(255),
                expires_at TIMESTAMP NULL,
                PRIMARY KEY (user_id, role_id),
                FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE CASCADE
            )
            "#,
                &[],
            )
            .await?;

        // Create audit_log table
        self.connection
            .execute_query(
                r#"
            CREATE TABLE IF NOT EXISTS audit_log (
                id BIGINT PRIMARY KEY AUTO_INCREMENT,
                user_id VARCHAR(255),
                action VARCHAR(255) NOT NULL,
                resource VARCHAR(255),
                result VARCHAR(50) NOT NULL,
                context TEXT, -- JSON
                timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                INDEX idx_user_timestamp (user_id, timestamp),
                INDEX idx_action_timestamp (action, timestamp)
            )
            "#,
                &[],
            )
            .await?;

        info!("Database schema initialized successfully");
        Ok(())
    }

    /// Clear caches
    async fn clear_caches(&self) {
        let mut role_cache = self.role_cache.write().await;
        let mut permission_cache = self.permission_cache.write().await;
        role_cache.clear();
        permission_cache.clear();
        debug!("Cleared authorization caches");
    }
}

#[async_trait]
impl RoleStorage for DatabaseStorage {
    async fn create_role(&self, role: &Role) -> StorageResult<()> {
        let role_json =
            serde_json::to_string(role).map_err(|e| StorageError::Serialization(e.to_string()))?;

        let parent_id = role.parent.as_deref();

        self.connection
            .execute_query(
                "INSERT INTO roles (id, name, description, parent_id) VALUES (?, ?, ?, ?)",
                &[
                    &role.id,
                    &role.name,
                    &role.description.as_deref().unwrap_or(""),
                    &parent_id,
                ],
            )
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        // Clear cache to ensure consistency
        self.clear_caches().await;

        info!("Created role: {}", role.name);
        Ok(())
    }

    async fn update_role(&self, role: &Role) -> StorageResult<()> {
        let parent_id = role.parent.as_deref();

        self.connection.execute_query(
            "UPDATE roles SET name = ?, description = ?, parent_id = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?",
            &[&role.name, &role.description.as_deref().unwrap_or(""), &parent_id, &role.id],
        ).await.map_err(|e| StorageError::Database(e.to_string()))?;

        // Update cache
        let mut cache = self.role_cache.write().await;
        cache.insert(role.id.clone(), role.clone());

        info!("Updated role: {}", role.name);
        Ok(())
    }

    async fn delete_role(&self, role_id: &str) -> StorageResult<()> {
        self.connection
            .execute_query("DELETE FROM roles WHERE id = ?", &[&role_id])
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        // Remove from cache
        let mut cache = self.role_cache.write().await;
        cache.remove(role_id);

        info!("Deleted role: {}", role_id);
        Ok(())
    }

    async fn get_role(&self, role_id: &str) -> StorageResult<Option<Role>> {
        // Check cache first
        {
            let cache = self.role_cache.read().await;
            if let Some(role) = cache.get(role_id) {
                debug!("Role cache hit: {}", role_id);
                return Ok(Some(role.clone()));
            }
        }

        // Fetch from database
        let row = match self
            .connection
            .fetch_one(
                "SELECT id, name, description, parent_id FROM roles WHERE id = ?",
                &[&role_id],
            )
            .await
        {
            Ok(row) => row,
            Err(DatabaseError::Query(_)) => return Ok(None), // Not found
            Err(e) => return Err(StorageError::Database(e.to_string())),
        };

        let role = self.row_to_role(row)?;

        // Update cache
        {
            let mut cache = self.role_cache.write().await;
            cache.insert(role_id.to_string(), role.clone());
        }

        Ok(Some(role))
    }

    async fn list_roles(&self) -> StorageResult<Vec<Role>> {
        let rows = self
            .connection
            .fetch_all(
                "SELECT id, name, description, parent_id FROM roles ORDER BY name",
                &[],
            )
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        let mut roles = Vec::new();
        for row in rows {
            roles.push(self.row_to_role(row)?);
        }

        debug!("Listed {} roles", roles.len());
        Ok(roles)
    }

    async fn create_permission(&self, permission: &Permission) -> StorageResult<()> {
        let conditions_json = permission
            .conditions
            .as_ref()
            .map(|c| serde_json::to_string(c))
            .transpose()
            .map_err(|e| StorageError::Serialization(e.to_string()))?;

        self.connection
            .execute_query(
                "INSERT INTO permissions (id, action, resource, conditions) VALUES (?, ?, ?, ?)",
                &[
                    &permission.id,
                    &permission.action,
                    &permission.resource,
                    &conditions_json.as_deref(),
                ],
            )
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        // Clear cache
        self.clear_caches().await;

        info!(
            "Created permission: {}:{}",
            permission.action, permission.resource
        );
        Ok(())
    }

    async fn get_permission(&self, permission_id: &str) -> StorageResult<Option<Permission>> {
        // Check cache first
        {
            let cache = self.permission_cache.read().await;
            if let Some(permission) = cache.get(permission_id) {
                debug!("Permission cache hit: {}", permission_id);
                return Ok(Some(permission.clone()));
            }
        }

        // Fetch from database
        let row = match self
            .connection
            .fetch_one(
                "SELECT id, action, resource, conditions FROM permissions WHERE id = ?",
                &[&permission_id],
            )
            .await
        {
            Ok(row) => row,
            Err(DatabaseError::Query(_)) => return Ok(None),
            Err(e) => return Err(StorageError::Database(e.to_string())),
        };

        let permission = self.row_to_permission(row)?;

        // Update cache
        {
            let mut cache = self.permission_cache.write().await;
            cache.insert(permission_id.to_string(), permission.clone());
        }

        Ok(Some(permission))
    }

    async fn assign_role(&self, assignment: &RoleAssignment) -> StorageResult<()> {
        self.connection.execute_query(
            "INSERT OR REPLACE INTO user_roles (user_id, role_id, assigned_by, expires_at) VALUES (?, ?, ?, ?)",
            &[&assignment.user_id, &assignment.role_id, &assignment.assigned_by.as_deref(), &assignment.expires_at.map(|t| t.timestamp())],
        ).await.map_err(|e| StorageError::Database(e.to_string()))?;

        info!(
            "Assigned role {} to user {}",
            assignment.role_id, assignment.user_id
        );
        Ok(())
    }

    async fn revoke_role(&self, user_id: &str, role_id: &str) -> StorageResult<()> {
        self.connection
            .execute_query(
                "DELETE FROM user_roles WHERE user_id = ? AND role_id = ?",
                &[&user_id, &role_id],
            )
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        info!("Revoked role {} from user {}", role_id, user_id);
        Ok(())
    }

    async fn get_user_roles(&self, user_id: &str) -> StorageResult<Vec<String>> {
        let rows = self.connection.fetch_all(
            "SELECT role_id FROM user_roles WHERE user_id = ? AND (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)",
            &[&user_id],
        ).await.map_err(|e| StorageError::Database(e.to_string()))?;

        let mut role_ids = Vec::new();
        for row in rows {
            if let Some(DatabaseColumnValue::String(role_id)) = row.columns.get("role_id") {
                role_ids.push(role_id.clone());
            }
        }

        debug!("User {} has {} roles", user_id, role_ids.len());
        Ok(role_ids)
    }

    async fn get_role_permissions(&self, role_id: &str) -> StorageResult<Vec<String>> {
        let rows = self
            .connection
            .fetch_all(
                "SELECT permission_id FROM role_permissions WHERE role_id = ?",
                &[&role_id],
            )
            .await
            .map_err(|e| StorageError::Database(e.to_string()))?;

        let mut permission_ids = Vec::new();
        for row in rows {
            if let Some(DatabaseColumnValue::String(permission_id)) =
                row.columns.get("permission_id")
            {
                permission_ids.push(permission_id.clone());
            }
        }

        debug!("Role {} has {} permissions", role_id, permission_ids.len());
        Ok(permission_ids)
    }

    async fn log_audit_entry(&self, entry: &AuditEntry) -> StorageResult<()> {
        let context_json = serde_json::to_string(&entry.context)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;

        self.connection.execute_query(
            "INSERT INTO audit_log (user_id, action, resource, result, context) VALUES (?, ?, ?, ?, ?)",
            &[&entry.user_id.as_deref(), &entry.action, &entry.resource.as_deref(), &entry.result, &context_json],
        ).await.map_err(|e| StorageError::Database(e.to_string()))?;

        debug!(
            "Logged audit entry for user {:?}: {} on {:?}",
            entry.user_id, entry.action, entry.resource
        );
        Ok(())
    }
}

impl DatabaseStorage {
    /// Convert database row to Role
    fn row_to_role(&self, row: Row) -> StorageResult<Role> {
        let id = self.get_string_column(&row, "id")?;
        let name = self.get_string_column(&row, "name")?;
        let description = self.get_optional_string_column(&row, "description");
        let parent = self.get_optional_string_column(&row, "parent_id");

        Ok(Role {
            id,
            name,
            description,
            parent,
            permissions: Vec::new(), // Will be loaded separately if needed
        })
    }

    /// Convert database row to Permission
    fn row_to_permission(&self, row: Row) -> StorageResult<Permission> {
        let id = self.get_string_column(&row, "id")?;
        let action = self.get_string_column(&row, "action")?;
        let resource = self.get_string_column(&row, "resource")?;

        let conditions =
            if let Some(conditions_str) = self.get_optional_string_column(&row, "conditions") {
                Some(
                    serde_json::from_str(&conditions_str)
                        .map_err(|e| StorageError::Serialization(e.to_string()))?,
                )
            } else {
                None
            };

        Ok(Permission {
            id,
            action,
            resource,
            conditions,
        })
    }

    /// Get string column from row
    fn get_string_column(&self, row: &Row, column: &str) -> StorageResult<String> {
        match row.columns.get(column) {
            Some(DatabaseColumnValue::String(value)) => Ok(value.clone()),
            Some(DatabaseColumnValue::Null) => {
                Err(StorageError::Database(format!("Column {} is null", column)))
            }
            Some(_) => Err(StorageError::Database(format!(
                "Column {} is not a string",
                column
            ))),
            None => Err(StorageError::Database(format!(
                "Column {} not found",
                column
            ))),
        }
    }

    /// Get optional string column from row
    fn get_optional_string_column(&self, row: &Row, column: &str) -> Option<String> {
        match row.columns.get(column) {
            Some(DatabaseColumnValue::String(value)) => Some(value.clone()),
            _ => None,
        }
    }
}

/// In-memory storage adapter for testing and development
pub struct MemoryStorage {
    roles: Arc<RwLock<HashMap<String, Role>>>,
    permissions: Arc<RwLock<HashMap<String, Permission>>>,
    user_roles: Arc<RwLock<HashMap<String, Vec<String>>>>,
    role_permissions: Arc<RwLock<HashMap<String, Vec<String>>>>,
    audit_log: Arc<RwLock<Vec<AuditEntry>>>,
}

impl Default for MemoryStorage {
    fn default() -> Self {
        Self::new()
    }
}

impl MemoryStorage {
    /// Create new memory storage
    pub fn new() -> Self {
        Self {
            roles: Arc::new(RwLock::new(HashMap::new())),
            permissions: Arc::new(RwLock::new(HashMap::new())),
            user_roles: Arc::new(RwLock::new(HashMap::new())),
            role_permissions: Arc::new(RwLock::new(HashMap::new())),
            audit_log: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Clear all data (useful for testing)
    pub async fn clear(&self) {
        let mut roles = self.roles.write().await;
        let mut permissions = self.permissions.write().await;
        let mut user_roles = self.user_roles.write().await;
        let mut role_permissions = self.role_permissions.write().await;
        let mut audit_log = self.audit_log.write().await;

        roles.clear();
        permissions.clear();
        user_roles.clear();
        role_permissions.clear();
        audit_log.clear();
    }
}

#[async_trait]
impl RoleStorage for MemoryStorage {
    async fn create_role(&self, role: &Role) -> StorageResult<()> {
        let mut roles = self.roles.write().await;
        roles.insert(role.id.clone(), role.clone());
        info!("Created role in memory: {}", role.name);
        Ok(())
    }

    async fn update_role(&self, role: &Role) -> StorageResult<()> {
        let mut roles = self.roles.write().await;
        roles.insert(role.id.clone(), role.clone());
        info!("Updated role in memory: {}", role.name);
        Ok(())
    }

    async fn delete_role(&self, role_id: &str) -> StorageResult<()> {
        let mut roles = self.roles.write().await;
        roles.remove(role_id);
        info!("Deleted role from memory: {}", role_id);
        Ok(())
    }

    async fn get_role(&self, role_id: &str) -> StorageResult<Option<Role>> {
        let roles = self.roles.read().await;
        Ok(roles.get(role_id).cloned())
    }

    async fn list_roles(&self) -> StorageResult<Vec<Role>> {
        let roles = self.roles.read().await;
        Ok(roles.values().cloned().collect())
    }

    async fn create_permission(&self, permission: &Permission) -> StorageResult<()> {
        let mut permissions = self.permissions.write().await;
        permissions.insert(permission.id.clone(), permission.clone());
        info!(
            "Created permission in memory: {}:{}",
            permission.action, permission.resource
        );
        Ok(())
    }

    async fn get_permission(&self, permission_id: &str) -> StorageResult<Option<Permission>> {
        let permissions = self.permissions.read().await;
        Ok(permissions.get(permission_id).cloned())
    }

    async fn assign_role(&self, assignment: &RoleAssignment) -> StorageResult<()> {
        let mut user_roles = self.user_roles.write().await;
        user_roles
            .entry(assignment.user_id.clone())
            .or_default()
            .push(assignment.role_id.clone());
        info!(
            "Assigned role in memory: {} to {}",
            assignment.role_id, assignment.user_id
        );
        Ok(())
    }

    async fn revoke_role(&self, user_id: &str, role_id: &str) -> StorageResult<()> {
        let mut user_roles = self.user_roles.write().await;
        if let Some(roles) = user_roles.get_mut(user_id) {
            roles.retain(|r| r != role_id);
        }
        info!("Revoked role from memory: {} from {}", role_id, user_id);
        Ok(())
    }

    async fn get_user_roles(&self, user_id: &str) -> StorageResult<Vec<String>> {
        let user_roles = self.user_roles.read().await;
        Ok(user_roles.get(user_id).cloned().unwrap_or_default())
    }

    async fn get_role_permissions(&self, role_id: &str) -> StorageResult<Vec<String>> {
        let role_permissions = self.role_permissions.read().await;
        Ok(role_permissions.get(role_id).cloned().unwrap_or_default())
    }

    async fn log_audit_entry(&self, entry: &AuditEntry) -> StorageResult<()> {
        let mut audit_log = self.audit_log.write().await;
        audit_log.push(entry.clone());
        debug!(
            "Logged audit entry in memory for user {:?}: {} on {:?}",
            entry.user_id, entry.action, entry.resource
        );
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use role_system::{Permission, Role};

    #[tokio::test]
    async fn test_memory_storage_basic_operations() {
        let storage = MemoryStorage::new();

        // Test role creation
        let role = Role {
            id: "test_role".to_string(),
            name: "Test Role".to_string(),
            description: Some("A test role".to_string()),
            parent: None,
            permissions: Vec::new(),
        };

        storage.create_role(&role).await.unwrap();

        // Test role retrieval
        let retrieved = storage.get_role("test_role").await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().name, "Test Role");

        // Test role listing
        let roles = storage.list_roles().await.unwrap();
        assert_eq!(roles.len(), 1);

        // Test role deletion
        storage.delete_role("test_role").await.unwrap();
        let retrieved = storage.get_role("test_role").await.unwrap();
        assert!(retrieved.is_none());
    }

    #[tokio::test]
    async fn test_memory_storage_permissions() {
        let storage = MemoryStorage::new();

        let permission = Permission {
            id: "test_perm".to_string(),
            action: "read".to_string(),
            resource: "users".to_string(),
            conditions: None,
        };

        storage.create_permission(&permission).await.unwrap();

        let retrieved = storage.get_permission("test_perm").await.unwrap();
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().action, "read");
    }
}