novy213 / yii2-otp
根据RFC 4226/6238 (HOTP/TOTP算法) 生成一次性密码的YII2扩展和认证小部件
2.0.1
2024-01-22 10:55 UTC
Requires
- php: >=8.1
- 2amigos/qrcode-library: ^1.1.0
- spomky-labs/otphp: ^11.0
Requires (Dev)
- codeception/codeception: *
- predis/predis: ^1.0
- yiisoft/yii2-codeception: ~2.0.0
- yiisoft/yii2-redis: ~2.0.0
This package is auto-updated.
Last update: 2024-09-22 12:15:37 UTC
README
根据RFC 4226 (HOTP算法) 和RFC 6238 (TOTP算法) 生成一次性密码的YII2扩展
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
composer require novy213/yii2-otp:~2.0.0
或
"novy213/yii2-otp" : "~2.0.0"
将以下内容添加到您的应用程序的 composer.json
文件的require部分。
使用方法
在安装扩展后,您需要设置认证客户端集合应用程序组件
配置
<?php use novy213\otp\Otp; ... 'components' => [ 'otp' => [ 'class' => Otp::className(), // 'totp' only now 'algorithm' => novy213\otp\Otp::ALGORITHM_TOTP, // length of code 'digits' => 6, // Algorithm for hashing 'digest' => 'sha1', // Label of application 'label' => 'yii2-otp', // Uri to image (application icon) 'imgLabelUrl' => Yii::to('/icon.png'), // Betwen 8 and 1024 'secretLength' => 64, // Time interval in seconds, must be at least 1 'interval' ], ... ]
添加行为 添加任何模型列以存储安全代码。//我的案例:使用两步验证
<?php use novy213\otp\behaviors\OtpBehavior; ... 'behavior' => [ 'otp' => [ 'class' => OtpBehavior::className(), // Component name 'component' => 'otp', // column|property name for get and set secure phrase //'secretAttribute' => 'secret' // column|property name for get code and confirm secret //'codeAttribute' => 'secret' //Window in time for check authorithation (current +/- window*interval) //'window' => 0 ], ... ]
小部件使用 生成初始化二维码的小部件。有关QrParams的更多信息,请参阅qrcode-library。
use novy213\otp\widgets\OtpInit; <?php echo $form->field($model, 'secret')->widget( OtpInit::className() ,[ 'component'=>'otp', // link text 'link' => 'ADD OTP BY LINK', 'QrParams' => [ // pixels width 'size' => 300, // margin around QR-code 'margin' => 10, // Path to logo on image 'logo' => '/icon.png', // Width logo on image 'logoWidth' => 50, // RGB color 'foregroundColor' => [0,0,0], // RGB color 'backgroundColor' => [255,255,255], // Qulity of QR: LOW, MEDIUM, HIGHT, QUARTILE 'level' => ErrorCorrectionLevelInterface::HIGH, // Image format: PNG, JPG, SVG, EPS 'type' => PngWriter::class, // Locale 'encoding' => 'UTF-8', // Text on image under QR code 'label' => 'QR code', // by default image create and save at Yii::$app->runtimePath . '/temporaryQR/' 'outfile' => '/tmp/'.uniqid(), // save or delete after generate 'save' => false, ] ]); ?>
更多信息
致谢
许可
GNU Lesser General Public License v3.请参阅许可文件以获取更多信息。