Skip to main content

Overview

The MFA models provide the database layer for storing multi-factor authentication data, including TOTP secrets, WebAuthn credentials, and recovery codes.

Models

Authenticator

Source: allauth/mfa/models.py:22 The main model for storing MFA authenticators associated with user accounts.

Fields

  • user (ForeignKey) - Reference to the user model
  • type (CharField) - Type of authenticator (see Authenticator.Type)
  • data (JSONField) - Authenticator-specific data (secrets, credentials, etc.)
  • created_at (DateTimeField) - Timestamp when authenticator was created
  • last_used_at (DateTimeField) - Timestamp of last use (nullable)

Type Choices

Source: allauth/mfa/models.py:23

Methods

wrap()
Source: allauth/mfa/models.py:55 Wraps the authenticator instance in a type-specific wrapper class. Returns: Type-specific wrapper instance (TOTP, RecoveryCodes, or WebAuthn)
record_usage()
Source: allauth/mfa/models.py:66 Records when the authenticator was last used by updating last_used_at.

Constraints

The model enforces a unique constraint ensuring users can only have one TOTP authenticator and one recovery codes authenticator. Multiple WebAuthn authenticators are allowed. Source: allauth/mfa/models.py:37

Wrapper Classes

The Authenticator model stores generic data, but each type has a specialized wrapper class that provides type-specific functionality.

TOTP

Source: allauth/mfa/totp/internal/auth.py:78 Wrapper for Time-based One-Time Password authenticators.

Class Methods

activate(user, secret)
Source: allauth/mfa/totp/internal/auth.py:83 Creates and saves a new TOTP authenticator for a user. Parameters:
  • user - User instance
  • secret (str) - Base32-encoded TOTP secret
Returns: TOTP instance

Instance Methods

validate_code(code)
Source: allauth/mfa/totp/internal/auth.py:90 Validates a TOTP code, checking against the current time window and preventing replay attacks. Parameters:
  • code (str) - The TOTP code to validate
Returns: bool - True if code is valid

RecoveryCodes

Source: allauth/mfa/recovery_codes/internal/auth.py:11 Wrapper for recovery code authenticators.

Class Methods

activate(user)
Source: allauth/mfa/recovery_codes/internal/auth.py:16 Creates or retrieves recovery codes authenticator for a user. Parameters:
  • user - User instance
Returns: RecoveryCodes instance
generate_seed()
Source: allauth/mfa/recovery_codes/internal/auth.py:34 Generates a random seed for recovery code generation. Returns: str - 80-character hex string

Instance Methods

generate_codes()
Source: allauth/mfa/recovery_codes/internal/auth.py:44 Generates all recovery codes from the stored seed. Returns: List[str] - List of recovery codes
get_unused_codes()
Source: allauth/mfa/recovery_codes/internal/auth.py:73 Returns only the codes that haven’t been used yet. Returns: List[str] - List of unused recovery codes
validate_code(code)
Source: allauth/mfa/recovery_codes/internal/auth.py:101 Validates a recovery code and marks it as used. Parameters:
  • code (str) - Recovery code to validate
Returns: bool - True if code is valid and unused

WebAuthn

Source: allauth/mfa/webauthn/internal/auth.py:180 Wrapper for WebAuthn/FIDO2 authenticators (security keys, biometrics).

Class Methods

add(user, name, credential)
Source: allauth/mfa/webauthn/internal/auth.py:185 Creates a new WebAuthn authenticator. Parameters:
  • user - User instance
  • name (str) - User-friendly name for the authenticator
  • credential (dict) - Credential data from registration ceremony
Returns: WebAuthn instance

Properties

name
Source: allauth/mfa/webauthn/internal/auth.py:198 Gets or sets the user-friendly name of the authenticator.
authenticator_data
Source: allauth/mfa/webauthn/internal/auth.py:206 Returns the parsed AuthenticatorData from the credential. Returns: fido2.webauthn.AuthenticatorData
is_passwordless
Source: allauth/mfa/webauthn/internal/auth.py:212 Checks if this is a passwordless/resident key credential. Returns: Optional[bool] - True if passwordless, False if not, None if unknown

Manager

AuthenticatorManager

Source: allauth/mfa/models.py:18 The default manager for Authenticator model. Currently provides standard Django manager functionality.

Usage Examples

Checking if User Has MFA Enabled

Getting All WebAuthn Keys

Validating TOTP Code

Data Structure

TOTP Data Format

Recovery Codes Data Format

WebAuthn Data Format