oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! MFA Storage Implementation
//!
//! Provides persistent storage for MFA-related data including:
//! - TOTP secrets
//! - Backup codes
//! - Email addresses
//! - SMS phone numbers
//! - WebAuthn credentials

use crate::error::{FusekiError, FusekiResult};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use tokio::fs;
use tracing::{debug, info, warn};

/// MFA storage backend
pub struct MfaStorage {
    /// In-memory cache of MFA data
    cache: Arc<DashMap<String, UserMfaData>>,
    /// Optional persistent storage path
    storage_path: Option<String>,
}

/// Complete MFA data for a user
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserMfaData {
    pub username: String,
    pub totp_secret: Option<String>,
    pub backup_codes: Vec<String>,
    pub email: Option<String>,
    pub sms_phone: Option<String>,
    pub webauthn_credentials: Vec<WebAuthnCredential>,
    pub enrolled_methods: Vec<String>,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
}

/// WebAuthn credential
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebAuthnCredential {
    pub credential_id: String,
    pub public_key: String,
    pub counter: u32,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub last_used: Option<chrono::DateTime<chrono::Utc>>,
}

impl MfaStorage {
    /// Create a new MFA storage instance
    pub fn new(storage_path: Option<String>) -> Self {
        Self {
            cache: Arc::new(DashMap::new()),
            storage_path,
        }
    }

    /// Initialize storage from persistent backend
    pub async fn initialize(&self) -> FusekiResult<()> {
        if let Some(path) = &self.storage_path {
            info!("Initializing MFA storage from: {}", path);
            self.load_from_disk(path).await?;
        }
        Ok(())
    }

    /// Load MFA data from disk
    async fn load_from_disk(&self, path: &str) -> FusekiResult<()> {
        let path = Path::new(path);
        if !path.exists() {
            debug!("MFA storage file does not exist, starting with empty storage");
            return Ok(());
        }

        let content = fs::read_to_string(path)
            .await
            .map_err(|e| FusekiError::internal(format!("Failed to read MFA storage: {}", e)))?;

        let data: HashMap<String, UserMfaData> = serde_json::from_str(&content)
            .map_err(|e| FusekiError::internal(format!("Failed to parse MFA storage: {}", e)))?;

        for (username, user_data) in data {
            self.cache.insert(username, user_data);
        }

        info!("Loaded MFA data for {} users", self.cache.len());
        Ok(())
    }

    /// Save MFA data to disk
    async fn save_to_disk(&self) -> FusekiResult<()> {
        if let Some(path) = &self.storage_path {
            let data: HashMap<String, UserMfaData> = self
                .cache
                .iter()
                .map(|entry| (entry.key().clone(), entry.value().clone()))
                .collect();

            let content = serde_json::to_string_pretty(&data)
                .map_err(|e| FusekiError::internal(format!("Failed to serialize MFA data: {}", e)))?;

            fs::write(path, content)
                .await
                .map_err(|e| FusekiError::internal(format!("Failed to write MFA storage: {}", e)))?;

            debug!("Saved MFA data to disk");
        }
        Ok(())
    }

    /// Store TOTP secret for user
    pub async fn store_totp_secret(&self, username: &str, secret: &str) -> FusekiResult<()> {
        let now = chrono::Utc::now();

        self.cache
            .entry(username.to_string())
            .and_modify(|data| {
                data.totp_secret = Some(secret.to_string());
                data.updated_at = now;
                if !data.enrolled_methods.contains(&"totp".to_string()) {
                    data.enrolled_methods.push("totp".to_string());
                }
            })
            .or_insert_with(|| UserMfaData {
                username: username.to_string(),
                totp_secret: Some(secret.to_string()),
                backup_codes: Vec::new(),
                email: None,
                sms_phone: None,
                webauthn_credentials: Vec::new(),
                enrolled_methods: vec!["totp".to_string()],
                created_at: now,
                updated_at: now,
            });

        self.save_to_disk().await?;
        info!("Stored TOTP secret for user: {}", username);
        Ok(())
    }

    /// Get TOTP secret for user
    pub async fn get_totp_secret(&self, username: &str) -> FusekiResult<Option<String>> {
        Ok(self.cache.get(username).and_then(|data| data.totp_secret.clone()))
    }

