backbone-bucket 0.2.0

Bucket Bounded Context: File Storage Module for Backbone Framework
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
// Authentication Middleware
// JWT-based authentication and authorization middleware

use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage, Result};
use actix_web::{error::ErrorUnauthorized, middleware, web, HttpRequest};
use actix_web_httpauth::extractors::Authentication;
use actix_web_httpauth::middleware::HttpAuthentication;
use chrono::{Duration, Utc};
use futures_util::future::{ok, Ready};
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

// JWT Claims
#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
    pub sub: String,            // Subject (user ID)
    pub email: String,          // User email
    pub roles: Vec<String>,     // User roles
    pub permissions: Vec<String>, // User permissions
    pub exp: usize,             // Expiration time
    pub iat: usize,             // Issued at
    pub iss: String,            // Issuer
    pub aud: String,            // Audience
}

impl Claims {
    pub fn new(
        user_id: String,
        email: String,
        roles: Vec<String>,
        permissions: Vec<String>,
        secret: &str,
        expiration_hours: i64,
    ) -> Result<Self,jsonwebtoken::errors::Error> {
        let now = Utc::now();
        let exp = now + Duration::hours(expiration_hours);

        Ok(Self {
            sub: user_id,
            email,
            roles,
            permissions,
            exp: exp.timestamp() as usize,
            iat: now.timestamp() as usize,
            iss: "backbone-api".to_string(),
            aud: "backbone-clients".to_string(),
        })
    }

    pub fn create_token(&self, secret: &str) -> Result<String, jsonwebtoken::errors::Error> {
        let header = Header::default();
        let encoding_key = EncodingKey::from_secret(secret.as_ref());

        encode(&header, &self, &encoding_key)
    }

    pub fn decode_token(token: &str, secret: &str) -> Result<Self, jsonwebtoken::errors::Error> {
        let decoding_key = DecodingKey::from_secret(secret.as_ref());
        let validation = Validation::new(Algorithm::HS256);

        decode::<Claims>(token, &decoding_key, &validation).map(|data| data.claims)
    }

    pub fn is_expired(&self) -> bool {
        let now = Utc::now().timestamp() as usize;
        self.exp <= now
    }

    pub fn has_role(&self, role: &str) -> bool {
        self.roles.contains(&role.to_string())
    }

    pub fn has_permission(&self, permission: &str) -> bool {
        self.permissions.contains(&permission.to_string())
    }

    pub fn has_any_role(&self, roles: &[&str]) -> bool {
        roles.iter().any(|role| self.has_role(role))
    }

    pub fn has_any_permission(&self, permissions: &[&str]) -> bool {
        permissions.iter().any(|permission| self.has_permission(permission))
    }

    pub fn has_all_roles(&self, roles: &[&str]) -> bool {
        roles.iter().all(|role| self.has_role(role))
    }

    pub fn has_all_permissions(&self, permissions: &[&str]) -> bool {
        permissions.iter().all(|permission| self.has_permission(permission))
    }
}

// Authentication Request Extensions
#[derive(Debug, Clone)]
pub struct AuthenticatedUser {
    pub user_id: String,
    pub email: String,
    pub roles: Vec<String>,
    pub permissions: Vec<String>,
}

impl AuthenticatedUser {
    pub fn from_claims(claims: &Claims) -> Self {
        Self {
            user_id: claims.sub.clone(),
            email: claims.email.clone(),
            roles: claims.roles.clone(),
            permissions: claims.permissions.clone(),
        }
    }

    pub fn has_role(&self, role: &str) -> bool {
        self.roles.contains(&role.to_string())
    }

    pub fn has_permission(&self, permission: &str) -> bool {
        self.permissions.contains(&permission.to_string())
    }
}

// JWT Authentication Middleware
pub struct JwtAuth {
    secret: String,
}

impl JwtAuth {
    pub fn new(secret: String) -> Self {
        Self { secret }
    }

    pub fn validate_token(&self, token: &str) -> Result<Claims, String> {
        Claims::decode_token(token, &self.secret)
            .map_err(|e| format!("Invalid token: {}", e))
    }

    pub fn extract_token_from_header(req: &HttpRequest) -> Option<String> {
        if let Some(auth_header) = req.headers().get("Authorization") {
            if let Ok(auth_str) = auth_header.to_str() {
                if auth_str.starts_with("Bearer ") {
                    return Some(auth_str[7..].to_string());
                }
            }
        }
        None
    }
}

