hryvinskyi / magento2-totp
Time-based one-time passwords (RFC 6238) for Magento 2, in the format every authenticator app reads: a random shared secret, the otpauth:// provisioning URI and its QR code as SVG, and verification within one time step either side with a step accepted only once.
Package info
github.com/hryvinskyi/magento2-totp
Type:magento2-module
pkg:composer/hryvinskyi/magento2-totp
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- bacon/bacon-qr-code: ^2.0 || ^3.0
- hryvinskyi/magento2-clock: ^1.0
- magento/framework: *
- paragonie/constant_time_encoding: ^2.6 || ^3.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Time-based one-time passwords (TOTP, RFC 6238) in the format every authenticator app reads: a random shared secret, the otpauth:// link an app scans and its QR code as SVG, and the check of a code a person types. The module stores nothing and shows nothing. The module that uses it keeps each secret, asks for the codes and limits wrong entries.
What it does for a store
- The format apps read. HMAC-SHA1, 6 digits, a 30-second time step: the settings every RFC 6238 authenticator app uses by default.
- A shared secret. 20 random bytes from a cryptographically secure source, written in upper-case Base32 without padding (32 characters).
- The provisioning link and its QR code. The link reads
otpauth://totp/<issuer>:<account>?secret=<secret>&issuer=<issuer>&algorithm=SHA1&digits=6&period=30, with the issuer and the account name URL-encoded. The issuer is the name the app shows above the code, such as the shop's name. The QR code is SVG markup, 200 pixels wide and high. - One step either side. A code of the current 30-second step, the step before or the step after is accepted, so a phone clock up to about 30 seconds off still works.
- A step is accepted once. The check returns the step the code matched. The caller stores it and passes it back on the next check, and a code of that step or an earlier one is refused, so a code seen over someone's shoulder cannot be used again.
- Refused, never reported. A code that is not 6 digits once spaces are removed is refused. A secret that is not valid upper-case Base32 refuses every code; its decoding error never leaves the module, so the secret never reaches a log.
Requirements
- Magento Open Source or Adobe Commerce 2.4.x. The module is built and tested on 2.4.9.
- PHP 8.1 to 8.5.
bacon/bacon-qr-code^2.0 or ^3.0,paragonie/constant_time_encoding^2.6 or ^3.0, andhryvinskyi/magento2-clock^1.0.
Installation
Run from the Magento root.
composer require hryvinskyi/magento2-totp
bin/magento module:enable Hryvinskyi_Clock Hryvinskyi_Totp
bin/magento setup:upgrade
bin/magento setup:di:compile # production mode only
bin/magento cache:flush
Verify: bin/magento module:status Hryvinskyi_Totp prints Hryvinskyi_Totp : Module is enabled.
The module has no settings and creates no table.
Using it
Inject Hryvinskyi\Totp\Api\TotpInterface and, to draw the QR code, Hryvinskyi\Totp\Api\ProvisioningQrInterface:
use Hryvinskyi\Totp\Api\ProvisioningQrInterface; use Hryvinskyi\Totp\Api\TotpInterface; public function __construct( private readonly TotpInterface $totp, private readonly ProvisioningQrInterface $qr ) { } // Setting up an app: show the QR code, and the secret for typing in by hand. $secret = $this->totp->generateSecret(); $svg = $this->qr->svg($this->totp->provisioningUri($secret, $storeName, $email)); // Checking a code: store the returned step as the last accepted one. $step = $this->totp->verify($secret, $typedCode, $lastAcceptedStep); if ($step !== null) { $lastAcceptedStep = $step; }
| Call | Returns |
|---|---|
generateSecret() |
A new secret, 32 upper-case Base32 characters. |
provisioningUri($secret, $issuer, $accountName) |
The otpauth://totp/… link. |
verify($secret, $code, $lastAcceptedStep) |
The time step the code matched, or null when it is refused. Pass null as $lastAcceptedStep before any code was accepted. |
ProvisioningQrInterface::svg($uri) |
The QR code of the link as SVG markup. |
Ask for one code when the person sets the app up, and keep the secret only once that code passes: it proves the app holds the secret.
Extension points
TotpInterface and ProvisioningQrInterface are @api. To draw the QR code another way, replace the preference for ProvisioningQrInterface in your module's etc/di.xml:
<preference for="Hryvinskyi\Totp\Api\ProvisioningQrInterface" type="Vendor\Module\Model\PngProvisioningQr"/>
Model\BaconProvisioningQr is the only class that names bacon/bacon-qr-code.
Stored data
None. The caller keeps the secret and the last accepted step. Anyone who holds the secret can make valid codes, so keep it encrypted (the platform's Magento\Framework\Encryption\EncryptorInterface), never in plain text and never in a log.
Limits
- No entry limit. Each check compares up to three codes, so a guess has about three chances in a million. The module does not count wrong entries; the caller limits how many it accepts.
- A step is refused a second time only when the caller passes the last accepted step back. A caller that stores no step accepts a code again for up to about 90 seconds.
- A clock more than one step off fails. A phone or a server whose clock is off by more than about 30 seconds produces codes the check refuses. The server's time comes from
hryvinskyi/magento2-clock. - One format. SHA-1, 6 digits and 30 seconds are fixed. Secrets made for SHA-256, 8 digits or another period are not supported.
- The secret must be upper-case Base32, as
generateSecret()makes it. A secret in lower case refuses every code.
Registrations
| Where | What |
|---|---|
Preference Hryvinskyi\Totp\Api\TotpInterface |
Hryvinskyi\Totp\Model\Totp |
Preference Hryvinskyi\Totp\Api\ProvisioningQrInterface |
Hryvinskyi\Totp\Model\BaconProvisioningQr |
Preference Hryvinskyi\Totp\Model\RandomBytesInterface |
Hryvinskyi\Totp\Model\RandomBytes |
The module registers no route, setting, table, cron job, command, event or plugin.
Versioning
The package follows Semantic Versioning.
Tests
Run from the Magento root.
- Unit:
vendor/bin/phpunit -c app/code/Hryvinskyi/Totp/phpunit.xml.dist. - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/Totp/phpstan.neon.dist --level=10, afterbin/magento setup:di:compile), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/Totp/phpcs.xml.dist app/code/Hryvinskyi/Totp.