    /// Store backup codes for user
    pub async fn store_backup_codes(&self, username: &str, codes: Vec<String>) -> FusekiResult<()> {
        let now = chrono::Utc::now();

        self.cache
            .entry(username.to_string())
            .and_modify(|data| {
                data.backup_codes = codes.clone();
                data.updated_at = now;
            })
            .or_insert_with(|| UserMfaData {
                username: username.to_string(),
                totp_secret: None,
                backup_codes: codes,
                email: None,
                sms_phone: None,
                webauthn_credentials: Vec::new(),
                enrolled_methods: Vec::new(),
                created_at: now,
                updated_at: now,
            });

        self.save_to_disk().await?;
        info!("Stored {} backup codes for user: {}", codes.len(), username);
        Ok(())
    }

    /// Get backup codes for user
    pub async fn get_backup_codes(&self, username: &str) -> FusekiResult<Vec<String>> {
        Ok(self
            .cache
            .get(username)
            .map(|data| data.backup_codes.clone())
            .unwrap_or_default())
    }

    /// Verify and consume a backup code
    pub async fn verify_backup_code(&self, username: &str, code: &str) -> FusekiResult<bool> {
        let mut consumed = false;

        self.cache.entry(username.to_string()).and_modify(|data| {
            if let Some(index) = data.backup_codes.iter().position(|c| c == code) {
                data.backup_codes.remove(index);
                data.updated_at = chrono::Utc::now();
                consumed = true;
            }
        });

        if consumed {
            self.save_to_disk().await?;
            info!("Consumed backup code for user: {}", username);
        }

        Ok(consumed)
    }

    /// Store email for MFA
    pub async fn store_email(&self, username: &str, email: &str) -> FusekiResult<()> {
        let now = chrono::Utc::now();

        self.cache
            .entry(username.to_string())
            .and_modify(|data| {
                data.email = Some(email.to_string());
                data.updated_at = now;
            })
            .or_insert_with(|| UserMfaData {
                username: username.to_string(),
                totp_secret: None,
                backup_codes: Vec::new(),
                email: Some(email.to_string()),
                sms_phone: None,
                webauthn_credentials: Vec::new(),
                enrolled_methods: Vec::new(),
                created_at: now,
                updated_at: now,
            });

        self.save_to_disk().await?;
        debug!("Stored email for user: {}", username);
        Ok(())
    }

    /// Get email for user
    pub async fn get_email(&self, username: &str) -> FusekiResult<Option<String>> {
        Ok(self.cache.get(username).and_then(|data| data.email.clone()))
    }

    /// Store SMS phone number
    pub async fn store_sms_phone(&self, username: &str, phone: &str) -> FusekiResult<()> {
        let now = chrono::Utc::now();

        self.cache
            .entry(username.to_string())
            .and_modify(|data| {
                data.sms_phone = Some(phone.to_string());
                data.updated_at = now;
                if !data.enrolled_methods.contains(&"sms".to_string()) {
                    data.enrolled_methods.push("sms".to_string());
                }
            })
            .or_insert_with(|| UserMfaData {
                username: username.to_string(),
                totp_secret: None,
                backup_codes: Vec::new(),
                email: None,
                sms_phone: Some(phone.to_string()),
                webauthn_credentials: Vec::new(),
                enrolled_methods: vec!["sms".to_string()],
                created_at: now,
                updated_at: now,
            });

        self.save_to_disk().await?;
        info!("Stored SMS phone for user: {}", username);
        Ok(())
    }

    /// Get SMS phone number for user
    pub async fn get_sms_phone(&self, username: &str) -> FusekiResult<Option<String>> {
        Ok(self.cache.get(username).and_then(|data| data.sms_phone.clone()))
    }

