lsat / yii2-otp

LSAT YII2 扩展,用于根据 RFC 4226/6238 (HOTP/TOTP 算法) 生成一次性密码并支持身份验证小部件,升级版本 lib 2amigos/qrcode-library 和 spomky-labs/otphp 从 https://github.com/sam002/yii2-otp (sam002/yii2-otp)。支持 PHP 至 ^8.0

维护者

详细信息

git.lcsa.vn/packages/yii2-otp

主页

安装: 545

依赖项: 0

建议者: 0

安全: 0

类型:yii2-extension

1.0.4 2023-12-13 04:56 UTC

This package is auto-updated.

Last update: 2024-09-13 06:45:35 UTC


README

LSAT Yii2 OTP


如何安装

docker-compose exec api composer require lsat/yii2-otp:~1.0.0

或者

composer require lsat/yii2-otp:~1.0.0 

或者

"lsat/yii2-otp" : "~1.0.0"

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

向 web.php 或 console.php 添加组件

'components' => [
    'otp' => [
        'class' => lsat\otp\Otp::class,
        // 'totp' only now
        'algorithm' => lsat\otp\Otp::ALGORITHM_TOTP,

        // length of code
        'digits' => 6,

        //  Algorithm for hashing
        'digest' => 'sha1',

        // Label of application
        'label' => '1022_Bình Dương',

        // Uri to image (application icon)
//            'imgLabelUrl' => \yii\helpers\Url::to('\app\web\logo.php'),

        // Betwen 8 and 1024
        'secretLength' => 72,
        // Time interval in seconds, must be at least 1
        'interval' => 30,
        'issuer' => '1022_BD',
    ],
]

使用行为

在模型中设置行为

<?php
...

'behavior' => [
    'otp' => [
        'class' => lsat\otp\behavior\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' => 'code'
        
        //Window in time for check authorithation (current +/- window*interval) 
        //'window' => 0
    ],
...
]

使用动态表单 attachBehavior

// create form or load secret form for each user
$dynamicModel = new yii\base\DynamicModel(['code','secret']);
$dynamicModel->addRule(['code'],'required');
$dynamicModel->addRule(['code'],'string', ['min' => 6]);
$dynamicModel->addRule(['secret'],'string');
// set secret attribute
$dynamicModel->secret = "YOURSECRET";
$dynamicModel->attachBehavior("otp", [
    'class' => OtpBehavior::class,
//            'secretAttribute' => "CustomSecretField",
//            'codeAttribute' => "CustomCodeField",
]);
// Load value code attribute
$code = Yii::$app->request->post("code");
$dynamicModel->code = $code;
// Validate otp code and secret attribute
if (!$dynamicModel->validate()) {
    var_dump($dynamicModel->errors);
}

生成初始化 QR 码的 Widget

关于 QrParams 的更多信息,请参阅 qrcode-library

<?php 
echo $form->field($model, 'secret')->widget(
            lsat\otp\widgets\OtpInit::class, [
            'component' => 'otp',

            // link text
            'link' => false,

            'QrParams' => [
                // pixels width
                'size' => 200,

                // margin around QR-code
                'margin' => 10,

                // Path to logo on image
                'logo' => Yii::getAlias("@app/web/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' => '',

                // by default image create and save at Yii::$app->runtimePath . '/temporaryQR/'
//                            'outfile' => '/tmp/'.uniqid(),

                // save or delete after generate
                'save' => false,
            ]
        ])->label(false); 
?>