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
//! Comprehensive administration module for AuthFramework management.
//!
//! This module provides multiple administrative interfaces for monitoring,
//! configuring, and managing AuthFramework deployments. It includes both
//! interactive and programmatic interfaces suitable for different operational
//! environments.
//!
//! # Administrative Interfaces
//!
//! - **CLI (Command Line Interface)**: Scriptable command-line administration
//! - **TUI (Terminal User Interface)**: Interactive terminal dashboard
//! - **Web Interface**: Browser-based administrative console
//! - **API**: RESTful API for programmatic management
//!
//! # Core Capabilities
//!
//! - **Real-time Monitoring**: Live metrics and health status
//! - **Configuration Management**: Dynamic configuration updates
//! - **User Management**: User account and permission administration
//! - **Security Monitoring**: Threat detection and incident response
//! - **Audit Logging**: Comprehensive activity tracking
//! - **Performance Analytics**: System performance and optimization
//!
//! # Security Features
//!
//! - **Role-based Access**: Admin, operator, and read-only roles
//! - **Audit Trail**: All administrative actions are logged
//! - **Secure Sessions**: Encrypted admin sessions
//! - **MFA Enforcement**: Multi-factor authentication for admins
//! - **IP Whitelisting**: Restrict admin access by network
//!
//! # Monitoring Dashboard
//!
//! The administrative interfaces provide comprehensive monitoring:
//! - Active user sessions
//! - Authentication success/failure rates
//! - Security alerts and incidents
//! - System performance metrics
//! - Error rates and debugging information
//!
//! # Configuration Management
//!
//! - **Live Updates**: Modify configuration without restarts
//! - **Validation**: Real-time configuration validation
//! - **Backup/Restore**: Configuration versioning and rollback
//! - **Environment Management**: Dev, staging, production configs
//!
//! # Example Usage
//!
//! ```rust
//! use auth_framework::admin::{AdminInterface, AppState};
//!
//! // Create administrative interface
//! let app_state = AppState::new(config_manager).await?;
//! let admin = AdminInterface::new(app_state);
//!
//! // Start web interface
//! admin.start_web_interface("127.0.0.1:8080").await?;
//!
//! // Start TUI interface
//! admin.start_tui_interface().await?;
//! ```
//!
//! # Deployment Scenarios
//!
//! - **Development**: TUI for local development and testing
//! - **Production**: Web interface for remote administration
//! - **Automation**: CLI for scripted operations and CI/CD
//! - **Monitoring**: API integration with external monitoring systems
//!
//! # Integration
//!
//! Integrates with external systems:
//! - Prometheus metrics export
//! - Grafana dashboard templates
//! - SIEM system integration
//! - Log aggregation systems

use crate::{config::AuthFrameworkSettings, errors::Result};
use chrono;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Shared application state
#[derive(Debug, Clone)]
pub struct AppState {
    pub config: Arc<RwLock<AuthFrameworkSettings>>,
    pub config_manager: crate::config::ConfigManager,
    pub health_status: HealthStatus,
    pub server_status: Arc<RwLock<ServerStatus>>,
}

/// Server status information
#[derive(Debug, Clone)]
pub struct ServerStatus {
    pub web_server_running: bool,
    pub web_server_port: Option<u16>,
    pub last_config_update: Option<chrono::DateTime<chrono::Utc>>,
    pub active_sessions: u32,
    pub health_status: HealthStatus,
}

/// System health status
#[derive(Debug, Clone)]
pub enum HealthStatus {
    Healthy,
    Warning(String),
    Critical(String),
}

/// Server information for TUI display
#[derive(Debug, Clone)]
pub struct ServerInfo {
    pub version: String,
    pub uptime: String,
    pub status: String,
    pub port: Option<u16>,
    pub active_sessions: u32,
}

/// User statistics for TUI display
#[derive(Debug, Clone)]
pub struct UserStatistics {
    pub total_users: u32,
    pub active_sessions: u32,
    pub failed_logins_today: u32,
    pub new_registrations_today: u32,
}

/// Security event for TUI display
#[derive(Debug, Clone)]
pub struct SecurityEvent {
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub event_type: String,
    pub description: String,
    pub ip_address: Option<String>,
    pub user_id: Option<String>,
}

impl AppState {
    pub fn new(settings: AuthFrameworkSettings) -> Result<Self> {
        let config = Arc::new(RwLock::new(settings));
        let config_manager = crate::config::ConfigManager::new()?;

        let server_status = ServerStatus {
            web_server_running: false,
            web_server_port: None,
            last_config_update: None,
            active_sessions: 0,
            health_status: HealthStatus::Healthy,
        };

        Ok(Self {
            config,
            config_manager,
            health_status: HealthStatus::Healthy,
            server_status: Arc::new(RwLock::new(server_status)),
        })
    }

    pub async fn get_health_status(&self) -> HealthStatus {
        // Simplified health check - in a real implementation this would
        // check database connections, external services, etc.
        HealthStatus::Healthy
    }

    pub async fn reload_config(&self) -> Result<()> {
        // Reload configuration logic here
        // For now, just update the timestamp
        let mut status = self.server_status.write().await;
        status.last_config_update = Some(chrono::Utc::now());
        Ok(())
    }

    pub async fn update_server_status(&self, running: bool, port: Option<u16>) {
        let mut status = self.server_status.write().await;
        status.web_server_running = running;
        status.web_server_port = port;
    }