    /// Store WebAuthn credential
    pub async fn store_webauthn_credential(
        &self,
        username: &str,
        credential: WebAuthnCredential,
    ) -> FusekiResult<()> {
        let now = chrono::Utc::now();

        self.cache
            .entry(username.to_string())
            .and_modify(|data| {
                data.webauthn_credentials.push(credential.clone());
                data.updated_at = now;
                if !data.enrolled_methods.contains(&"webauthn".to_string()) {
                    data.enrolled_methods.push("webauthn".to_string());
                }
            })
            .or_insert_with(|| UserMfaData {
                username: username.to_string(),
                totp_secret: None,
                backup_codes: Vec::new(),
                email: None,
                sms_phone: None,
                webauthn_credentials: vec![credential],
                enrolled_methods: vec!["webauthn".to_string()],
                created_at: now,
                updated_at: now,
            });

        self.save_to_disk().await?;
        info!("Stored WebAuthn credential for user: {}", username);
        Ok(())
    }

    /// Get WebAuthn credentials for user
    pub async fn get_webauthn_credentials(
        &self,
        username: &str,
    ) -> FusekiResult<Vec<WebAuthnCredential>> {
        Ok(self
            .cache
            .get(username)
            .map(|data| data.webauthn_credentials.clone())
            .unwrap_or_default())
    }

    /// Get enrolled MFA methods for user
    pub async fn get_enrolled_methods(&self, username: &str) -> FusekiResult<Vec<String>> {
        Ok(self
            .cache
            .get(username)
            .map(|data| data.enrolled_methods.clone())
            .unwrap_or_default())
    }

    /// Disable MFA method for user
    pub async fn disable_method(&self, username: &str, method: &str) -> FusekiResult<()> {
        self.cache.entry(username.to_string()).and_modify(|data| {
            data.enrolled_methods.retain(|m| m != method);

            match method {
                "totp" => data.totp_secret = None,
                "sms" => data.sms_phone = None,
                "webauthn" => data.webauthn_credentials.clear(),
                _ => warn!("Unknown MFA method: {}", method),
            }

            data.updated_at = chrono::Utc::now();
        });

        self.save_to_disk().await?;
        info!("Disabled MFA method '{}' for user: {}", method, username);
        Ok(())
    }

    /// Remove all MFA data for user
    pub async fn remove_user(&self, username: &str) -> FusekiResult<()> {
        self.cache.remove(username);
        self.save_to_disk().await?;
        info!("Removed all MFA data for user: {}", username);
        Ok(())
    }

    /// Get complete MFA data for user
    pub async fn get_user_data(&self, username: &str) -> FusekiResult<Option<UserMfaData>> {
        Ok(self.cache.get(username).map(|data| data.clone()))
    }
}

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

    #[tokio::test]
    async fn test_totp_secret_storage() {
        let storage = MfaStorage::new(None);
        let username = "testuser";
        let secret = "JBSWY3DPEHPK3PXP";

        storage.store_totp_secret(username, secret).await.unwrap();
        let retrieved = storage.get_totp_secret(username).await.unwrap();
        assert_eq!(retrieved, Some(secret.to_string()));
    }

    #[tokio::test]
    async fn test_backup_codes() {
        let storage = MfaStorage::new(None);
        let username = "testuser";
        let codes = vec!["ABC123".to_string(), "DEF456".to_string()];

        storage.store_backup_codes(username, codes.clone()).await.unwrap();
        let retrieved = storage.get_backup_codes(username).await.unwrap();
        assert_eq!(retrieved, codes);

        // Test code verification and consumption
        let verified = storage.verify_backup_code(username, "ABC123").await.unwrap();
        assert!(verified);

        let remaining = storage.get_backup_codes(username).await.unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0], "DEF456");
    }

    #[tokio::test]
    async fn test_email_storage() {
        let storage = MfaStorage::new(None);
        let username = "testuser";
        let email = "test@example.com";

        storage.store_email(username, email).await.unwrap();
        let retrieved = storage.get_email(username).await.unwrap();
        assert_eq!(retrieved, Some(email.to_string()));
    }

    #[tokio::test]
    async fn test_enrolled_methods() {
        let storage = MfaStorage::new(None);
        let username = "testuser";

        storage.store_totp_secret(username, "SECRET").await.unwrap();
        storage.store_sms_phone(username, "+1234567890").await.unwrap();

        let methods = storage.get_enrolled_methods(username).await.unwrap();
        assert_eq!(methods.len(), 2);
        assert!(methods.contains(&"totp".to_string()));
        assert!(methods.contains(&"sms".to_string()));
    }
}