pulseengine-mcp-auth 0.14.0

Authentication and authorization framework for MCP servers - PulseEngine MCP 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
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
//! Consent management operations and storage
//!
//! This module provides the main ConsentManager for handling consent
//! operations, storage, and audit trails.

use super::{
    ConsentAuditEntry, ConsentError, ConsentRecord, ConsentStatus, ConsentSummary, ConsentType,
    LegalBasis,
};
use async_trait::async_trait;
use chrono::Utc;
use serde_json;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Parameters for requesting consent
#[derive(Debug, Clone)]
pub struct ConsentRequest {
    pub subject_id: String,
    pub consent_type: ConsentType,
    pub legal_basis: LegalBasis,
    pub purpose: String,
    pub data_categories: Vec<String>,
    pub consent_source: String,
    pub expires_in_days: Option<u32>,
}

/// Simple key-value storage trait for consent data
#[async_trait]
pub trait ConsentStorage: Send + Sync {
    async fn get(&self, key: &str) -> Result<String, Box<dyn std::error::Error + Send + Sync>>;
    async fn set(
        &self,
        key: &str,
        value: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
    async fn delete(&self, key: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
    async fn list(&self) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>>;
}

/// Simple in-memory storage implementation for consent data
pub struct MemoryConsentStorage {
    data: Arc<RwLock<HashMap<String, String>>>,
}

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

impl MemoryConsentStorage {
    pub fn new() -> Self {
        Self {
            data: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl ConsentStorage for MemoryConsentStorage {
    async fn get(&self, key: &str) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        let data = self.data.read().await;
        data.get(key).cloned().ok_or_else(|| "Key not found".into())
    }

    async fn set(
        &self,
        key: &str,
        value: &str,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = self.data.write().await;
        data.insert(key.to_string(), value.to_string());
        Ok(())
    }

    async fn delete(&self, key: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut data = self.data.write().await;
        data.remove(key);
        Ok(())
    }

    async fn list(&self) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
        let data = self.data.read().await;
        Ok(data.keys().cloned().collect())
    }
}

/// Consent manager configuration
#[derive(Debug, Clone)]
pub struct ConsentConfig {
    /// Enable consent management
    pub enabled: bool,

    /// Default consent expiration in days (None = no expiration)
    pub default_expiration_days: Option<u32>,

    /// Require explicit consent for all operations
    pub require_explicit_consent: bool,

    /// Enable consent audit logging
    pub enable_audit_log: bool,

    /// Path for consent audit log
    pub audit_log_path: Option<std::path::PathBuf>,

    /// Automatic cleanup of expired consents after days
    pub cleanup_expired_after_days: u32,
}

impl Default for ConsentConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            default_expiration_days: Some(365), // 1 year default
            require_explicit_consent: true,
            enable_audit_log: true,
            audit_log_path: None,
            cleanup_expired_after_days: 90,
        }
    }
}

/// Main consent manager
pub struct ConsentManager {
    config: ConsentConfig,
    storage: Arc<dyn ConsentStorage>,
    audit_entries: Arc<RwLock<Vec<ConsentAuditEntry>>>,
    consent_cache: Arc<RwLock<HashMap<String, ConsentRecord>>>,
}