// Actix-web authentication extractor
impl actix_web_httpauth::extractors::AuthenticationError for AuthenticatedUser {
    fn from_request(req: &ServiceRequest, credentials: Self::Credentials) -> Result<Self, Error> {
        let token = match credentials {
            actix_web_httpauth::extractors::Authentication::Bearer(token) => token,
            _ => return Err(ErrorUnauthorized("Bearer token required")),
        };

        let jwt_secret = req
            .app_data::<web::Data<String>>()
            .ok_or_else(|| ErrorUnauthorized("JWT secret not configured"))?;

        let claims = Claims::decode_token(token, jwt_secret)
            .map_err(|_| ErrorUnauthorized("Invalid JWT token"))?;

        if claims.is_expired() {
            return Err(ErrorUnauthorized("Token has expired"));
        }

        Ok(AuthenticatedUser::from_claims(&claims))
    }
}

// Authorization middleware factory
pub fn create_jwt_auth(jwt_secret: String) -> HttpAuthentication<JwtAuth> {
    HttpAuthentication::with_fn(JwtAuth::new(jwt_secret), |req, auth: JwtAuth| {
        let token = JwtAuth::extract_token_from_header(req)
            .ok_or_else(|| ErrorUnauthorized("No token provided"))?;

        auth.validate_token(&token)
            .map_err(|_| ErrorUnauthorized("Invalid token"))?;

        Ok(())
    })
}

// Role-based authorization middleware
pub fn require_role(role: &'static str) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_role(role)
}

// Permission-based authorization middleware
pub fn require_permission(permission: &'static str) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_permission(permission)
}

// Multiple roles authorization middleware
pub fn require_any_role(roles: &'static [&'static str]) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_any_role(roles)
}

// Multiple permissions authorization middleware
pub fn require_any_permission(permissions: &'static [&'static str]) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_any_permission(permissions)
}

// All roles authorization middleware
pub fn require_all_roles(roles: &'static [&'static str]) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_all_roles(roles)
}

// All permissions authorization middleware
pub fn require_all_permissions(permissions: &'static [&'static str]) -> impl Fn(&AuthenticatedUser) -> bool {
    move |user: &AuthenticatedUser| user.has_all_permissions(permissions)
}

// Rate limiting middleware
pub struct RateLimiter {
    requests: std::sync::RwLock<HashMap<String, std::sync::RwLock<RequestInfo>>>,
    max_requests: usize,
    window_seconds: u64,
}

#[derive(Debug, Clone)]
struct RequestInfo {
    count: usize,
    window_start: chrono::DateTime<chrono::Utc>,
}

impl RateLimiter {
    pub fn new(max_requests: usize, window_seconds: u64) -> Self {
        Self {
            requests: std::sync::RwLock::new(HashMap::new()),
            max_requests,
            window_seconds,
        }
    }

    pub fn is_allowed(&self, key: &str) -> bool {
        // Use unwrap_or_else to recover from poisoned lock
        let mut requests = match self.requests.write() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        let now = Utc::now();

        match requests.get_mut(key) {
            Some(info) => {
                let window_elapsed = now.signed_duration_since(info.window_start).num_seconds();

                if window_elapsed >= self.window_seconds as i64 {
                    // Reset window
                    info.count = 1;
                    info.window_start = now;
                    true
                } else if info.count < self.max_requests {
                    info.count += 1;
                    true
                } else {
                    false
                }
            }
            None => {
                requests.insert(
                    key.to_string(),
                    std::sync::RwLock::new(RequestInfo {
                        count: 1,
                        window_start: now,
                    }),
                );
                true
            }
        }
    }

    pub fn cleanup_expired_entries(&self) {
        // Use unwrap_or_else to recover from poisoned lock
        let mut requests = match self.requests.write() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        let now = Utc::now();

        requests.retain(|_, info| {
            let info = match info.read() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            now.signed_duration_since(info.window_start).num_seconds() < (self.window_seconds * 2) as i64
        });
    }
}

// Rate limiting middleware factory
pub fn create_rate_limiter(
    max_requests: usize,
    window_seconds: u64,
) -> impl Fn(ServiceRequest, actix_web::dev::Transform) -> Ready<Result<ServiceRequest, Error>> {
    let rate_limiter = std::sync::Arc::new(RateLimiter::new(max_requests, window_seconds));

    move |req: ServiceRequest, _: actix_web::dev::Transform| {
        let client_ip = req
            .connection_info()
            .peer_addr()
            .map(|addr| addr.ip().to_string())
            .unwrap_or_else(|| "unknown".to_string());

        let key = format!("{}:{}", client_ip, req.path());

        if !rate_limiter.is_allowed(&key) {
            return ok(req.error_response(ErrorTooManyRequests("Rate limit exceeded")));
        }

        ok(req)
    }
}