    /// Get server information for display in TUI
    pub async fn get_server_info(&self) -> Result<ServerInfo> {
        let status = self.server_status.read().await;
        Ok(ServerInfo {
            version: env!("CARGO_PKG_VERSION").to_string(),
            uptime: "N/A".to_string(), // Would be calculated from start time
            status: if status.web_server_running {
                "Running"
            } else {
                "Stopped"
            }
            .to_string(),
            port: status.web_server_port,
            active_sessions: status.active_sessions,
        })
    }

    /// Get user statistics for display in TUI
    pub async fn get_user_statistics(&self) -> Result<UserStatistics> {
        // Mock data - in real implementation would query the database
        Ok(UserStatistics {
            total_users: 0,
            active_sessions: 0,
            failed_logins_today: 0,
            new_registrations_today: 0,
        })
    }

    /// Get recent security events for display in TUI
    pub async fn get_recent_security_events(&self) -> Result<Vec<SecurityEvent>> {
        // Mock data - in real implementation would query the audit log
        Ok(vec![])
    }
} // Command line interface types and functions
#[cfg(feature = "cli")]
pub mod cli;

#[cfg(feature = "tui")]
pub mod tui;

#[cfg(feature = "web-gui")]
pub mod web;

// CLI command types
#[derive(Debug, Clone, clap::Subcommand)]
pub enum CliCommand {
    /// Configuration management commands
    Config {
        #[command(subcommand)]
        action: ConfigAction,
    },
    /// User management commands
    Users {
        #[command(subcommand)]
        action: UserAction,
    },
    /// Server management commands
    Server {
        #[command(subcommand)]
        action: ServerAction,
    },
    /// Security management commands
    Security {
        #[command(subcommand)]
        action: SecurityAction,
    },
    /// Show system status
    Status {
        /// Show detailed information
        #[arg(long)]
        detailed: bool,
        /// Output format (json, yaml, table)
        #[arg(long, default_value = "table")]
        format: String,
    },
}

#[derive(Debug, Clone, clap::Subcommand)]
pub enum ConfigAction {
    /// Show configuration
    Show {
        /// Configuration section to show
        section: Option<String>,
        /// Output format (json, yaml, table)
        #[arg(long, default_value = "table")]
        format: String,
    },
    /// Set configuration value
    Set {
        /// Configuration key
        key: String,
        /// Configuration value
        value: String,
        /// Apply hot reload
        #[arg(long)]
        hot_reload: bool,
    },
    /// Reset configuration to defaults
    Reset,
    /// Validate configuration
    Validate {
        /// Configuration file to validate
        file: Option<String>,
    },
    /// Get configuration value
    Get {
        /// Configuration key
        key: String,
    },
    /// Reload configuration
    Reload {
        /// Show configuration differences
        #[arg(long)]
        show_diff: bool,
    },
    /// Generate configuration template
    Template {
        /// Output file path
        output: Option<String>,
        /// Generate complete template with all options
        #[arg(long)]
        complete: bool,
    },
}

#[derive(Debug, Clone, clap::Subcommand)]
pub enum UserAction {
    /// List users
    List {
        /// Maximum number of users to show
        limit: Option<u32>,
        /// Show only active users
        #[arg(long)]
        active: bool,
    },
    /// Create new user
    Create {
        /// User email address
        email: String,
        /// User password (will prompt if not provided)
        password: Option<String>,
        /// Grant admin privileges
        #[arg(long)]
        admin: bool,
    },
    /// Delete user
    Delete {
        /// User to delete (email or ID)
        user: String,
        /// Force deletion without confirmation
        #[arg(long)]
        force: bool,
    },
    /// Set user role
    SetRole {
        /// User email
        email: String,
        /// Role to assign
        role: String,
    },
    /// Update user properties
    Update {
        /// User to update (email or ID)
        user: String,
        /// New email address
        #[arg(long)]
        email: Option<String>,
        /// Set active status
        #[arg(long)]
        active: Option<bool>,
    },
}

#[derive(Debug, Clone, clap::Subcommand)]
pub enum ServerAction {
    /// Show server status
    Status,
    /// Start the server
    Start {
        /// Port to bind to
        port: Option<u16>,
        /// Run as daemon
        #[arg(long)]
        daemon: bool,
    },
    /// Stop the server
    Stop {
        /// Force stop without graceful shutdown
        #[arg(long)]
        force: bool,
    },
    /// Restart the server
    Restart {
        /// Port to bind to
        port: Option<u16>,
    },
}

#[derive(Debug, Clone, clap::Subcommand)]
pub enum SecurityAction {
    /// Show audit log
    AuditLog,
    /// Generate threat report
    ThreatReport,
    /// Force user logout
    ForceLogout {
        /// User ID to logout
        user_id: String,
    },
    /// Run security audit
    Audit {
        /// Number of days to audit
        #[arg(long, default_value = "7")]
        days: u32,
        /// Show detailed information
        #[arg(long)]
        detailed: bool,
    },
    /// Manage user sessions
    Sessions {
        /// Filter by specific user
        #[arg(long)]
        user: Option<String>,
        /// Terminate specific session
        #[arg(long)]
        terminate: Option<String>,
    },
    /// Threat intelligence operations
    ThreatIntel {
        /// Update threat intelligence database
        #[arg(long)]
        update: bool,
        /// Check specific IP address
        #[arg(long)]
        check_ip: Option<String>,
    },
}