MMOframework
Why AI Playground Libraries Developers Agency
FR EN
Build a project live
Index

Mfa

MMO\oauth\helpers · 1 symbol · generated 30 Nov 2026

class

Mfa

Mfa.php:18

The second factor of a login by password: a TOTP (Totp) enrolled from a QR code, and recovery codes for a phone lost (ISO 27001 A.8.5, A.5.17). What a user keeps (mfa, tUser::$mfa): its secret encrypted with $keyFile's key, the step of the last code accepted (a code is accepted once), the recovery codes hashed with that key, each used once. AccessController::$mfa gives its options and asks it at the login

final class MMO\oauth\helpers\Mfa
Namespace
MMO\oauth\helpers
$mfa = new Mfa(['required' => ['admin', 'sudo'], 'keyFile' => '{configs}/mfa.key']); if ($mfa->applies($user) && !$mfa->verify($user, $_POST['code'])) {...}

Index

issuer
key
keyFile
pendingTTL
recoveryCodes
required
window
applies($user)
enrol($user, $secret, $code)Enrol $secret as $user's second factor, if $code is one of it: the user's app has it
hashRecovery($code)
isEnrolled($user)
isRequired($user)
key()
newSecret()
recoveryLeft($user)
reset($user)Remove $user's second factor: it enrols a new one at its next login, if it is required to
uri($user, $secret)
verify($user, $code)Check a code of $user's second factor, or one of its recovery codes: a code accepted is no longer (its step, or the recovery code, is kept in the user, which the caller commits)
__construct($options)

Properties7

keyFile

string $keyFile = '{configs}/mfa.key'
Type
string

pendingTTL

int $pendingTTL = 300
Type
int

recoveryCodes

int $recoveryCodes = 10
Type
int

required

array $required = []
Type
array

window

int $window = 1
Type
int

Methods12

applies($user)

Mfa.php:55
function applies(MMO\oauth\interfaces\iUser $user): bool
$useriUser
returnsbooltrue if a login of $user by password asks a code: enrolled, or required to enrol

enrol($user, $secret, $code)

Mfa.php:74

Enrol $secret as $user's second factor, if $code is one of it: the user's app has it

function enrol(MMO\oauth\interfaces\iUser $user, string $secret, string $code): ?array
$useriUser
$secretstring
$codestring
returnsnull | arraythe recovery codes, to show once; null if $code is not one of $secret
$codes = $mfa->enrol($user, $secret, '123456'); // ['7KQ2M-XW4PA', ...], or null

hashRecovery($code)

Mfa.php:137
protected function hashRecovery(string $code): string
$codestring
returnsstringa recovery code hashed with the key: its case, spaces and dashes ignored

isEnrolled($user)

Mfa.php:50
function isEnrolled(MMO\oauth\interfaces\iUser $user): bool
$useriUser
returnsbooltrue if $user has enrolled a second factor

isRequired($user)

Mfa.php:42
function isRequired(MMO\oauth\interfaces\iUser $user): bool
$useriUser
returnsbooltrue if $user holds one of $required

key()

Mfa.php:145
function key(): string
returnsstringthe key of $keyFile, written (32 random bytes, readable by its owner only) when missing
throwsif it cannot be read nor written

newSecret()

Mfa.php:60
function newSecret(): string
returnsstringa new secret, to show (uri) until a code of it confirms it (enrol)

recoveryLeft($user)

Mfa.php:127
function recoveryLeft(MMO\oauth\interfaces\iUser $user): int
$useriUser
returnsinthow many recovery codes $user has left

reset($user)

Mfa.php:132

Remove $user's second factor: it enrols a new one at its next login, if it is required to

function reset(MMO\oauth\interfaces\iUser $user): void
$useriUser
returnsvoid

uri($user, $secret)

Mfa.php:65
function uri(MMO\oauth\interfaces\iUser $user, string $secret): string
$useriUser
$secretstring
returnsstringthe otpauth:// link of $secret for $user, its QR code's text

verify($user, $code)

Mfa.php:99

Check a code of $user's second factor, or one of its recovery codes: a code accepted is no longer (its step, or the recovery code, is kept in the user, which the caller commits)

function verify(MMO\oauth\interfaces\iUser $user, string $code): ?string
$useriUser
$codestring
returnsnull | stringtotp or recovery for a code accepted, null otherwise (none enrolled included)
$mfa->verify($user, '287 082'); // 'totp'

__construct($options)

Mfa.php:35
function __construct(array $options = [])
$options = []array