Skip to main content

MfaManager

Struct MfaManager 

Source
pub struct MfaManager {
    pub totp: TotpManager,
    pub sms: SmsKitManager,
    pub email: EmailManager,
    pub backup_codes: BackupCodesManager,
    /* private fields */
}
Expand description

Centralized multi-factor authentication (MFA) manager.

MfaManager coordinates all MFA operations across different authentication factors including TOTP, SMS, email, and backup codes. It provides a unified interface for MFA setup, challenge generation, and verification while supporting multiple MFA methods simultaneously.

§Supported MFA Methods

  • TOTP (Time-based OTP): RFC 6238 compliant authenticator apps
  • SMS: Text message-based verification codes
  • Email: Email-based verification codes
  • Backup Codes: Single-use recovery codes

§Multi-Method Support

Users can enable multiple MFA methods simultaneously, providing flexibility and redundancy. The manager handles method coordination and fallback scenarios.

§Security Features

  • Challenge Expiration: Time-limited challenges prevent replay attacks
  • Rate Limiting: Prevents brute force attacks on MFA codes
  • Secure Code Generation: Cryptographically secure random code generation
  • Method Validation: Validates MFA setup before enabling
  • Audit Logging: Comprehensive logging of all MFA operations

§Cross-Method Operations

The manager supports advanced scenarios like:

  • Method fallback when primary method fails
  • Cross-method challenge validation
  • Method strength assessment
  • Risk-based MFA requirements

§Example

use auth_framework::auth_modular::mfa::MfaManager;
use std::sync::Arc;

// Create MFA manager with storage backend
let mfa_manager = MfaManager::new(storage);

// Generate a TOTP secret for a user (store it and show QR code to the user)
let secret = mfa_manager.totp.generate_secret("user123").await?;

// Verify a TOTP code during an authentication attempt
let is_valid = mfa_manager.totp.verify_code("user123", "123456").await?;

§Thread Safety

The MFA manager is designed for concurrent use and safely coordinates access to underlying MFA method implementations.

§Storage Integration

Integrates with the framework’s storage system to persist:

  • User MFA method configurations
  • Active challenges
  • Usage statistics and audit logs
  • Backup codes and secrets

Fields§

§totp: TotpManager

TOTP manager

§sms: SmsKitManager

SMS manager (using SMSKit)

§email: EmailManager

Email manager

§backup_codes: BackupCodesManager

Backup codes manager

Implementations§

Source§

impl MfaManager

Source

pub fn new(storage: Arc<dyn AuthStorage>) -> Self

Create a new MFA manager

Source

pub fn new_with_smskit_config( storage: Arc<dyn AuthStorage>, smskit_config: SmsKitConfig, ) -> Result<Self>

Create a new MFA manager with SMSKit configuration

Source

pub async fn store_challenge(&self, challenge: MfaChallenge) -> Result<()>

Store an MFA challenge

Source

pub async fn guard_and_store(&self, challenge: MfaChallenge) -> Result<()>

Guard the global challenge budget and store the challenge.

Returns an error if more than 10 000 challenges are pending. A user also keeps at most MAX_PENDING_PER_USER pending challenges: storing a new one retires that user’s oldest, so minting challenges cannot multiply the guesses available for one account.

Source

pub async fn get_challenge( &self, challenge_id: &str, ) -> Result<Option<MfaChallenge>>

Get an MFA challenge

Source

pub async fn begin_attempt( &self, challenge_id: &str, ) -> Result<Option<MfaChallenge>>

Spend one verification attempt on a challenge, atomically.

Returns the stored challenge if it exists, has not expired and still has an attempt left (the attempt is counted BEFORE the code is checked, so concurrent guesses are all counted). Otherwise the challenge is removed and None is returned.

Source

pub async fn remove_challenge(&self, challenge_id: &str) -> Result<()>

Remove an MFA challenge

Source

pub async fn cleanup_expired_challenges(&self) -> Result<()>

Clean up expired challenges

Source

pub async fn get_active_challenge_count(&self) -> usize

Get count of active challenges

Source

pub async fn verify_challenge_code( &self, challenge: &MfaChallenge, code: &str, ) -> Result<bool>

Verify a code against the given MFA challenge, dispatching to the appropriate sub-manager.

Source

pub async fn initiate_step_up_authentication( &self, user_id: &str, required_methods: &[MfaMethod], risk_level: RiskLevel, ) -> Result<CrossMethodChallenge>

MFA CROSS-METHOD OPERATIONS: Step-up authentication with multiple factors

Source

pub async fn complete_cross_method_step( &self, challenge_id: &str, method: MfaMethod, response: &str, ) -> Result<CrossMethodCompletionResult>

Complete a specific method within a cross-method challenge

Source

pub async fn get_available_methods( &self, user_id: &str, ) -> Result<Vec<MfaMethod>>

Get available MFA methods for a user

Source

pub async fn perform_method_fallback( &self, user_id: &str, failed_method: MfaMethod, fallback_order: &[MfaMethod], ) -> Result<MethodFallbackResult>

Perform method fallback when primary method fails

Source

pub async fn generate_emergency_bypass_token( &self, admin_user_id: &str, target_user_id: &str, lifetime: Duration, ) -> Result<String>

Generate a signed emergency MFA bypass token for a specific target user.

Source

pub async fn emergency_mfa_bypass( &self, user_id: &str, admin_token: &str, ) -> Result<bool>

Emergency MFA bypass using direct storage access This method provides a way to recover when all MFA methods fail

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more