cedros-login-server 0.0.45

Authentication server for cedros-login with email/password, Google OAuth, and Solana wallet sign-in
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
//! TOTP (Time-based One-Time Password) repository for MFA

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::collections::HashMap;
use tokio::sync::RwLock;
use uuid::Uuid;

use crate::errors::AppError;
use crate::services::TotpService;

/// TOTP secret entity
#[derive(Debug, Clone)]
pub struct TotpSecret {
    pub id: Uuid,
    pub user_id: Uuid,
    /// Base32-encoded TOTP secret
    pub secret: String,
    /// Whether MFA is enabled for this user
    pub enabled: bool,
    /// When the secret was created
    pub created_at: DateTime<Utc>,
    /// When MFA was enabled (None if not yet enabled)
    pub enabled_at: Option<DateTime<Utc>>,
    /// S-14: Last used time step (Unix time / 30) for replay protection
    pub last_used_time_step: Option<i64>,
}

/// Recovery code entity
#[derive(Debug, Clone)]
pub struct RecoveryCode {
    pub id: Uuid,
    pub user_id: Uuid,
    /// Argon2id hash of the recovery code
    pub code_hash: String,
    /// Whether this code has been used
    pub used: bool,
    /// When it was created
    pub created_at: DateTime<Utc>,
    /// When it was used (if applicable)
    pub used_at: Option<DateTime<Utc>>,
}

/// TOTP repository trait
#[async_trait]
pub trait TotpRepository: Send + Sync {
    /// Create or update TOTP secret for a user
    async fn upsert_secret(&self, user_id: Uuid, secret: &str) -> Result<TotpSecret, AppError>;

    /// Get TOTP secret for a user
    async fn find_by_user(&self, user_id: Uuid) -> Result<Option<TotpSecret>, AppError>;

    /// Enable MFA for a user (marks the secret as enabled)
    async fn enable_mfa(&self, user_id: Uuid) -> Result<(), AppError>;

    /// Disable MFA for a user (removes the secret)
    async fn disable_mfa(&self, user_id: Uuid) -> Result<(), AppError>;

    /// Check if user has MFA enabled
    async fn has_mfa_enabled(&self, user_id: Uuid) -> Result<bool, AppError>;

    /// S-14: Record the time step of a successfully verified TOTP code.
    /// Returns true only if the time step is newer than any previously recorded value.
    async fn record_used_time_step_if_newer(
        &self,
        user_id: Uuid,
        time_step: i64,
    ) -> Result<bool, AppError>;

    /// Store recovery codes for a user (replaces existing)
    async fn store_recovery_codes(
        &self,
        user_id: Uuid,
        code_hashes: Vec<String>,
    ) -> Result<(), AppError>;

    /// Get unused recovery codes for a user
    async fn get_recovery_codes(&self, user_id: Uuid) -> Result<Vec<RecoveryCode>, AppError>;

    /// Use a recovery code (verifies against stored hashes and marks as used)
    ///
    /// # Arguments
    /// * `user_id` - The user attempting to use a recovery code
    /// * `code` - The plaintext recovery code to verify (not the hash)
    ///
    /// # Returns
    /// * `Ok(true)` if the code was valid and has been marked as used
    /// * `Ok(false)` if the code was invalid or already used
    async fn use_recovery_code(&self, user_id: Uuid, code: &str) -> Result<bool, AppError>;

    /// Delete all recovery codes for a user
    async fn delete_recovery_codes(&self, user_id: Uuid) -> Result<(), AppError>;
}

/// In-memory TOTP repository
pub struct InMemoryTotpRepository {
    secrets: RwLock<HashMap<Uuid, TotpSecret>>,
    recovery_codes: RwLock<HashMap<Uuid, Vec<RecoveryCode>>>,
}

impl InMemoryTotpRepository {
    pub fn new() -> Self {
        Self {
            secrets: RwLock::new(HashMap::new()),
            recovery_codes: RwLock::new(HashMap::new()),
        }
    }
}

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

