webservco / mfa
A PHP component/library. TOTP multi-factor authentication (Google Authenticator compatible): enrollment with a QR code, verification with replay protection and throttling, recovery codes and encrypted secrets.
Requires
- php: ^8.4
- ext-xmlwriter: *
- bacon/bacon-qr-code: ^3
- psr/clock: ^1
- webservco/configuration: ^2
- webservco/data: ^2
- webservco/database: ^2
- webservco/encryption: ^0
Requires (Dev)
- pds/skeleton: ^1
- phan/phan: ^6
- php-parallel-lint/php-console-highlighter: ^1
- php-parallel-lint/php-parallel-lint: ^1
- phpmd/phpmd: ^3
- phpstan/phpstan: ^2
- phpstan/phpstan-deprecation-rules: ^2
- phpstan/phpstan-phpunit: ^2
- phpstan/phpstan-strict-rules: ^2
- phpunit/phpunit: ^13
- slevomat/coding-standard: ^8
- squizlabs/php_codesniffer: ^4
- vimeo/psalm: ^6
- webservco/coding-standards: ^2.4
- webservco/component-common: ^2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP component/library. Please see webservco/sandbox for an example project using these types of components.
TOTP multi-factor authentication (Google Authenticator compatible): enrollment with a QR code, verification with replay protection and throttling, recovery codes and encrypted secrets.
- Requires: PHP extension
xmlwriter, andsodiumthroughwebservco/encryption; the tables inresources/mfa.sql; theMFA_ENCRYPTION_KEYandMFA_ISSUERconfiguration keys - Related:
webservco/auth(an application asks for the code while its login is pending);webservco/database(the PDO container);webservco/encryption(encrypts the secrets)
Installation
composer require webservco/mfa
Usage
One service per realm, that is per kind of account (admin, customer…). Account ids are strings of 1 to 64 characters.
use WebServCo\Data\Factory\Extraction\DataExtractionContainerFactory; use WebServCo\Mfa\Factory\MfaServiceFactory; use WebServCo\Mfa\Model\VerificationResult; // \WebServCo\Mfa\Contract\MfaServiceFactoryInterface $mfaServiceFactory = new MfaServiceFactory( $configurationGetter, // \WebServCo\Configuration\Contract\ConfigurationGetterInterface (new DataExtractionContainerFactory())->createDataExtractionContainer(false), $pdoContainer, // \WebServCo\Database\Contract\PDOContainerInterface ); // \WebServCo\Mfa\Contract\MfaServiceInterface $mfaService = $mfaServiceFactory->createMfaService('admin'); // Login: the password was right, and MFA is enabled for the account. $result = $mfaService->verify($accountId, $submittedCode); if ($result === VerificationResult::Accepted || $result === VerificationResult::AcceptedRecoveryCode) { // Complete the login. } // VerificationResult::Locked: too many failures, even a valid code is refused for now.
Enrollment
// \WebServCo\Mfa\Model\DataTransfer\Enrollment $enrollment = $mfaService->startEnrollment($accountId, $username); // Show $enrollment->qrCodeSvg (SVG markup) and $enrollment->manualKey, then ask for the code the app shows. $recoveryCodes = $mfaService->confirmEnrollment($accountId, $submittedCode); if ($recoveryCodes === null) { // Wrong code: ask again. MFA stays off until a code is right. } // MFA is enabled. Show the 10 recovery codes once; each works one time instead of a code.
Status, new recovery codes, disable
// \WebServCo\Mfa\Model\DataTransfer\MfaStatus: isEnabled, isLocked, remainingRecoveryCodes $status = $mfaService->getStatus($accountId); // Replaces the old codes. Ask for a current code first. $recoveryCodes = $mfaService->regenerateRecoveryCodes($accountId); // The user turns MFA off, or an administrator resets a lost device. $mfaService->disable($accountId);
Contracts
| Interface | Implementation | Purpose |
|---|---|---|
Contract\MfaServiceFactoryInterface |
Factory\MfaServiceFactory |
Creates MfaServiceInterface for a realm. |
Contract\MfaServiceInterface |
Service\MfaService |
Enrollment, verification, status, recovery codes, disable. |
Contract\Storage\TotpCredentialStorageInterface |
Service\Storage\PDOTotpCredentialStorage |
Encrypted secrets, used time steps, failures. |
Contract\Storage\RecoveryCodeStorageInterface |
Service\Storage\PDORecoveryCodeStorage |
Recovery code hashes. |
Psr\Clock\ClockInterface |
Service\SystemClock |
Current time. |
Namespaces are relative to WebServCo\Mfa\. For other table names, build the services as MfaServiceFactory does: the storages take the table name as their last argument.
Configuration
[MFA] ; Base64 of 32 random bytes; one key per application and environment: ; php -r 'echo base64_encode(random_bytes(32)), PHP_EOL;' MFA_ENCRYPTION_KEY = '' ; Shown in the authenticator app. No colon. MFA_ISSUER = 'Example Admin'
Notes
- A code, TOTP or recovery, is accepted once. The current and the previous 30-second step are valid.
- From the 5th consecutive failure each failure locks MFA for 15 minutes; from the 20th it stays locked until
disable(). A success resets the count. Enrollmentand the recovery codes contain secrets: show them to the user only, never log or store them.- Keep the key out of the database and back it up. Without it every user has to enroll again.
- Server clocks must be NTP-synchronized. TOTP is not phishing-resistant.
- Who may call
disable()is the application's decision.
Development
# Start project. ddev start # Install dependencies. ddev c u # Run all validations. ddev c c # Run unit tests, with coverage (needs Xdebug, which DDEV leaves off). ddev xdebug on && ddev c test:dox; ddev xdebug off