Skip to main content

Overview

The MFA adapter provides hooks to customize multi-factor authentication behavior, including TOTP configuration, WebAuthn settings, and encryption of secrets.

DefaultMFAAdapter

Source: allauth/mfa/adapter.py:23 The base adapter class that can be subclassed to customize MFA functionality.

Configuration

To use a custom adapter, set MFA_ADAPTER in your settings:

Error Messages

Source: allauth/mfa/adapter.py:30 The adapter defines standard error messages that can be overridden:

TOTP Methods

get_totp_label(user)

Source: allauth/mfa/adapter.py:47 Returns the label used to represent the user in a TOTP QR code. Parameters:
  • user - User instance
Returns: str - User identifier (email, username, or string representation) Default behavior: Returns user’s email, username, or str(user) in that order of preference.

get_totp_issuer()

Source: allauth/mfa/adapter.py:64 Returns the TOTP issuer name displayed in authenticator apps. Returns: str - Issuer name Default behavior: Returns MFA_TOTP_ISSUER setting or site name.

build_totp_url(user, secret)

Source: allauth/mfa/adapter.py:73 Builds the otpauth:// URL for TOTP QR codes. Parameters:
  • user - User instance
  • secret (str) - Base32-encoded TOTP secret
Returns: str - TOTP URL in otpauth://totp/ format URL format:

build_totp_svg(url)

Source: allauth/mfa/adapter.py:88 Generates SVG QR code from TOTP URL. Parameters:
  • url (str) - TOTP URL to encode
Returns: str - SVG markup as string Requirements: Requires qrcode package with SVG support.

Encryption Methods

encrypt(text)

Source: allauth/mfa/adapter.py:105 Encrypts secrets before storing in the database. Parameters:
  • text (str) - Plain text to encrypt
Returns: str - Encrypted text Default behavior: Returns text unchanged (no encryption). Security Note: Override this method to encrypt TOTP secrets and recovery code seeds.

decrypt(encrypted_text)

Source: allauth/mfa/adapter.py:112 Decrypts secrets retrieved from the database. Parameters:
  • encrypted_text (str) - Encrypted text
Returns: str - Decrypted plain text Default behavior: Returns text unchanged.

Authorization Methods

can_delete_authenticator(authenticator)

Source: allauth/mfa/adapter.py:117 Determines if an authenticator can be deleted. Parameters:
  • authenticator (Authenticator) - The authenticator to check
Returns: bool - True if deletion is allowed Default behavior: Always returns True.

is_mfa_enabled(user, types=None)

Source: allauth/mfa/adapter.py:123 Checks if user has MFA enabled. Parameters:
  • user - User instance
  • types (Optional[List]) - List of authenticator types to check
Returns: bool - True if user has MFA enabled

WebAuthn Methods

get_public_key_credential_rp_entity()

Source: allauth/mfa/adapter.py:146 Returns the Relying Party entity for WebAuthn. Returns: Dict[str, str] - Dictionary with id and name keys Default behavior:

get_public_key_credential_user_entity(user)

Source: allauth/mfa/adapter.py:153 Returns the user entity for WebAuthn credential creation. Parameters:
  • user - User instance
Returns: dict - Dictionary with id, display_name, and name keys Default behavior:

Naming Methods

generate_authenticator_name(user, type)

Source: allauth/mfa/adapter.py:134 Generates a default name for new authenticators (primarily for WebAuthn). Parameters:
  • user - User instance
  • type (Authenticator.Type) - Type of authenticator
Returns: str - Generated name Default behavior:
  • First key: “Master key”
  • Second key: “Backup key”
  • Additional keys: “Key nr. 3”, “Key nr. 4”, etc.

Notification Methods

send_notification_mail()

Source: allauth/mfa/adapter.py:120 Sends notification emails for MFA events. Default behavior: Delegates to the account adapter’s send_notification_mail() method.

Helper Functions

get_adapter()

Source: allauth/mfa/adapter.py:161 Returns the configured MFA adapter instance. Returns: Configured DefaultMFAAdapter subclass instance

Complete Custom Adapter Example

Settings Integration