impl ConsentManager {
    /// Create a new consent manager
    pub fn new(config: ConsentConfig, storage: Arc<dyn ConsentStorage>) -> Self {
        Self {
            config,
            storage,
            audit_entries: Arc::new(RwLock::new(Vec::new())),
            consent_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Request consent from a subject with individual parameters
    #[allow(clippy::too_many_arguments)]
    pub async fn request_consent_individual(
        &self,
        subject_id: String,
        consent_type: ConsentType,
        legal_basis: LegalBasis,
        purpose: String,
        data_categories: Vec<String>,
        consent_source: String,
        expires_in_days: Option<u32>,
    ) -> Result<ConsentRecord, ConsentError> {
        let request = ConsentRequest {
            subject_id,
            consent_type,
            legal_basis,
            purpose,
            data_categories,
            consent_source,
            expires_in_days,
        };
        self.request_consent(request).await
    }

    /// Request consent from a subject
    pub async fn request_consent(
        &self,
        request: ConsentRequest,
    ) -> Result<ConsentRecord, ConsentError> {
        if !self.config.enabled {
            return Err(ConsentError::InvalidData(
                "Consent management is disabled".to_string(),
            ));
        }

        // Check if consent already exists
        let existing_key = format!(
            "consent:{}:{}",
            request.subject_id,
            self.consent_type_key(&request.consent_type)
        );
        if self.storage.get(&existing_key).await.is_ok() {
            return Err(ConsentError::ConsentExists(format!(
                "{}:{:?}",
                request.subject_id, request.consent_type
            )));
        }

        // Create consent record
        let mut record = ConsentRecord::new(
            request.subject_id.clone(),
            request.consent_type.clone(),
            request.legal_basis,
            request.purpose,
            request.consent_source.clone(),
        );

        // Add data categories
        for category in request.data_categories {
            record.add_data_category(category);
        }

        // Set expiration
        if let Some(days) = request
            .expires_in_days
            .or(self.config.default_expiration_days)
        {
            let expires_at = Utc::now() + chrono::Duration::days(days as i64);
            record.set_expiration(expires_at);
        }

        // Store consent record
        let consent_data =
            serde_json::to_string(&record).map_err(ConsentError::SerializationError)?;

        self.storage
            .set(&existing_key, &consent_data)
            .await
            .map_err(|e| ConsentError::StorageError(e.to_string()))?;

        // Update cache
        {
            let mut cache = self.consent_cache.write().await;
            cache.insert(record.id.clone(), record.clone());
        }

        // Create audit entry
        self.create_audit_entry(
            &record,
            "consent_requested".to_string(),
            None,
            ConsentStatus::Pending,
            request.consent_source,
            None,
            HashMap::new(),
        )
        .await?;

        info!(
            "Consent requested for subject {} with type {:?}",
            request.subject_id, request.consent_type
        );
        Ok(record)
    }

    /// Grant consent
    pub async fn grant_consent(
        &self,
        subject_id: &str,
        consent_type: &ConsentType,
        source_ip: Option<String>,
        action_source: String,
    ) -> Result<ConsentRecord, ConsentError> {
        let consent_key = format!(
            "consent:{}:{}",
            subject_id,
            self.consent_type_key(consent_type)
        );

        // Load existing consent record
        let consent_data =
            self.storage.get(&consent_key).await.map_err(|_| {
                ConsentError::ConsentNotFound(format!("{subject_id}:{consent_type:?}"))
            })?;

        let mut record: ConsentRecord =
            serde_json::from_str(&consent_data).map_err(ConsentError::SerializationError)?;

        let previous_status = record.status.clone();

        // Grant consent
        record.grant(source_ip.clone());

        // Update storage
        let updated_data =
            serde_json::to_string(&record).map_err(ConsentError::SerializationError)?;

        self.storage
            .set(&consent_key, &updated_data)
            .await
            .map_err(|e| ConsentError::StorageError(e.to_string()))?;

        // Update cache
        {
            let mut cache = self.consent_cache.write().await;
            cache.insert(record.id.clone(), record.clone());
        }

        // Create audit entry
        self.create_audit_entry(
            &record,
            "consent_granted".to_string(),
            Some(previous_status),
            record.status.clone(),
            action_source,
            source_ip,
            HashMap::new(),
        )
        .await?;

        info!(
            "Consent granted for subject {} with type {:?}",
            subject_id, consent_type
        );
        Ok(record)
    }

    /// Withdraw consent
    pub async fn withdraw_consent(
        &self,
        subject_id: &str,
        consent_type: &ConsentType,
        source_ip: Option<String>,
        action_source: String,
    ) -> Result<ConsentRecord, ConsentError> {
        let consent_key = format!(
            "consent:{}:{}",
            subject_id,
            self.consent_type_key(consent_type)
        );

        // Load existing consent record
        let consent_data =
            self.storage.get(&consent_key).await.map_err(|_| {
                ConsentError::ConsentNotFound(format!("{subject_id}:{consent_type:?}"))
            })?;

        let mut record: ConsentRecord =
            serde_json::from_str(&consent_data).map_err(ConsentError::SerializationError)?;

        let previous_status = record.status.clone();

        // Withdraw consent
        record.withdraw(source_ip.clone());

        // Update storage
        let updated_data =
            serde_json::to_string(&record).map_err(ConsentError::SerializationError)?;

        self.storage
            .set(&consent_key, &updated_data)
            .await
            .map_err(|e| ConsentError::StorageError(e.to_string()))?;

        // Update cache
        {
            let mut cache = self.consent_cache.write().await;
            cache.insert(record.id.clone(), record.clone());
        }

        // Create audit entry
        self.create_audit_entry(
            &record,
            "consent_withdrawn".to_string(),
            Some(previous_status),
            record.status.clone(),
            action_source,
            source_ip,
            HashMap::new(),
        )
        .await?;

        warn!(
            "Consent withdrawn for subject {} with type {:?}",
            subject_id, consent_type
        );
        Ok(record)
    }

    /// Check if consent is valid for a subject and type
    pub async fn check_consent(
        &self,
        subject_id: &str,
        consent_type: &ConsentType,
    ) -> Result<bool, ConsentError> {
        if !self.config.enabled {
            // If consent management is disabled, assume consent
            return Ok(true);
        }

        let consent_key = format!(
            "consent:{}:{}",
            subject_id,
            self.consent_type_key(consent_type)
        );

        // Try cache first
        {
            let cache = self.consent_cache.read().await;
            if let Some(record) = cache
                .values()
                .find(|r| r.subject_id == subject_id && &r.consent_type == consent_type)
            {
                return Ok(record.is_valid());
            }
        }

        // Load from storage
        match self.storage.get(&consent_key).await {
            Ok(consent_data) => {
                let record: ConsentRecord = serde_json::from_str(&consent_data)
                    .map_err(ConsentError::SerializationError)?;

                // Update cache
                {
                    let mut cache = self.consent_cache.write().await;
                    cache.insert(record.id.clone(), record.clone());
                }

                Ok(record.is_valid())
            }
            Err(_) => {
                if self.config.require_explicit_consent {
                    Ok(false) // No consent found and explicit consent required
                } else {
                    Ok(true) // No consent found but explicit consent not required
                }
            }
        }
    }

    /// Get consent summary for a subject
    pub async fn get_consent_summary(
        &self,
        subject_id: &str,
    ) -> Result<ConsentSummary, ConsentError> {
        let mut consents = HashMap::new();
        let mut pending_requests = 0;
        let mut expired_consents = 0;
        let mut last_updated = Utc::now();

        // Search for all consent records for this subject
        // This is simplified - in a real implementation you'd want indexed lookups
        let all_keys = self
            .storage
            .list()
            .await
            .map_err(|e| ConsentError::StorageError(e.to_string()))?;

        let subject_prefix = format!("consent:{subject_id}:");

        for key in all_keys {
            if key.starts_with(&subject_prefix) {
                if let Ok(consent_data) = self.storage.get(&key).await {
                    if let Ok(record) = serde_json::from_str::<ConsentRecord>(&consent_data) {
                        consents.insert(record.consent_type.clone(), record.status.clone());

                        if record.status == ConsentStatus::Pending {
                            pending_requests += 1;
                        }

                        if record.is_expired() {
                            expired_consents += 1;
                        }

                        if record.updated_at > last_updated {
                            last_updated = record.updated_at;
                        }
                    }
                }
            }
        }

        let is_valid = consents
            .iter()
            .all(|(_, status)| *status == ConsentStatus::Granted);

        Ok(ConsentSummary {
            subject_id: subject_id.to_string(),
            consents,
            is_valid,
            last_updated,
            pending_requests,
            expired_consents,
        })
    }

    /// Clean up expired consents
    pub async fn cleanup_expired_consents(&self) -> Result<usize, ConsentError> {
        let cutoff_date =
            Utc::now() - chrono::Duration::days(self.config.cleanup_expired_after_days as i64);
        let mut cleaned_count = 0;

        let all_keys = self
            .storage
            .list()
            .await
            .map_err(|e| ConsentError::StorageError(e.to_string()))?;

        for key in all_keys {
            if key.starts_with("consent:") {
                if let Ok(consent_data) = self.storage.get(&key).await {
                    if let Ok(record) = serde_json::from_str::<ConsentRecord>(&consent_data) {
                        if record.is_expired() && record.updated_at < cutoff_date {
                            self.storage
                                .delete(&key)
                                .await
                                .map_err(|e| ConsentError::StorageError(e.to_string()))?;

                            // Remove from cache
                            {
                                let mut cache = self.consent_cache.write().await;
                                cache.remove(&record.id);
                            }

                            cleaned_count += 1;
                            debug!("Cleaned up expired consent record: {}", record.id);
                        }
                    }
                }
            }
        }

        info!("Cleaned up {} expired consent records", cleaned_count);
        Ok(cleaned_count)
    }

    /// Get audit trail for a subject
    pub async fn get_audit_trail(&self, subject_id: &str) -> Vec<ConsentAuditEntry> {
        let audit_entries = self.audit_entries.read().await;
        audit_entries
            .iter()
            .filter(|entry| entry.subject_id == subject_id)
            .cloned()
            .collect()
    }

    /// Create an audit entry
    #[allow(clippy::too_many_arguments)]
    async fn create_audit_entry(
        &self,
        record: &ConsentRecord,
        action: String,
        previous_status: Option<ConsentStatus>,
        new_status: ConsentStatus,
        action_source: String,
        source_ip: Option<String>,
        details: HashMap<String, String>,
    ) -> Result<(), ConsentError> {
        if !self.config.enable_audit_log {
            return Ok(());
        }

        let audit_entry = ConsentAuditEntry {
            id: Uuid::new_v4().to_string(),
            consent_id: record.id.clone(),
            subject_id: record.subject_id.clone(),
            action,
            previous_status,
            new_status,
            action_source,
            source_ip,
            details,
            timestamp: Utc::now(),
        };

        // Add to in-memory audit log
        {
            let mut audit_entries = self.audit_entries.write().await;
            audit_entries.push(audit_entry.clone());

            // Keep only last 10000 entries to prevent memory bloat
            if audit_entries.len() > 10000 {
                audit_entries.drain(0..1000);
            }
        }

        // Write to persistent audit log file if configured
        if let Some(log_path) = &self.config.audit_log_path {
            let log_entry = serde_json::to_string(&audit_entry)
                .unwrap_or_else(|_| "Failed to serialize audit entry".to_string());
            let log_line = format!("{}\n", log_entry);

            match tokio::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(log_path)
                .await
            {
                Ok(mut file) => {
                    use tokio::io::AsyncWriteExt;
                    if let Err(e) = file.write_all(log_line.as_bytes()).await {
                        tracing::error!("Failed to write audit log to file: {}", e);
                    } else if let Err(e) = file.flush().await {
                        tracing::error!("Failed to flush audit log file: {}", e);
                    }
                }
                Err(e) => {
                    tracing::error!("Failed to open audit log file: {}", e);
                }
            }
        }

        Ok(())
    }

    /// Convert consent type to storage key
    fn consent_type_key(&self, consent_type: &ConsentType) -> String {
        match consent_type {
            ConsentType::DataProcessing => "data_processing".to_string(),
            ConsentType::Marketing => "marketing".to_string(),
            ConsentType::Analytics => "analytics".to_string(),
            ConsentType::DataSharing => "data_sharing".to_string(),
            ConsentType::AutomatedDecisionMaking => "automated_decision_making".to_string(),
            ConsentType::SessionStorage => "session_storage".to_string(),
            ConsentType::AuditLogging => "audit_logging".to_string(),
            ConsentType::Custom(name) => {
                format!("custom_{}", name.to_lowercase().replace(' ', "_"))
            }
        }
    }
}

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

    #[tokio::test]
    async fn test_consent_manager_creation() {
        let config = ConsentConfig::default();
        let storage = Arc::new(MemoryConsentStorage::new());
        let manager = ConsentManager::new(config, storage);

        // Manager should be created successfully
        assert!(manager.config.enabled);
    }

    #[tokio::test]
    async fn test_consent_request_and_grant() {
        let config = ConsentConfig::default();
        let storage = Arc::new(MemoryConsentStorage::new());
        let manager = ConsentManager::new(config, storage);

        // Request consent
        let request = ConsentRequest {
            subject_id: "user123".to_string(),
            consent_type: ConsentType::DataProcessing,
            legal_basis: LegalBasis::Consent,
            purpose: "Process authentication data".to_string(),
            data_categories: vec!["personal_identifiers".to_string()],
            consent_source: "test".to_string(),
            expires_in_days: None,
        };
        let record = manager.request_consent(request).await.unwrap();

        assert_eq!(record.status, ConsentStatus::Pending);

        // Grant consent
        let granted_record = manager
            .grant_consent(
                "user123",
                &ConsentType::DataProcessing,
                Some("127.0.0.1".to_string()),
                "test".to_string(),
            )
            .await
            .unwrap();

        assert_eq!(granted_record.status, ConsentStatus::Granted);

        // Check consent
        let is_valid = manager
            .check_consent("user123", &ConsentType::DataProcessing)
            .await
            .unwrap();
        assert!(is_valid);
    }

    #[tokio::test]
    async fn test_consent_withdrawal() {
        let config = ConsentConfig::default();
        let storage = Arc::new(MemoryConsentStorage::new());
        let manager = ConsentManager::new(config, storage);

        // Request and grant consent
        let request = ConsentRequest {
            subject_id: "user123".to_string(),
            consent_type: ConsentType::Analytics,
            legal_basis: LegalBasis::Consent,
            purpose: "Analytics tracking".to_string(),
            data_categories: vec![],
            consent_source: "test".to_string(),
            expires_in_days: None,
        };
        manager.request_consent(request).await.unwrap();

        manager
            .grant_consent("user123", &ConsentType::Analytics, None, "test".to_string())
            .await
            .unwrap();

        // Withdraw consent
        let withdrawn_record = manager
            .withdraw_consent("user123", &ConsentType::Analytics, None, "test".to_string())
            .await
            .unwrap();

        assert_eq!(withdrawn_record.status, ConsentStatus::Withdrawn);

        // Check consent is no longer valid
        let is_valid = manager
            .check_consent("user123", &ConsentType::Analytics)
            .await
            .unwrap();
        assert!(!is_valid);
    }
}