rustchain 0.1.0

Workflow transpilation and execution framework - import LangChain, Airflow, GitHub Actions, and more
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
use crate::security::SecurityConfig;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

/// Authentication credentials
#[derive(Debug, Clone)]
pub enum Credentials {
    UsernamePassword {
        username: String,
        password: String,
        ip_address: Option<String>,
    },
    JwtToken {
        token: String,
        ip_address: Option<String>,
    },
    ApiKey {
        key: String,
        ip_address: Option<String>,
    },
    OAuth2 {
        provider: String,
        access_token: String,
        ip_address: Option<String>,
    },
}

impl Credentials {
    pub fn user_id(&self) -> &str {
        match self {
            Credentials::UsernamePassword { username, .. } => username,
            Credentials::JwtToken { .. } => "jwt_user",
            Credentials::ApiKey { .. } => "api_user",
            Credentials::OAuth2 { .. } => "oauth_user",
        }
    }

    pub fn auth_method(&self) -> &str {
        match self {
            Credentials::UsernamePassword { .. } => "username_password",
            Credentials::JwtToken { .. } => "jwt",
            Credentials::ApiKey { .. } => "api_key",
            Credentials::OAuth2 { .. } => "oauth2",
        }
    }

    pub fn ip_address(&self) -> Option<String> {
        match self {
            Credentials::UsernamePassword { ip_address, .. } => ip_address.clone(),
            Credentials::JwtToken { ip_address, .. } => ip_address.clone(),
            Credentials::ApiKey { ip_address, .. } => ip_address.clone(),
            Credentials::OAuth2 { ip_address, .. } => ip_address.clone(),
        }
    }
}

/// Authentication result
#[derive(Debug, Clone)]
pub struct AuthenticationResult {
    pub user_id: String,
    pub tenant_id: Option<String>,
    pub permissions: Vec<String>,
    pub roles: Vec<String>,
    pub expires_at: Option<DateTime<Utc>>,
    pub metadata: HashMap<String, String>,
}

/// JWT claims structure
#[derive(Debug, Serialize, Deserialize)]
pub struct JwtClaims {
    pub sub: String, // Subject (user ID)
    pub exp: i64,    // Expiration time
    pub iat: i64,    // Issued at
    pub iss: String, // Issuer
    pub aud: String, // Audience
    pub tenant_id: Option<String>,
    pub permissions: Vec<String>,
    pub roles: Vec<String>,
}

/// Authentication service trait
#[async_trait]
pub trait AuthenticationService: Send + Sync {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> crate::core::error::Result<AuthenticationResult>;
    async fn validate_token(&self, token: &str)
        -> crate::core::error::Result<AuthenticationResult>;
    async fn create_token(
        &self,
        user_id: &str,
        permissions: Vec<String>,
    ) -> crate::core::error::Result<String>;
    async fn revoke_token(&self, token: &str) -> crate::core::error::Result<()>;
}

/// JWT-based authentication service
pub struct JwtAuthenticationService {
    config: Arc<SecurityConfig>,
    secret: Vec<u8>,
}

impl JwtAuthenticationService {
    pub fn new(config: Arc<SecurityConfig>) -> crate::core::error::Result<Self> {
        let secret = config
            .jwt_secret
            .as_ref()
            .ok_or_else(|| {
                crate::core::error::RustChainError::Security(
                    "JWT secret not configured".to_string(),
                )
            })?
            .as_bytes()
            .to_vec();

        Ok(Self { config, secret })
    }

    fn _generate_secret() -> String {
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(uuid::Uuid::new_v4().to_string());
        hex::encode(hasher.finalize())
    }
}

