swadhinbiswas 🔒 p53-54: eliminate hot-path unwrap() panics + add typed CoreError system · 7/26/2026
a1a2d89
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
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
/// In-memory store for email verification tokens.
/// In production, use Redis or the database instead.
pub struct VerificationStore {
pub(crate) tokens: Arc<RwLock<HashMap<String, VerificationEntry>>>,
}
#[derive(Clone)]
pub(crate) struct VerificationEntry {
pub email: String,
pub purpose: String,
pub created_at: u64,
pub used: bool,
}
impl VerificationStore {
pub fn new() -> Self {
Self { tokens: Arc::new(RwLock::new(HashMap::new())) }
}
/// Generate a new verification token and store it.
pub async fn create_token(&self, email: &str, purpose: &str, _ttl_secs: u64) -> String {
use rand::Rng;
let token: String = rand::thread_rng()
.sample_iter(&rand::distributions::Alphanumeric)
.take(32)
.map(char::from)
.collect();
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
let mut store = self.tokens.write().await;
store.insert(
token.clone(),
VerificationEntry {
email: email.to_string(),
purpose: purpose.to_string(),
created_at: now,
used: false,
},
);
token
}
/// Verify a token: returns the email if valid, None otherwise.
/// Consumes the token (one-time use).
pub async fn verify(&self, token: &str, purpose: &str, max_age_secs: u64) -> Option<String> {
let mut store = self.tokens.write().await;
let mut entry = match store.get(token) {
Some(e) => e.clone(),
None => return None,
};
if entry.used {
return None;
}
if entry.purpose != purpose {
return None;
}
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
if now - entry.created_at > max_age_secs {
store.remove(token);
return None;
}
let email = entry.email.clone();
entry.used = true;
store.insert(token.to_string(), entry);
Some(email)
}
/// Clean up expired tokens from the store.
pub async fn cleanup(&self, max_age_secs: u64) {
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
let mut store = self.tokens.write().await;
store.retain(|_, entry| now - entry.created_at <= max_age_secs);
}
}
impl Default for VerificationStore {
fn default() -> Self {
Self::new()
}
}