#[async_trait]
impl TotpRepository for InMemoryTotpRepository {
    async fn upsert_secret(&self, user_id: Uuid, secret: &str) -> Result<TotpSecret, AppError> {
        let mut secrets = self.secrets.write().await;

        let totp_secret = TotpSecret {
            id: Uuid::new_v4(),
            user_id,
            secret: secret.to_string(),
            enabled: false,
            created_at: Utc::now(),
            enabled_at: None,
            last_used_time_step: None,
        };

        secrets.insert(user_id, totp_secret.clone());
        Ok(totp_secret)
    }

    async fn find_by_user(&self, user_id: Uuid) -> Result<Option<TotpSecret>, AppError> {
        let secrets = self.secrets.read().await;

        Ok(secrets.get(&user_id).cloned())
    }

    async fn enable_mfa(&self, user_id: Uuid) -> Result<(), AppError> {
        let mut secrets = self.secrets.write().await;

        if let Some(secret) = secrets.get_mut(&user_id) {
            secret.enabled = true;
            secret.enabled_at = Some(Utc::now());
            Ok(())
        } else {
            Err(AppError::NotFound("TOTP secret not found".into()))
        }
    }

    async fn disable_mfa(&self, user_id: Uuid) -> Result<(), AppError> {
        let mut secrets = self.secrets.write().await;

        secrets.remove(&user_id);

        // Also remove recovery codes
        let mut codes = self.recovery_codes.write().await;
        codes.remove(&user_id);

        Ok(())
    }

    async fn has_mfa_enabled(&self, user_id: Uuid) -> Result<bool, AppError> {
        let secrets = self.secrets.read().await;

        Ok(secrets.get(&user_id).map(|s| s.enabled).unwrap_or(false))
    }

    async fn record_used_time_step_if_newer(
        &self,
        user_id: Uuid,
        time_step: i64,
    ) -> Result<bool, AppError> {
        let mut secrets = self.secrets.write().await;

        if let Some(secret) = secrets.get_mut(&user_id) {
            let should_update = secret
                .last_used_time_step
                .map(|last| time_step > last)
                .unwrap_or(true);
            if should_update {
                secret.last_used_time_step = Some(time_step);
            }
            Ok(should_update)
        } else {
            Err(AppError::NotFound("TOTP secret not found".into()))
        }
    }

    async fn store_recovery_codes(
        &self,
        user_id: Uuid,
        code_hashes: Vec<String>,
    ) -> Result<(), AppError> {
        let mut codes = self.recovery_codes.write().await;

        let now = Utc::now();
        let recovery_codes: Vec<RecoveryCode> = code_hashes
            .into_iter()
            .map(|hash| RecoveryCode {
                id: Uuid::new_v4(),
                user_id,
                code_hash: hash,
                used: false,
                created_at: now,
                used_at: None,
            })
            .collect();

        codes.insert(user_id, recovery_codes);
        Ok(())
    }

    async fn get_recovery_codes(&self, user_id: Uuid) -> Result<Vec<RecoveryCode>, AppError> {
        let codes = self.recovery_codes.read().await;

        Ok(codes
            .get(&user_id)
            .map(|c| c.iter().filter(|code| !code.used).cloned().collect())
            .unwrap_or_default())
    }

    async fn use_recovery_code(&self, user_id: Uuid, code: &str) -> Result<bool, AppError> {
        let mut codes = self.recovery_codes.write().await;

        if let Some(user_codes) = codes.get_mut(&user_id) {
            // Verify the plaintext code against each stored Argon2id hash.
            // Argon2 verification is already constant-time.
            // We check all unused codes to maintain consistent timing.
            let mut matched_idx: Option<usize> = None;
            for (idx, stored_code) in user_codes.iter().enumerate() {
                if !stored_code.used {
                    // Use Argon2id verification (constant-time)
                    if TotpService::verify_recovery_code(code, &stored_code.code_hash) {
                        matched_idx = Some(idx);
                        // Don't break early - continue checking to maintain constant time
                    }
                }
            }

            if let Some(idx) = matched_idx {
                user_codes[idx].used = true;
                user_codes[idx].used_at = Some(Utc::now());
                return Ok(true);
            }
        }

        Ok(false)
    }