#[async_trait]
impl AuthenticationService for JwtAuthenticationService {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> crate::core::error::Result<AuthenticationResult> {
        match credentials {
            Credentials::UsernamePassword {
                username, password, ..
            } => {
                #[cfg(feature = "enterprise")]
                {
                    // Enterprise edition: implement basic authentication
                    let admin_user = std::env::var("RUSTCHAIN_ADMIN_USER")
                        .unwrap_or_else(|_| "admin".to_string());
                    let admin_pass = std::env::var("RUSTCHAIN_ADMIN_PASS")
                        .unwrap_or_else(|_| "change_me_immediately".to_string());

                    if *username == admin_user && *password == admin_pass {
                        Ok(AuthenticationResult {
                            user_id: username.clone(),
                            tenant_id: Some("default".to_string()),
                            permissions: vec![
                                "admin:system".to_string(),
                                "read:missions".to_string(),
                                "write:missions".to_string(),
                            ],
                            roles: vec!["admin".to_string()],
                            expires_at: Some(Utc::now() + chrono::Duration::hours(1)),
                            metadata: HashMap::new(),
                        })
                    } else {
                        Err(crate::core::error::RustChainError::Security(
                            "Invalid credentials".to_string(),
                        ))
                    }
                }

                #[cfg(not(feature = "enterprise"))]
                {
                    // Community edition: authentication not implemented
                    Err(crate::core::error::RustChainError::Security(
                        "Authentication requires enterprise features. Use --features enterprise to enable.".to_string()
                    ))
                }
            }

            Credentials::JwtToken { token, .. } => self.validate_token(token).await,

            Credentials::ApiKey { key, .. } => {
                #[cfg(feature = "enterprise")]
                {
                    // Enterprise edition: implement basic API key authentication
                    let expected_key = std::env::var("RUSTCHAIN_API_KEY")
                        .unwrap_or_else(|_| "change_me_api_key".to_string());

                    if *key == expected_key {
                        Ok(AuthenticationResult {
                            user_id: "api_client".to_string(),
                            tenant_id: Some("default".to_string()),
                            permissions: vec!["read:missions".to_string()],
                            roles: vec!["client".to_string()],
                            expires_at: Some(Utc::now() + chrono::Duration::hours(1)),
                            metadata: HashMap::new(),
                        })
                    } else {
                        Err(crate::core::error::RustChainError::Security(
                            "Invalid API key".to_string(),
                        ))
                    }
                }

                #[cfg(not(feature = "enterprise"))]
                {
                    // Community edition: API key auth not implemented
                    Err(crate::core::error::RustChainError::Security(
                        "API key authentication requires enterprise features. Use --features enterprise to enable.".to_string()
                    ))
                }
            }

            Credentials::OAuth2 {
                provider,
                access_token: _access_token,
                ..
            } => {
                // Community edition: OAuth not implemented
                Err(crate::core::error::RustChainError::Security(
                    format!("OAuth2 authentication for {} requires enterprise features. Use --features enterprise to enable.", provider)
                ))
            }
        }
    }

    async fn validate_token(
        &self,
        token: &str,
    ) -> crate::core::error::Result<AuthenticationResult> {
        #[cfg(feature = "enterprise")]
        {
            use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};

            let validation = Validation::new(Algorithm::HS256);
            let token_data =
                decode::<JwtClaims>(token, &DecodingKey::from_secret(&self.secret), &validation)
                    .map_err(|e| {
                        crate::core::error::RustChainError::Security(format!(
                            "Invalid JWT token: {}",
                            e
                        ))
                    })?;

            let claims = token_data.claims;

            // Check expiration
            if claims.exp < Utc::now().timestamp() {
                return Err(crate::core::error::RustChainError::Security(
                    "Token expired".to_string(),
                ));
            }

            Ok(AuthenticationResult {
                user_id: claims.sub,
                tenant_id: claims.tenant_id,
                permissions: claims.permissions,
                roles: claims.roles,
                expires_at: Some(DateTime::from_timestamp(claims.exp, 0).unwrap_or_else(Utc::now)),
                metadata: HashMap::new(),
            })
        }

        #[cfg(not(feature = "enterprise"))]
        {
            Err(crate::core::error::RustChainError::Security(
                "JWT validation requires enterprise features".to_string(),
            ))
        }
    }

    async fn create_token(
        &self,
        user_id: &str,
        permissions: Vec<String>,
    ) -> crate::core::error::Result<String> {
        #[cfg(feature = "enterprise")]
        {
            use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};

            let now = Utc::now();
            let expires_at =
                now + chrono::Duration::minutes(self.config.session_timeout_minutes as i64);

            let claims = JwtClaims {
                sub: user_id.to_string(),
                exp: expires_at.timestamp(),
                iat: now.timestamp(),
                iss: "rustchain".to_string(),
                aud: "rustchain-client".to_string(),
                tenant_id: Some("default".to_string()),
                permissions,
                roles: vec!["user".to_string()],
            };

            let header = Header::new(Algorithm::HS256);
            let token =
                encode(&header, &claims, &EncodingKey::from_secret(&self.secret)).map_err(|e| {
                    crate::core::error::RustChainError::Security(format!(
                        "Failed to create token: {}",
                        e
                    ))
                })?;

            Ok(token)
        }

        #[cfg(not(feature = "enterprise"))]
        {
            Err(crate::core::error::RustChainError::Security(
                "JWT token creation requires enterprise features".to_string(),
            ))
        }
    }

    async fn revoke_token(&self, _token: &str) -> crate::core::error::Result<()> {
        // Community edition: token revocation not implemented
        Err(crate::core::error::RustChainError::Security(
            "Token revocation requires enterprise features. Use --features enterprise to enable."
                .to_string(),
        ))
    }
}

