chillerlan/2fa-qrcode-bundle
Composer 安装命令:
composer require chillerlan/2fa-qrcode-bundle
包简介
An authenticator and a QR Code generator bundled together for MFA in frameworks and applications.
关键字:
README 文档
README
An authenticator (chillerlan/php-authenticator) and a QR Code generator (chillerlan/php-qrcode) bundled together for MFA in frameworks and applications.
Requirements
- PHP 8.4+
ext-mbstring- optional:
ext-gdforQRGdImagebased outputext-imagickwith ImageMagick installedext-fileinforequired byQRImagickoutput- optional libraries, see the php-qrcode requirements
Documentation
You can find the documentation of the bundled libraries in their respective repositories:
- chillerlan/php-authenticator
- chillerlan/php-qrcode
- User manual: https://php-qrcode.readthedocs.io/en/v6.0.x/
- API documentation: https://chillerlan.github.io/php-qrcode/
- chillerlan/php-settings-container
User Guide
Invocation
Fetch the settings from e.g. a framework config and invoke the TwoFactorQRCodeOptions instance with it.
Please note that this object combines the settings for AuthenticatorOptions and QROptions.
Alternatively, you can just pass an iterable of options to the TwoFactorQRCode constructor.
use chillerlan\TwoFactorQRCode\TwoFactorQRCode; $options = [ 'secret_length' => 128, 'adjacent' => 2, ]; $twoFactorQRCode = new TwoFactorQRCode($options);
User registration
- Create a secret, present it to the user as text.
- Create a QR Code and present it to the user.
- Generate a backup code and present it as well, save the counter value.
- Let the user create a fresh OTP with the new credentials.
- Save the data on successful verification and let the user triple check if they saved their backup code.
$newSecret = $twoFactorQRCode->createSecret(); $qrcode = $twoFactorQRCode->getQRCode('label', 'issuer'); $backup = $twoFactorQRCode->createBackupCode($counterValue); if($twoFactorQRCode->verifyOTP($newOTP)){ echo 'yay! do not forget to save your backup code!'; }
Normal usage: log-ins, reverification etc.
- Set the secret from the user's data.
- Present the user with a form field for the OTP and verify it.
- Verify the log-in and redirect to wherever the user was headed.
$twoFactorQRCode->setSecret($userSecret); if($twoFactorQRCode->verifyOTP($OTP)){ // do stuff... // redirect header('Location: ...'); }
Using a backup code
The user has lost access to their authenticator, send them to a form separate from the usual OTP input:
- Verify the given backup OTP against the stored counter value.
- After verification, increase the counter, create a new backup code and save the new counter value.
- Present the new backup code to the user and make them triple check that they have carefully saved it.
$twoFactorQRCode->setSecret($userSecret); if($twoFactorQRCode->verifyBackupCode($backupOTP, $counterValue)){ $counterValue++; $newBackup = $twoFactorQRCode->createBackupCode($counterValue); // redirect }
Redirect the user to wherever they can manage their 2FA settings and retrieve their secret once again:
$twoFactorQRCode->setSecret($userSecret); $currentBackup = $twoFactorQRCode->createBackupCode($currentCounterValue); $qrcode = $twoFactorQRCode->getQRCode('label', 'issuer');
Validating an e-mail address
The previous flow can also be used for other tasks, such as e-mail verification:
- Create a temporary secret and random counter value.
- Create a verification code from the above data.
- Store the temporary secret and counter together with the email address and the current timestamp.
- Send an e-mail with the code to the given e-mail address.
$tempSecret = $twoFactorQRCode->createSecret(); $tempCounter = random_int(1, 999999); $verificationCode = $twoFactorQRCode->createBackupCode($counterValue);
Now present the user with a form where they can enter the received code:
- Fetch the temporary counter and secret from the storage.
- Verify the code.
- Delete the temporary data after successful verification (or after a defined duration).
$twoFactorQRCode->setSecret($tempSecret); if($twoFactorQRCode->verifyBackupCode($verificationCode, $tempCounter)){ // success! }
A note on the secret length
The secret length as per the specifications (RFCs 4226 and 6238) is the length of the binary string that is given to the HMAC hash function - there is no encoding involved at all. Google's "Key URI format" specification uses base32 encoding in order to make the binary secret string portable (URI safe). The base32 encoding naturally results in longer strings than the original, about 60%, so a secret of 20 bytes length results in a 32 byte long base32 encoded string.
However, some of the top used libraries on packagist use some kind of pseudo base32 encoding, with a shorter secret string than requested as a result (secret length = base32 encoded length), which can compromise the security of the algorithm.
This library refers to the secret length always as the length of the raw binary string.
API
TwoFactorQRCode
The class is not final and you're free to extend it to add/change functionality to your liking.
| method | return | description |
|---|---|---|
__construct(SettingsContainerInterface|TwoFactorQRCodeOptions |iterable $options = new TwoFactorQRCodeOptions) |
- | |
createSecret(int|null $length = null) |
string |
Creates a cryptograpically secure random secret and returns it as Base32 encoded string |
setSecret(string $encodedSecret) |
static |
Sets a secret phrase from an encoded representation |
getSecret() |
string |
Returns an encoded representation of the current secret phrase |
setRawSecret(string $rawSecret) |
static |
Sets a secret phrase from a raw binary representation |
getRawSecret() |
string |
Returns the raw representation of the current secret phrase |
verifyOTP(string $otp, int|null $timestamp = null) |
bool |
Verifies a one-time password (TOTP) with an optional unix timestamp |
createBackupCode(int $counter) |
string |
Creates a counter-based one-time password (HOTP) from the given counter value |
verifyBackupCode(string $otp, int $counter) |
bool |
Verifies a counter-based backup code (HOTP) against the given counter value |
getQRCode(string $label, string $issuer) |
string |
Creates a QR Code for use with a mobile authenticator application (TOTP) with the given label and issuer name |
Anti clanker policy
No fascist plagiarism machines were - or will ever be - used in creating of this and any of the bundled libraries. Clanker created pull requests will not be accepted. However, I have no control over 3rd-party libraries, but will avoid clankers wherever I can.
Disclaimer
Use at your own risk!
chillerlan/2fa-qrcode-bundle 适用场景与选型建议
chillerlan/2fa-qrcode-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「qrcode」 「otp」 「hotp」 「totp」 「rfc4226」 「rfc6238」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chillerlan/2fa-qrcode-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chillerlan/2fa-qrcode-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chillerlan/2fa-qrcode-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Security tools for PHP
Simple HOTP/TOTP generation library
A Google Authenticator Bundle for Symfony
Based in http://phpqrcode.sourceforge.net/
Symfony 4, 5, 6 ,7 Barcode Generator Bundle with Twig function extension
Generates QR Codes
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 47
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-16