    async fn delete_recovery_codes(&self, user_id: Uuid) -> Result<(), AppError> {
        let mut codes = self.recovery_codes.write().await;

        codes.remove(&user_id);
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_create_and_enable_mfa() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        // Create secret
        let secret = repo
            .upsert_secret(user_id, "JBSWY3DPEHPK3PXP")
            .await
            .unwrap();
        assert!(!secret.enabled);

        // Check not enabled yet
        assert!(!repo.has_mfa_enabled(user_id).await.unwrap());

        // Enable MFA
        repo.enable_mfa(user_id).await.unwrap();
        assert!(repo.has_mfa_enabled(user_id).await.unwrap());
    }

    #[tokio::test]
    async fn test_disable_mfa() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        repo.upsert_secret(user_id, "JBSWY3DPEHPK3PXP")
            .await
            .unwrap();
        repo.enable_mfa(user_id).await.unwrap();
        assert!(repo.has_mfa_enabled(user_id).await.unwrap());

        repo.disable_mfa(user_id).await.unwrap();
        assert!(!repo.has_mfa_enabled(user_id).await.unwrap());
    }

    #[tokio::test]
    async fn test_recovery_codes() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        // Create Argon2id hashes for test codes
        let code1 = "ABCD-1234";
        let code2 = "EFGH-5678";
        let code3 = "IJKL-9012";
        let hashes = vec![
            TotpService::hash_recovery_code(code1).unwrap(),
            TotpService::hash_recovery_code(code2).unwrap(),
            TotpService::hash_recovery_code(code3).unwrap(),
        ];
        repo.store_recovery_codes(user_id, hashes).await.unwrap();

        let codes = repo.get_recovery_codes(user_id).await.unwrap();
        assert_eq!(codes.len(), 3);

        // Use one code with plaintext
        assert!(repo.use_recovery_code(user_id, code1).await.unwrap());
        assert!(!repo.use_recovery_code(user_id, code1).await.unwrap()); // Can't reuse

        let codes = repo.get_recovery_codes(user_id).await.unwrap();
        assert_eq!(codes.len(), 2);

        // Wrong code should fail
        assert!(!repo.use_recovery_code(user_id, "WRONG-CODE").await.unwrap());
    }

    #[tokio::test]
    async fn test_recovery_code_case_insensitive() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        // Store hash for uppercase code
        let code = "ABCD-1234";
        let hashes = vec![TotpService::hash_recovery_code(code).unwrap()];
        repo.store_recovery_codes(user_id, hashes).await.unwrap();

        // Verify works with different case/format
        assert!(repo.use_recovery_code(user_id, "abcd-1234").await.unwrap());
    }

    #[tokio::test]
    async fn test_disable_mfa_removes_recovery_codes() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        repo.upsert_secret(user_id, "JBSWY3DPEHPK3PXP")
            .await
            .unwrap();
        let hashes = vec![TotpService::hash_recovery_code("ABCD-1234").unwrap()];
        repo.store_recovery_codes(user_id, hashes).await.unwrap();

        assert_eq!(repo.get_recovery_codes(user_id).await.unwrap().len(), 1);

        repo.disable_mfa(user_id).await.unwrap();
        assert_eq!(repo.get_recovery_codes(user_id).await.unwrap().len(), 0);
    }

    #[tokio::test]
    async fn test_record_used_time_step_if_newer() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        repo.upsert_secret(user_id, "JBSWY3DPEHPK3PXP")
            .await
            .unwrap();

        assert!(repo
            .record_used_time_step_if_newer(user_id, 100)
            .await
            .unwrap());
        assert!(!repo
            .record_used_time_step_if_newer(user_id, 100)
            .await
            .unwrap());
        assert!(!repo
            .record_used_time_step_if_newer(user_id, 99)
            .await
            .unwrap());
        assert!(repo
            .record_used_time_step_if_newer(user_id, 101)
            .await
            .unwrap());
    }

    #[tokio::test]
    async fn test_record_used_time_step_if_newer_concurrent() {
        let repo = InMemoryTotpRepository::new();
        let user_id = Uuid::new_v4();

        repo.upsert_secret(user_id, "JBSWY3DPEHPK3PXP")
            .await
            .unwrap();

        let (first, second) = tokio::join!(
            repo.record_used_time_step_if_newer(user_id, 200),
            repo.record_used_time_step_if_newer(user_id, 200)
        );

        let first = first.unwrap();
        let second = second.unwrap();
        assert_ne!(first, second);
    }
}