swadhinbiswas 🔒 p53-54: eliminate hot-path unwrap() panics + add typed CoreError system · 7/26/2026
4c3cd66
24 lines · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::time::{SystemTime, UNIX_EPOCH};

use super::verify::{VerificationEntry, VerificationStore};

impl VerificationStore {
    /// Generate a numeric OTP (6 digits) and store it.
    pub async fn create_otp(&self, email: &str, purpose: &str, _ttl_secs: u64) -> String {
        use rand::Rng;
        let otp: String = format!("{:06}", rand::thread_rng().gen_range(0..1_000_000));
        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
        let mut store = self.tokens.write().await;
        store.insert(
            otp.clone(),
            VerificationEntry {
                email: email.to_string(),
                purpose: purpose.to_string(),
                created_at: now,
                used: false,
            },
        );
        otp
    }
}