auth-framework 0.5.0-rc19

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
//! Security Features - Rate Limiting, DoS Protection, IP Blacklisting
//!
//! Advanced security features for API protection

use crate::api::{ApiResponse, ApiState};
use axum::{
    Json,
    extract::{ConnectInfo, State},
    http::{Request, StatusCode},
    middleware::Next,
    response::Response,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Rate limiter configuration
#[derive(Debug, Clone)]
pub struct RateLimitConfig {
    /// Maximum requests per window
    pub max_requests: u32,
    /// Time window duration
    pub window_duration: Duration,
    /// Penalty duration for exceeding limit
    pub penalty_duration: Duration,
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            max_requests: 100,
            window_duration: Duration::from_secs(60),
            penalty_duration: Duration::from_secs(300), // 5 minutes
        }
    }
}

/// DoS protection configuration
#[derive(Debug, Clone)]
pub struct DosProtectionConfig {
    /// Maximum request rate (requests per second) before triggering protection
    pub max_rate: f64,
    /// Duration to monitor for DoS attacks
    pub monitor_duration: Duration,
    /// Duration to block suspected DoS attackers
    pub block_duration: Duration,
}

impl Default for DosProtectionConfig {
    fn default() -> Self {
        Self {
            max_rate: 50.0, // 50 requests per second
            monitor_duration: Duration::from_secs(10),
            block_duration: Duration::from_secs(600), // 10 minutes
        }
    }
}

/// IP blacklist configuration
#[derive(Debug, Clone)]
pub struct IpBlacklistConfig {
    /// Duration to keep IPs in blacklist
    pub blacklist_duration: Duration,
    /// Maximum failed attempts before blacklisting
    pub max_failed_attempts: u32,
    /// Time window for counting failed attempts
    pub attempt_window: Duration,
}

impl Default for IpBlacklistConfig {
    fn default() -> Self {
        Self {
            blacklist_duration: Duration::from_secs(3600), // 1 hour
            max_failed_attempts: 10,
            attempt_window: Duration::from_secs(300), // 5 minutes
        }
    }
}

/// Request tracking information
#[derive(Debug, Clone)]
struct RequestInfo {
    count: u32,
    first_request: Instant,
    last_request: Instant,
    penalty_until: Option<Instant>,
}

/// DoS tracking information
#[derive(Debug, Clone)]
struct DosInfo {
    request_times: Vec<Instant>,
    blocked_until: Option<Instant>,
}

/// Failed attempt tracking
#[derive(Debug, Clone)]
struct FailureInfo {
    attempts: u32,
    first_attempt: Instant,
    blacklisted_until: Option<Instant>,
}

/// Security manager for handling rate limiting, DoS protection, and IP blacklisting
pub struct SecurityManager {
    rate_limit_config: RateLimitConfig,
    dos_config: DosProtectionConfig,
    blacklist_config: IpBlacklistConfig,

    // Rate limiting state
    rate_limits: Arc<RwLock<HashMap<IpAddr, RequestInfo>>>,

    // DoS protection state
    dos_tracking: Arc<RwLock<HashMap<IpAddr, DosInfo>>>,

    // IP blacklisting state
    failure_tracking: Arc<RwLock<HashMap<IpAddr, FailureInfo>>>,
    manual_blacklist: Arc<RwLock<Vec<IpAddr>>>,
}

impl SecurityManager {
    /// Create a new security manager with default configuration
    pub fn new() -> Self {
        Self::with_config(
            RateLimitConfig::default(),
            DosProtectionConfig::default(),
            IpBlacklistConfig::default(),
        )
    }

