khainifzyen/yii2-otp

基于 RFC 4226/6238 (HOTP/TOTP 算法) 的 YII2 扩展,用于生成一次性密码以及身份验证小部件

安装次数: 0

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 1

分支: 6

类型:yii2-extension

2.0.0 2018-01-14 18:00 UTC

This package is auto-updated.

Last update: 2024-09-17 07:53:57 UTC


README

Code Climate SensioLabsInsight

Latest Version Software License

Build Status

基于 RFC 4226 (HOTP 算法) 和 RFC 6238 (TOTP 算法) 的 YII2 扩展,用于生成一次性密码

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

composer require khanifzyen/yii2-otp:~2.0.0

或者

"khanifzyen/yii2-otp" : "~2.0.0"

将以下内容添加到您应用程序的 composer.json 文件的 require 部分。

用法

扩展安装后,您需要设置 auth client collection 应用组件

配置

<?php
use khanifzyen\otp\Otp;

...

'components' => [
    'otp' => [
        'class' => Otp::className(),
        // 'totp' only now
        'algorithm' => khanifzyen\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 khanifzyen\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
    ],
...
]

小部件使用 用于生成初始化 QR 码的小部件。有关 QrParams 的更多信息,请参阅 qrcode-library

use khanifzyen\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,
                        ]
                ]); ?>

更多信息

致谢

许可证

LGPLv3 许可证。有关更多信息,请参阅 许可证文件