class MfaService (View source)

Methods

__construct(UserMfaRepositoryInterface $userMfaRepository, ConfigurationManager $configManager, MailService $mailService, LoggerInterface|null $logger = null, UserTimezoneService|null $userTimezoneService = null)

MfaService constructor

getUserMfaRepository()

Get the user MFA repository

saveUserMfa(UserMfa $userMfa)

Save a UserMfa object

UserMfa|null
getUserMfa(int $userId)

Get user's MFA settings

UserMfa|null
getOrCreateUserMfa(int $userId)

Get or create MFA settings for a user

string
generateSecretKey(int $length = 16)

Generate a new secret key for authenticator app-based MFA

bool
isValidTotpSecret(string|null $secret)

Check if a secret key is valid for TOTP authentication

string
generateEmailVerificationCode()

Generate a simple numeric verification code for email-based MFA

enableMfa(int $userId, string $type = UserMfa::TYPE_APP)

Enable MFA for a user

UserMfa|null
disableMfa(int $userId)

Disable MFA for a user

bool
consumeRecoveryCode(int $userId, string $code)

Consume the supplied code if it is one of the user's recovery codes.

bool
verifyCode(int $userId, string $code)

Verify a TOTP code, email code, or recovery code for a user

void
invalidatePendingEmailCode(int $userId)

Burn the pending email code so it cannot be guessed any further.

string
generateQrCodeSvg(string $email, string $secret)

Generate a QR code SVG for MFA setup

string
sendEmailVerificationCode(int $userId, string $email)

Send verification code via email for email-based MFA

string|null
refreshEmailVerificationCodeIfNeeded(int $userId, string $email)

Generates a new verification code if the existing one has expired

bool
isMfaEnforced(int $userId, object $db, string|null $authMethod = null)

Check if MFA is enforced for a user

bool
isMfaSetupRequired(int $userId, object $db, string|null $authMethod = null)

Check if MFA setup is required for a user

bool
isMfaEnabled(int $userId)

Check if MFA is enabled for a user

string|null
getMfaType(int $userId)

Get MFA type for a user

array
getRecoveryCodes(int $userId)

Get recovery codes for a user

array
regenerateRecoveryCodes(int $userId)

Generate new recovery codes for a user

void
updateMfaSecretAfterLogin(int $userId, string|null $email = null)

Update MFA secret for a user after successful verification

Details

__construct(UserMfaRepositoryInterface $userMfaRepository, ConfigurationManager $configManager, MailService $mailService, LoggerInterface|null $logger = null, UserTimezoneService|null $userTimezoneService = null)

MfaService constructor

Parameters

UserMfaRepositoryInterface $userMfaRepository
ConfigurationManager $configManager
MailService $mailService
LoggerInterface|null $logger
UserTimezoneService|null $userTimezoneService

UserMfaRepositoryInterface getUserMfaRepository()

Get the user MFA repository

UserMfa saveUserMfa(UserMfa $userMfa)

Save a UserMfa object

Parameters

UserMfa $userMfa

The UserMfa object to save

Return Value

UserMfa

The saved UserMfa object

UserMfa|null getUserMfa(int $userId)

Get user's MFA settings

This method only retrieves the MFA settings and does not create a new record.

Parameters

int $userId

User ID

Return Value

UserMfa|null

MFA settings or null if not found

UserMfa|null getOrCreateUserMfa(int $userId)

Get or create MFA settings for a user

This method will create a new MFA record if one doesn't exist.

Parameters

int $userId

User ID

Return Value

UserMfa|null

MFA settings or null on error

string generateSecretKey(int $length = 16)

Generate a new secret key for authenticator app-based MFA

This method generates a proper Base32-encoded secret key that is compatible with Google Authenticator, Microsoft Authenticator, and other TOTP apps

Parameters

int $length

Length of the secret key in bytes (16 is standard)

Return Value

string

Base32-encoded secret key

Exceptions

IncompatibleWithGoogleAuthenticatorException
InvalidCharactersException
SecretKeyTooShortException

bool isValidTotpSecret(string|null $secret)

Check if a secret key is valid for TOTP authentication