    /// Create a new security manager with custom configuration
    pub fn with_config(
        rate_limit_config: RateLimitConfig,
        dos_config: DosProtectionConfig,
        blacklist_config: IpBlacklistConfig,
    ) -> Self {
        Self {
            rate_limit_config,
            dos_config,
            blacklist_config,
            rate_limits: Arc::new(RwLock::new(HashMap::new())),
            dos_tracking: Arc::new(RwLock::new(HashMap::new())),
            failure_tracking: Arc::new(RwLock::new(HashMap::new())),
            manual_blacklist: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Check if IP is rate limited
    pub async fn check_rate_limit(&self, ip: IpAddr) -> bool {
        let now = Instant::now();
        let mut rate_limits = self.rate_limits.write().await;

        // Clean expired entries
        rate_limits.retain(|_, info| {
            now.duration_since(info.first_request) < self.rate_limit_config.window_duration * 2
        });

        let info = rate_limits.entry(ip).or_insert_with(|| RequestInfo {
            count: 0,
            first_request: now,
            last_request: now,
            penalty_until: None,
        });

        // Check if still under penalty
        if let Some(penalty_until) = info.penalty_until {
            if now < penalty_until {
                return false; // Still penalized
            } else {
                info.penalty_until = None; // Clear expired penalty
            }
        }

        // Reset window if needed
        if now.duration_since(info.first_request) > self.rate_limit_config.window_duration {
            info.count = 0;
            info.first_request = now;
        }

        info.count += 1;
        info.last_request = now;

        // Check if limit exceeded
        if info.count > self.rate_limit_config.max_requests {
            info.penalty_until = Some(now + self.rate_limit_config.penalty_duration);
            return false;
        }

        true
    }

    /// Check for DoS attacks
    pub async fn check_dos_protection(&self, ip: IpAddr) -> bool {
        let now = Instant::now();
        let mut dos_tracking = self.dos_tracking.write().await;

        // Clean expired entries
        dos_tracking.retain(|_, info| {
            if let Some(blocked_until) = info.blocked_until {
                now < blocked_until
            } else {
                info.request_times.first().is_some_and(|first| {
                    now.duration_since(*first) < self.dos_config.monitor_duration * 2
                })
            }
        });

        let info = dos_tracking.entry(ip).or_insert_with(|| DosInfo {
            request_times: Vec::new(),
            blocked_until: None,
        });

        // Check if still blocked
        if let Some(blocked_until) = info.blocked_until {
            if now < blocked_until {
                return false; // Still blocked
            } else {
                info.blocked_until = None; // Clear expired block
                info.request_times.clear(); // Reset tracking
            }
        }

        // Add current request
        info.request_times.push(now);

        // Remove old requests outside monitor window
        info.request_times
            .retain(|&time| now.duration_since(time) <= self.dos_config.monitor_duration);

        // Check if DoS threshold exceeded
        let rate = info.request_times.len() as f64 / self.dos_config.monitor_duration.as_secs_f64();
        if rate > self.dos_config.max_rate {
            info.blocked_until = Some(now + self.dos_config.block_duration);
            return false;
        }

        true
    }

    /// Check if IP is blacklisted
    pub async fn check_blacklist(&self, ip: IpAddr) -> bool {
        // Check manual blacklist
        let manual_blacklist = self.manual_blacklist.read().await;
        if manual_blacklist.contains(&ip) {
            return false;
        }
        drop(manual_blacklist);

        // Check automatic blacklist
        let now = Instant::now();
        let mut failure_tracking = self.failure_tracking.write().await;

        // Clean expired entries
        failure_tracking.retain(|_, info| {
            if let Some(blacklisted_until) = info.blacklisted_until {
                now < blacklisted_until
            } else {
                now.duration_since(info.first_attempt) < self.blacklist_config.attempt_window * 2
            }
        });

        if let Some(info) = failure_tracking.get(&ip)
            && let Some(blacklisted_until) = info.blacklisted_until
        {
            return now >= blacklisted_until;
        }

        true
    }

    /// Record a failed authentication attempt
    pub async fn record_failure(&self, ip: IpAddr) {
        let now = Instant::now();
        let mut failure_tracking = self.failure_tracking.write().await;

        let info = failure_tracking.entry(ip).or_insert_with(|| FailureInfo {
            attempts: 0,
            first_attempt: now,
            blacklisted_until: None,
        });

        // Reset window if needed
        if now.duration_since(info.first_attempt) > self.blacklist_config.attempt_window {
            info.attempts = 0;
            info.first_attempt = now;
        }

        info.attempts += 1;

        // Check if should blacklist
        if info.attempts >= self.blacklist_config.max_failed_attempts {
            info.blacklisted_until = Some(now + self.blacklist_config.blacklist_duration);
        }
    }

    /// Manually add IP to blacklist
    pub async fn add_to_blacklist(&self, ip: IpAddr) {
        let mut manual_blacklist = self.manual_blacklist.write().await;
        if !manual_blacklist.contains(&ip) {
            manual_blacklist.push(ip);
        }
    }

    /// Remove IP from manual blacklist
    pub async fn remove_from_blacklist(&self, ip: IpAddr) {
        let mut manual_blacklist = self.manual_blacklist.write().await;
        manual_blacklist.retain(|&x| x != ip);
    }

    /// Get security statistics
    pub async fn get_stats(&self) -> SecurityStats {
        let rate_limits = self.rate_limits.read().await;
        let dos_tracking = self.dos_tracking.read().await;
        let failure_tracking = self.failure_tracking.read().await;
        let manual_blacklist = self.manual_blacklist.read().await;

        let now = Instant::now();

        SecurityStats {
            total_rate_limited_ips: rate_limits.len(),
            currently_penalized_ips: rate_limits
                .values()
                .filter(|info| info.penalty_until.is_some_and(|until| now < until))
                .count(),
            total_dos_tracked_ips: dos_tracking.len(),
            currently_blocked_ips: dos_tracking
                .values()
                .filter(|info| info.blocked_until.is_some_and(|until| now < until))
                .count(),
            total_failure_tracked_ips: failure_tracking.len(),
            currently_blacklisted_ips: failure_tracking
                .values()
                .filter(|info| info.blacklisted_until.is_some_and(|until| now < until))
                .count()
                + manual_blacklist.len(),
            manual_blacklist_size: manual_blacklist.len(),
        }
    }
}

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

/// Security statistics
#[derive(Debug, Serialize)]
pub struct SecurityStats {
    pub total_rate_limited_ips: usize,
    pub currently_penalized_ips: usize,
    pub total_dos_tracked_ips: usize,
    pub currently_blocked_ips: usize,
    pub total_failure_tracked_ips: usize,
    pub currently_blacklisted_ips: usize,
    pub manual_blacklist_size: usize,
}

/// Security middleware
pub async fn security_middleware(
    ConnectInfo(addr): ConnectInfo<SocketAddr>,
    State(state): State<ApiState>,
    request: Request<axum::body::Body>,
    next: Next,
) -> Result<Response, StatusCode> {
    let ip = addr.ip();

    // Get security manager
    let security_manager = match state.auth_framework.security_manager() {
        Some(manager) => manager,
        None => return Ok(next.run(request).await), // No security manager, allow request
    };

    // Check blacklist first
    if !security_manager.check_blacklist(ip).await {
        return Err(StatusCode::FORBIDDEN);
    }

    // Check DoS protection
    if !security_manager.check_dos_protection(ip).await {
        return Err(StatusCode::TOO_MANY_REQUESTS);
    }

    // Check rate limiting
    if !security_manager.check_rate_limit(ip).await {
        return Err(StatusCode::TOO_MANY_REQUESTS);
    }

    // Allow request
    Ok(next.run(request).await)
}

// ============================================================================
// API Endpoints
// ============================================================================

/// Get security statistics
pub async fn get_security_stats(
    State(state): State<ApiState>,
) -> Result<Json<ApiResponse<SecurityStats>>, StatusCode> {
    let security_manager = match state.auth_framework.security_manager() {
        Some(manager) => manager,
        None => return Err(StatusCode::SERVICE_UNAVAILABLE),
    };

    let stats = security_manager.get_stats().await;
    Ok(Json(ApiResponse::success(stats)))
}

/// Blacklist management request
#[derive(Debug, Deserialize)]
pub struct BlacklistRequest {
    pub ip: IpAddr,
}

/// Add IP to blacklist
pub async fn add_to_blacklist(
    State(state): State<ApiState>,
    Json(request): Json<BlacklistRequest>,
) -> Result<Json<ApiResponse<()>>, StatusCode> {
    let security_manager = match state.auth_framework.security_manager() {
        Some(manager) => manager,
        None => return Err(StatusCode::SERVICE_UNAVAILABLE),
    };

    security_manager.add_to_blacklist(request.ip).await;
    Ok(Json(ApiResponse::success_with_message(
        (),
        "IP added to blacklist",
    )))
}

/// Remove IP from blacklist
pub async fn remove_from_blacklist(
    State(state): State<ApiState>,
    Json(request): Json<BlacklistRequest>,
) -> Result<Json<ApiResponse<()>>, StatusCode> {
    let security_manager = match state.auth_framework.security_manager() {
        Some(manager) => manager,
        None => return Err(StatusCode::SERVICE_UNAVAILABLE),
    };

    security_manager.remove_from_blacklist(request.ip).await;
    Ok(Json(ApiResponse::success_with_message(
        (),
        "IP removed from blacklist",
    )))
}