pub struct MagicCodeStore { /* private fields */ }Expand description
A magic-code store. Wraps a MagicCodeBackend (in-memory by default)
and applies the verify/cooldown semantics. Hydrates the in-memory
cache from the backend on construction so durable backends survive
restart without losing in-flight codes.
Implementations§
Source§impl MagicCodeStore
impl MagicCodeStore
pub fn new() -> Self
Sourcepub fn with_backend(backend: Box<dyn MagicCodeBackend>) -> Self
pub fn with_backend(backend: Box<dyn MagicCodeBackend>) -> Self
Build a magic-code store backed by a persistent backend. Existing live codes are hydrated into the in-memory cache on construction so a server restart between “send” and “verify” doesn’t kill the user’s pending login.
Sourcepub fn create(&self, email: &str) -> String
pub fn create(&self, email: &str) -> String
Generate a 6-digit code for an email and return it. Subject to a
per-email cooldown — returns the error-shape via try_create.
Sourcepub fn try_create(&self, email: &str) -> Result<String, MagicCodeError>
pub fn try_create(&self, email: &str) -> Result<String, MagicCodeError>
Create a magic code, enforcing per-email cooldown. Returns the code or an error describing why one couldn’t be issued.
Sourcepub fn verify(&self, email: &str, code: &str) -> bool
pub fn verify(&self, email: &str, code: &str) -> bool
Verify a code for an email. Returns true if valid and not expired.
Uses constant-time comparison to prevent timing attacks.
Back-compat wrapper around [try_verify].
Sourcepub fn try_verify(&self, email: &str, code: &str) -> Result<(), MagicCodeError>
pub fn try_verify(&self, email: &str, code: &str) -> Result<(), MagicCodeError>
Verify a code. Returns a typed error so callers can surface specific
messages. On the MAX_ATTEMPTS-th failure, the code is burned — even
correct subsequent attempts return TooManyAttempts.