commy 0.4.1

A hierarchical, multi-tenant shared memory coordination system for Windows enabling secure, efficient data sharing between multiple processes via WebSocket and direct memory-mapping
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
//! Authentication method registration and management
//! 
//! Allows per-tenant configuration of authentication protocols:
//! - API Key authentication
//! - JWT (JSON Web Token)
//! - mTLS (mutual TLS)
//! - Custom authentication methods
//!
//! ## Hybrid Serialization Strategy
//! While the outer protocol messages use MessagePack for efficiency,
//! custom authentication configurations remain JSON (serde_json::Value).
//! This allows tenants to define custom authentication methods without
//! requiring schema coordination across the cluster.

use crate::auth::{AuthError, AuthResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Authentication method type
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AuthMethodType {
    /// API Key authentication (bearer token)
    ApiKey,
    /// JWT (JSON Web Token) authentication
    Jwt,
    /// mTLS (mutual TLS certificate) authentication
    Mtls,
    /// Custom authentication method
    Custom(String),
}

impl AuthMethodType {
    pub fn as_str(&self) -> &str {
        match self {
            Self::ApiKey => "api_key",
            Self::Jwt => "jwt",
            Self::Mtls => "mtls",
            Self::Custom(name) => name,
        }
    }

    pub fn from_str(s: &str) -> Self {
        match s {
            "api_key" => Self::ApiKey,
            "jwt" => Self::Jwt,
            "mtls" => Self::Mtls,
            custom => Self::Custom(custom.to_string()),
        }
    }
}

/// Configuration for API Key authentication
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ApiKeyConfig {
    /// Header name (e.g., "X-API-Key")
    pub header_name: String,
    /// API key value (in production, would be hashed)
    pub api_key: String,
}

/// Configuration for JWT authentication
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JwtConfig {
    /// Signing secret (in production, would be a key)
    pub signing_secret: String,
    /// Algorithm (HS256, RS256, etc.)
    pub algorithm: String,
    /// Token lifetime in seconds
    pub token_lifetime_secs: u64,
    /// Issuer claim
    pub issuer: String,
    /// Audience claim
    pub audience: String,
}

/// Configuration for mTLS authentication
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MtlsConfig {
    /// Path to CA certificate file
    pub ca_cert_path: String,
    /// Require client certificate
    pub require_client_cert: bool,
    /// Allowed certificate common names (Optional - allow all if None)
    pub allowed_cns: Option<Vec<String>>,
}

/// Base configuration for any auth method
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthMethodConfig {
    /// Method type
    pub method_type: String,
    /// Whether this method is enabled
    pub enabled: bool,
    /// Required for authentication
    pub required: bool,
    /// Configuration data (method-specific JSON)
    pub config: serde_json::Value,
}

/// Registered authentication method
#[derive(Clone, Debug)]
pub struct RegisteredAuthMethod {
    pub method_type: AuthMethodType,
    pub config: AuthMethodConfig,
}

/// Auth method registry for a tenant
/// 
/// Manages multiple authentication methods per tenant
pub struct AuthMethodRegistry {
    /// Tenant ID
    tenant_id: String,
    /// Registered methods
    methods: Arc<RwLock<HashMap<String, RegisteredAuthMethod>>>,
}

impl AuthMethodRegistry {
    /// Create a new auth method registry for a tenant
    pub fn new(tenant_id: String) -> Self {
        Self {
            tenant_id,
            methods: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Register an API Key authentication method
    pub async fn register_api_key(
        &self,
        name: String,
        config: ApiKeyConfig,
    ) -> AuthResult<()> {
        let method_config = AuthMethodConfig {
            method_type: AuthMethodType::ApiKey.as_str().to_string(),
            enabled: true,
            required: false,
            config: serde_json::to_value(&config).map_err(|e| {
                AuthError::ConfigurationError(format!("Failed to serialize API key config: {}", e))
            })?,
        };

        let mut methods = self.methods.write().await;
        methods.insert(
            name,
            RegisteredAuthMethod {
                method_type: AuthMethodType::ApiKey,
                config: method_config,
            },
        );

        Ok(())
    }

    /// Register a JWT authentication method
    pub async fn register_jwt(
        &self,
        name: String,
        config: JwtConfig,
    ) -> AuthResult<()> {
        let method_config = AuthMethodConfig {
            method_type: AuthMethodType::Jwt.as_str().to_string(),
            enabled: true,
            required: false,
            config: serde_json::to_value(&config).map_err(|e| {
                AuthError::ConfigurationError(format!("Failed to serialize JWT config: {}", e))
            })?,
        };

        let mut methods = self.methods.write().await;
        methods.insert(
            name,
            RegisteredAuthMethod {
                method_type: AuthMethodType::Jwt,
                config: method_config,
            },
        );

        Ok(())
    }

    /// Register an mTLS authentication method
    pub async fn register_mtls(
        &self,
        name: String,
        config: MtlsConfig,
    ) -> AuthResult<()> {
        let method_config = AuthMethodConfig {
            method_type: AuthMethodType::Mtls.as_str().to_string(),
            enabled: true,
            required: false,
            config: serde_json::to_value(&config).map_err(|e| {
                AuthError::ConfigurationError(format!("Failed to serialize mTLS config: {}", e))
            })?,
        };

        let mut methods = self.methods.write().await;
        methods.insert(
            name,
            RegisteredAuthMethod {
                method_type: AuthMethodType::Mtls,
                config: method_config,
            },
        );

        Ok(())
    }

    /// Register a custom authentication method
    pub async fn register_custom(
        &self,
        name: String,
        config: serde_json::Value,
    ) -> AuthResult<()> {
        let custom_type = name.clone();
        let method_config = AuthMethodConfig {
            method_type: custom_type.clone(),
            enabled: true,
            required: false,
            config,
        };

        let mut methods = self.methods.write().await;
        methods.insert(
            name,
            RegisteredAuthMethod {
                method_type: AuthMethodType::Custom(custom_type),
                config: method_config,
            },
        );

        Ok(())
    }

    /// Get a registered auth method
    pub async fn get_method(&self, name: &str) -> AuthResult<RegisteredAuthMethod> {
        let methods = self.methods.read().await;
        methods
            .get(name)
            .cloned()
            .ok_or_else(|| AuthError::ConfigurationError(format!("Auth method {} not found", name)))
    }

    /// Get all registered auth methods
    pub async fn get_all_methods(&self) -> Vec<RegisteredAuthMethod> {
        let methods = self.methods.read().await;
        methods.values().cloned().collect()
    }

    /// Enable/disable an auth method
    pub async fn set_method_enabled(&self, name: &str, enabled: bool) -> AuthResult<()> {
        let mut methods = self.methods.write().await;
        if let Some(method) = methods.get_mut(name) {
            method.config.enabled = enabled;
            Ok(())
        } else {
            Err(AuthError::ConfigurationError(format!("Auth method {} not found", name)))
        }
    }

    /// Get all enabled auth methods
    pub async fn get_enabled_methods(&self) -> Vec<RegisteredAuthMethod> {
        let methods = self.methods.read().await;
        methods
            .values()
            .filter(|m| m.config.enabled)
            .cloned()
            .collect()
    }

    /// Unregister an auth method
    pub async fn unregister_method(&self, name: &str) -> AuthResult<()> {
        let mut methods = self.methods.write().await;
        methods
            .remove(name)
            .ok_or_else(|| AuthError::ConfigurationError(format!("Auth method {} not found", name)))?;
        Ok(())
    }

    /// Check if a method exists
    pub async fn has_method(&self, name: &str) -> bool {
        let methods = self.methods.read().await;
        methods.contains_key(name)
    }

    /// Get tenant ID
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }
}

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

    #[tokio::test]
    async fn test_register_api_key() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let config = ApiKeyConfig {
            header_name: "X-API-Key".to_string(),
            api_key: "secret123".to_string(),
        };

        registry.register_api_key("default".to_string(), config).await.unwrap();

        let method = registry.get_method("default").await.unwrap();
        assert_eq!(method.method_type, AuthMethodType::ApiKey);
    }

    #[tokio::test]
    async fn test_register_jwt() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let config = JwtConfig {
            signing_secret: "secret".to_string(),
            algorithm: "HS256".to_string(),
            token_lifetime_secs: 3600,
            issuer: "commy".to_string(),
            audience: "tenant1".to_string(),
        };

        registry.register_jwt("default".to_string(), config).await.unwrap();

        let method = registry.get_method("default").await.unwrap();
        assert_eq!(method.method_type, AuthMethodType::Jwt);
    }

    #[tokio::test]
    async fn test_multiple_methods() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());

        let api_key_config = ApiKeyConfig {
            header_name: "X-API-Key".to_string(),
            api_key: "secret123".to_string(),
        };
        registry.register_api_key("api_key".to_string(), api_key_config).await.unwrap();

        let jwt_config = JwtConfig {
            signing_secret: "secret".to_string(),
            algorithm: "HS256".to_string(),
            token_lifetime_secs: 3600,
            issuer: "commy".to_string(),
            audience: "tenant1".to_string(),
        };
        registry.register_jwt("jwt".to_string(), jwt_config).await.unwrap();

        let all_methods = registry.get_all_methods().await;
        assert_eq!(all_methods.len(), 2);
    }

    #[tokio::test]
    async fn test_enable_disable_method() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let config = ApiKeyConfig {
            header_name: "X-API-Key".to_string(),
            api_key: "secret123".to_string(),
        };

        registry.register_api_key("default".to_string(), config).await.unwrap();
        registry.set_method_enabled("default", false).await.unwrap();

        let enabled = registry.get_enabled_methods().await;
        assert!(enabled.is_empty());
    }

    #[tokio::test]
    async fn test_custom_method() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let config = serde_json::json!({
            "custom_field": "custom_value"
        });

        registry.register_custom("custom".to_string(), config).await.unwrap();

        let method = registry.get_method("custom").await.unwrap();
        assert!(matches!(method.method_type, AuthMethodType::Custom(_)));
    }

    #[test]
    fn test_auth_method_type_as_str() {
        assert_eq!(AuthMethodType::ApiKey.as_str(), "api_key");
        assert_eq!(AuthMethodType::Jwt.as_str(), "jwt");
        assert_eq!(AuthMethodType::Mtls.as_str(), "mtls");
        assert_eq!(AuthMethodType::Custom("my_auth".to_string()).as_str(), "my_auth");
    }

    #[test]
    fn test_auth_method_type_from_str() {
        assert_eq!(AuthMethodType::from_str("api_key"), AuthMethodType::ApiKey);
        assert_eq!(AuthMethodType::from_str("jwt"), AuthMethodType::Jwt);
        assert_eq!(AuthMethodType::from_str("mtls"), AuthMethodType::Mtls);
        assert_eq!(
            AuthMethodType::from_str("custom_name"),
            AuthMethodType::Custom("custom_name".to_string())
        );
    }

    #[test]
    fn test_tenant_id() {
        let registry = AuthMethodRegistry::new("tenant_xyz".to_string());
        assert_eq!(registry.tenant_id(), "tenant_xyz");
    }

    #[tokio::test]
    async fn test_get_all_methods_empty() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let methods = registry.get_all_methods().await;
        assert!(methods.is_empty());
    }

    #[tokio::test]
    async fn test_has_method_true_and_false() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        assert!(!registry.has_method("api_key").await);

        let config = ApiKeyConfig {
            header_name: "X-API-Key".to_string(),
            api_key: "secret".to_string(),
        };
        registry.register_api_key("api_key".to_string(), config).await.unwrap();

        assert!(registry.has_method("api_key").await);
        assert!(!registry.has_method("nonexistent").await);
    }

    #[tokio::test]
    async fn test_unregister_method() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let config = ApiKeyConfig {
            header_name: "X-API-Key".to_string(),
            api_key: "secret".to_string(),
        };
        registry.register_api_key("api_key".to_string(), config).await.unwrap();
        assert!(registry.has_method("api_key").await);

        registry.unregister_method("api_key").await.unwrap();
        assert!(!registry.has_method("api_key").await);
    }

    #[tokio::test]
    async fn test_unregister_nonexistent_returns_error() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let result = registry.unregister_method("nonexistent").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_get_method_not_found_returns_error() {
        let registry = AuthMethodRegistry::new("tenant1".to_string());
        let result = registry.get_method("does_not_exist").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_register_mtls() {
        let registry = AuthMethodRegistry::new("tenant_mtls".to_string());
        let config = MtlsConfig {
            ca_cert_path: "/etc/certs/ca.pem".to_string(),
            require_client_cert: true,
            allowed_cns: Some(vec!["client.example.com".to_string()]),
        };
        registry.register_mtls("mtls".to_string(), config).await.unwrap();

        let method = registry.get_method("mtls").await.unwrap();
        assert_eq!(method.method_type, AuthMethodType::Mtls);
        assert!(method.config.enabled);
        assert_eq!(method.config.method_type, "mtls");

        // Also verify it appears in get_all_methods
        let all = registry.get_all_methods().await;
        assert_eq!(all.len(), 1);
    }
}