This method checks if a secret key meets the requirements for use with TOTP authenticator apps (proper Base32 encoding)

Parameters

string|null $secret

The secret key to check

Return Value

bool

True if the secret is valid, false otherwise

string generateEmailVerificationCode()

Generate a simple numeric verification code for email-based MFA

Return Value

string

A 6-digit verification code

UserMfa enableMfa(int $userId, string $type = UserMfa::TYPE_APP)

Enable MFA for a user

Parameters

int $userId
string $type

Return Value

UserMfa

UserMfa|null disableMfa(int $userId)

Disable MFA for a user

Parameters

int $userId

The user ID

Return Value

UserMfa|null

The updated MFA settings or null if not found

bool consumeRecoveryCode(int $userId, string $code)

Consume the supplied code if it is one of the user's recovery codes.

A recovery code is the documented way back into a locked account, so callers may check it before any lockout gate.

Parameters

int $userId
string $code

Return Value

bool

bool verifyCode(int $userId, string $code)

Verify a TOTP code, email code, or recovery code for a user

Parameters

int $userId

The user ID

string $code

The verification code to check

Return Value

bool

True if the code is valid, false otherwise

void invalidatePendingEmailCode(int $userId)

Burn the pending email code so it cannot be guessed any further.

Called when a user trips the second-factor attempt limit: the lockout window can be configured shorter than the code lifetime, and without this the same code would still be live when the lockout lifts.

Parameters

int $userId

Return Value

void

string generateQrCodeSvg(string $email, string $secret)

Generate a QR code SVG for MFA setup

Parameters

string $email
string $secret

Return Value

string

string sendEmailVerificationCode(int $userId, string $email)

Send verification code via email for email-based MFA

Generates a new verification code, saves it to the database, and sends it via email. For email-based MFA, the secret field is used to store the most recent verification code. The code is temporary and should be regenerated for each verification attempt.

Parameters

int $userId

The user ID

string $email

The email address to send the code to

Return Value

string

The generated verification code

Exceptions

LoaderError
RuntimeError
SyntaxError

string|null refreshEmailVerificationCodeIfNeeded(int $userId, string $email)

Generates a new verification code if the existing one has expired

This is useful for generating a new code when a user tries to log in again after their previous code has expired.

Parameters

int $userId

The user ID

string $email

The user's email address

Return Value

string|null

The new code if generated, null otherwise

bool isMfaEnforced(int $userId, object $db, string|null $authMethod = null)

Check if MFA is enforced for a user

This checks:

  1. Global mfa.enabled setting (MFA feature must be available)
  2. Global mfa.enforced setting (enforcement must be enabled)
  3. mfa.skip_for_external_auth exemption for external IdP logins
  4. User or group has user_enforce_mfa permission

Parameters

int $userId

The user ID

object $db

Database connection for permission check

string|null $authMethod

How the user authenticated (session auth_used value), null if unknown

Return Value

bool

True if MFA is enforced for this user

bool isMfaSetupRequired(int $userId, object $db, string|null $authMethod = null)

Check if MFA setup is required for a user

Returns true if MFA is enforced for the user but they haven't set it up yet.

Parameters

int $userId

The user ID

object $db

Database connection

string|null $authMethod

How the user authenticated (session auth_used value), null if unknown

Return Value

bool

True if MFA setup is required

bool isMfaEnabled(int $userId)

Check if MFA is enabled for a user

Parameters

int $userId

Return Value

bool

string|null getMfaType(int $userId)

Get MFA type for a user

Parameters

int $userId

Return Value

string|null

array getRecoveryCodes(int $userId)

Get recovery codes for a user

Parameters

int $userId

Return Value

array

array regenerateRecoveryCodes(int $userId)

Generate new recovery codes for a user

Parameters

int $userId

Return Value

array

void updateMfaSecretAfterLogin(int $userId, string|null $email = null)

Update MFA secret for a user after successful verification

For email-based MFA: Generates a new verification code for security For app-based MFA: Preserves the existing secret (must not change or app codes won't work)

Parameters

int $userId

The user ID

string|null $email

User's email address (optional, for logging)

Return Value

void