// CORS middleware factory
pub fn create_cors_middleware() -> middleware::Cors {
    middleware::Cors::default()
        .allowed_origin(vec!["http://localhost:3000", "http://localhost:8080"])
        .allowed_methods(vec!["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
        .allowed_headers(vec!["Authorization", "Content-Type", "Accept"])
        .expose_headers(vec!["X-Total-Count", "X-Page-Count"])
        .supports_credentials()
        .max_age(3600)
}

// Request logging middleware
pub async fn request_logger(
    req: ServiceRequest,
    next: actix_web::dev::Transform,
) -> Result<ServiceResponse, Error> {
    let start = std::time::Instant::now();
    let method = req.method().clone();
    let path = req.path().to_string();
    let user_agent = req
        .headers()
        .get("User-Agent")
        .and_then(|h| h.to_str().ok())
        .unwrap_or("unknown")
        .to_string();

    let resp = next.call(req).await?;

    let duration = start.elapsed();
    let status = resp.status();

    tracing::info!(
        method = %method,
        path = %path,
        status = %status.as_u16(),
        duration_ms = duration.as_millis(),
        user_agent = %user_agent,
        "HTTP request completed"
    );

    Ok(resp)
}

// Request ID middleware
pub async fn request_id(
    req: ServiceRequest,
    next: actix_web::dev::Transform,
) -> Result<ServiceResponse, Error> {
    use actix_web::http::header::HeaderValue;

    let request_id = uuid::Uuid::new_v4().to_string();

    let mut resp = next.call(req).await?;

    // UUID strings are always valid header values
    if let Ok(header_value) = HeaderValue::from_str(&request_id) {
        resp.response_mut().headers_mut().insert(
            actix_web::http::header::HeaderName::from_static("x-request-id"),
            header_value,
        );
    }

    Ok(resp)
}

// API version middleware
pub async fn api_version_middleware(
    req: ServiceRequest,
    next: actix_web::dev::Transform,
) -> Result<ServiceResponse, Error> {
    use actix_web::http::header::HeaderValue;

    let path = req.path();
    let version = path.strip_prefix("/api/v1").unwrap_or(path);

    let mut resp = next.call(req).await?;

    resp.response_mut().headers_mut().insert(
        actix_web::http::header::HeaderName::from_static("x-api-version"),
        HeaderValue::from_static("v1"),
    );

    // Path could contain characters invalid for headers, use fallback
    let header_value = HeaderValue::from_str(version)
        .unwrap_or_else(|_| HeaderValue::from_static("unknown"));
    resp.response_mut().headers_mut().insert(
        actix_web::http::header::HeaderName::from_static("x-api-path"),
        header_value,
    );

    Ok(resp)
}

// Security headers middleware
pub async fn security_headers(
    req: ServiceRequest,
    next: actix_web::dev::Transform,
) -> Result<ServiceResponse, Error> {
    use actix_web::http::header::HeaderValue;

    let mut resp = next.call(req).await?;

    let headers = resp.response_mut().headers_mut();

    // Security headers - using from_static for compile-time validated strings
    headers.insert(
        actix_web::http::header::HeaderName::from_static("x-content-type-options"),
        HeaderValue::from_static("nosniff"),
    );

    headers.insert(
        actix_web::http::header::HeaderName::from_static("x-frame-options"),
        HeaderValue::from_static("DENY"),
    );

    headers.insert(
        actix_web::http::header::HeaderName::from_static("x-xss-protection"),
        HeaderValue::from_static("1; mode=block"),
    );

    headers.insert(
        actix_web::http::header::HeaderName::from_static("strict-transport-security"),
        HeaderValue::from_static("max-age=31536000; includeSubDomains"),
    );

    Ok(resp)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;

    #[test]
    fn test_claims_creation() {
        let claims = Claims::new(
            "user123".to_string(),
            "user@example.com".to_string(),
            vec!["user".to_string(), "admin".to_string()],
            vec!["read".to_string(), "write".to_string()],
            "test-secret",
            24,
        ).unwrap();

        assert_eq!(claims.sub, "user123");
        assert_eq!(claims.email, "user@example.com");
        assert_eq!(claims.roles, vec!["user", "admin"]);
        assert_eq!(claims.permissions, vec!["read", "write"]);
        assert!(!claims.is_expired());
    }

    #[test]
    fn test_claims_token_creation() {
        let claims = Claims::new(
            "user123".to_string(),
            "user@example.com".to_string(),
            vec!["user".to_string()],
            vec!["read".to_string()],
            "test-secret",
            1,
        ).unwrap();

        let token = claims.create_token("test-secret").unwrap();
        assert!(!token.is_empty());

        // Verify token can be decoded
        let decoded_claims = Claims::decode_token(&token, "test-secret").unwrap();
        assert_eq!(decoded_claims.sub, "user123");
        assert_eq!(decoded_claims.email, "user@example.com");
    }

    #[test]
    fn test_claims_validation() {
        let claims = Claims::new(
            "user123".to_string(),
            "user@example.com".to_string(),
            vec!["user".to_string()],
            vec!["read".to_string()],
            "test-secret",
            1,
        ).unwrap();

        assert!(claims.has_role("user"));
        assert!(!claims.has_role("admin"));
        assert!(claims.has_permission("read"));
        assert!(!claims.has_permission("write"));

        assert!(claims.has_any_role(&["user", "admin"]));
        assert!(!claims.has_any_role(&["admin", "manager"]));
        assert!(claims.has_any_permission(&["read", "write"]));
        assert!(!claims.has_any_permission(&["write", "delete"]));

        assert!(claims.has_all_roles(&["user"]));
        assert!(!claims.has_all_roles(&["user", "admin"]));
        assert!(claims.has_all_permissions(&["read"]));
        assert!(!claims.has_all_permissions(&["read", "write"]));
    }

    #[test]
    fn test_authenticated_user() {
        let claims = Claims::new(
            "user123".to_string(),
            "user@example.com".to_string(),
            vec!["user".to_string()],
            vec!["read".to_string()],
            "test-secret",
            1,
        ).unwrap();

        let user = AuthenticatedUser::from_claims(&claims);
        assert_eq!(user.user_id, "user123");
        assert_eq!(user.email, "user@example.com");
        assert!(user.has_role("user"));
        assert!(user.has_permission("read"));
    }

    #[test]
    fn test_jwt_auth_token_extraction() {
        let jwt_auth = JwtAuth::new("test-secret".to_string());

        // Test valid token
        let token = "Bearer valid-token";
        assert_eq!(JwtAuth::extract_token_from_header(&create_test_request_with_auth(token)), "valid-token");

        // Test no authorization header
        let request = create_test_request();
        assert!(JwtAuth::extract_token_from_header(&request).is_none());

        // Test invalid authorization header
        let request = create_test_request_with_auth("InvalidToken");
        assert!(JwtAuth::extract_token_from_header(&request).is_none());
    }

    fn create_test_request() -> HttpRequest {
        actix_web::test::TestRequest::get()
            .uri("/")
            .to_request()
    }

    fn create_test_request_with_auth(auth_header: &str) -> HttpRequest {
        actix_web::test::TestRequest::get()
            .uri("/")
            .insert_header(("Authorization", auth_header))
            .to_request()
    }

    #[test]
    fn test_rate_limiter() {
        let rate_limiter = RateLimiter::new(5, 60); // 5 requests per minute

        let client_ip = "127.0.0.1";

        // First 5 requests should be allowed
        for _ in 0..5 {
            assert!(rate_limiter.is_allowed(client_ip));
        }

        // 6th request should be denied
        assert!(!rate_limiter.is_allowed(client_ip));

        // Wait for window to reset (in real implementation, this would require time manipulation)
        // For testing, we'll create a new rate limiter
        let rate_limiter = RateLimiter::new(5, 60);
        assert!(rate_limiter.is_allowed("different_ip"));
    }

    #[test]
    fn test_authorization_functions() {
        // Test role functions
        let check_admin = require_role("admin");
        let check_user = require_role("user");
        let check_any_role = require_any_role(&["admin", "user"]);
        let check_all_roles = require_all_roles(&["user", "reader"]);

        let admin_user = AuthenticatedUser {
            user_id: "admin123".to_string(),
            email: "admin@example.com".to_string(),
            roles: vec!["admin".to_string(), "user".to_string()],
            permissions: vec![],
        };

        let regular_user = AuthenticatedUser {
            user_id: "user123".to_string(),
            email: "user@example.com".to_string(),
            roles: vec!["user".to_string()],
            permissions: vec![],
        };

        assert!(check_admin(&admin_user));
        assert!(!check_admin(&regular_user));
        assert!(check_user(&admin_user));
        assert!(check_user(&regular_user));
        assert!(check_any_role(&admin_user));
        assert!(check_any_role(&regular_user));
        assert!(check_all_roles(&admin_user));
        assert!(!check_all_roles(&regular_user));

        // Test permission functions
        let check_read = require_permission("read");
        let check_write = require_permission("write");
        let check_any_permission = require_any_permission(&["read", "write"]);
        let check_all_permissions = require_all_permissions(&["read", "write"]);

        let user_with_permissions = AuthenticatedUser {
            user_id: "user123".to_string(),
            email: "user@example.com".to_string(),
            roles: vec![],
            permissions: vec!["read".to_string(), "write".to_string()],
        };

        let user_with_read_only = AuthenticatedUser {
            user_id: "user123".to_string(),
            email: "user@example.com".to_string(),
            roles: vec![],
            permissions: vec!["read".to_string()],
        };

        assert!(check_read(&user_with_permissions));
        assert!(check_write(&user_with_permissions));
        assert!(!check_write(&user_with_read_only));
        assert!(check_any_permission(&user_with_permissions));
        assert!(check_any_permission(&user_with_read_only));
        assert!(check_all_permissions(&user_with_permissions));
        assert!(!check_all_permissions(&user_with_read_only));
    }
}