/// Multi-factor authentication service
pub struct MfaAuthenticationService {
    base_service: Arc<dyn AuthenticationService>,
}

impl MfaAuthenticationService {
    pub fn new(base_service: Arc<dyn AuthenticationService>) -> Self {
        Self { base_service }
    }

    pub async fn verify_totp(
        &self,
        _user_id: &str,
        _code: &str,
    ) -> crate::core::error::Result<bool> {
        // Community edition: TOTP verification not implemented
        Err(crate::core::error::RustChainError::Security(
            "TOTP verification requires enterprise features. Use --features enterprise to enable."
                .to_string(),
        ))
    }

    pub async fn send_sms_code(
        &self,
        _user_id: &str,
        _phone: &str,
    ) -> crate::core::error::Result<String> {
        // Community edition: SMS sending not implemented
        Err(crate::core::error::RustChainError::Security(
            "SMS code sending requires enterprise features. Use --features enterprise to enable."
                .to_string(),
        ))
    }
}

#[async_trait]
impl AuthenticationService for MfaAuthenticationService {
    async fn authenticate(
        &self,
        credentials: &Credentials,
    ) -> crate::core::error::Result<AuthenticationResult> {
        // First, authenticate with the base service
        let mut result = self.base_service.authenticate(credentials).await?;

        // Add MFA requirement to metadata
        result
            .metadata
            .insert("mfa_required".to_string(), "true".to_string());
        result
            .metadata
            .insert("mfa_methods".to_string(), "totp,sms".to_string());

        Ok(result)
    }

    async fn validate_token(
        &self,
        token: &str,
    ) -> crate::core::error::Result<AuthenticationResult> {
        self.base_service.validate_token(token).await
    }

    async fn create_token(
        &self,
        user_id: &str,
        permissions: Vec<String>,
    ) -> crate::core::error::Result<String> {
        self.base_service.create_token(user_id, permissions).await
    }

    async fn revoke_token(&self, token: &str) -> crate::core::error::Result<()> {
        self.base_service.revoke_token(token).await
    }
}

#[cfg(all(test, feature = "enterprise"))]
mod tests {
    use super::*;

    fn set_auth_env() {
        std::env::set_var("RUSTCHAIN_ADMIN_USER", "admin");
        std::env::set_var("RUSTCHAIN_ADMIN_PASS", "admin123");
        std::env::set_var("RUSTCHAIN_API_KEY", "rustchain_api_key_12345");
    }

    #[tokio::test]
    async fn test_username_password_auth() {
        set_auth_env();
        let config = Arc::new(SecurityConfig {
            jwt_secret: Some("test_secret_key_12345".to_string()),
            ..Default::default()
        });

        let auth_service = JwtAuthenticationService::new(config).unwrap();

        let credentials = Credentials::UsernamePassword {
            username: "admin".to_string(),
            password: "admin123".to_string(),
            ip_address: Some("127.0.0.1".to_string()),
        };

        let result = auth_service.authenticate(&credentials).await.unwrap();

        assert_eq!(result.user_id, "admin");
        assert!(result.permissions.contains(&"admin:system".to_string()));
    }

    #[tokio::test]
    async fn test_invalid_credentials() {
        set_auth_env();
        let config = Arc::new(SecurityConfig {
            jwt_secret: Some("test_secret_key_12345".to_string()),
            ..Default::default()
        });

        let auth_service = JwtAuthenticationService::new(config).unwrap();

        let credentials = Credentials::UsernamePassword {
            username: "admin".to_string(),
            password: "wrong_password".to_string(),
            ip_address: Some("127.0.0.1".to_string()),
        };

        let result = auth_service.authenticate(&credentials).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_api_key_auth() {
        set_auth_env();
        let config = Arc::new(SecurityConfig {
            jwt_secret: Some("test_secret_key_12345".to_string()),
            ..Default::default()
        });

        let auth_service = JwtAuthenticationService::new(config).unwrap();

        let credentials = Credentials::ApiKey {
            key: "rustchain_api_key_12345".to_string(),
            ip_address: Some("127.0.0.1".to_string()),
        };

        let result = auth_service.authenticate(&credentials).await.unwrap();

        assert_eq!(result.user_id, "api_client");
        assert!(result.permissions.contains(&"read:missions".to_string()));
    }
}