lahthony/otp-auth-bundle

TOTP认证

1.2.4 2017-12-08 15:30 UTC

This package is not auto-updated.

Last update: 2024-09-20 03:30:06 UTC


README

Build Status StyleCI Coverage Status

SensioLabsInsight

关于

此包允许在symfony项目中轻松实现双因素认证

用户将通过使用类似Google Authenticator的应用来获得TOTP认证。

让我们开始吧。只需按照以下步骤进行。

步骤1:下载包

打开命令行,进入项目目录并执行以下命令以下载此包的最新稳定版本

$ composer require lahthony/otp-auth-bundle

此命令需要您全局安装Composer,如Composer文档中的安装章节所述。

步骤2:启用包

然后,将包添加到项目app/AppKernel.php文件中注册的包列表中,以启用包。

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new LahthonyOTPAuthBundle\LahthonyOTPAuthBundle(),
        );

        // ...
    }

    // ...
}

然后您需要更新service.yml

    YourBundle\:
        resource: '../../src/YourBundle/*'
        #Remove the folder Entity From exclude folder 
        exclude: '../../src/YourBundle/{Repository,Tests}'

步骤3:实现OTPAuthInterface

您需要在您的用户实体上实现OTPAuthInterface,通常位于src/AppBundle/Entity/User

⚠️ 不要忘记生成getter setter。 ⚠️

<?php
//src/AppBundle/Entity/User

use LahthonyOTPAuthBundle\Model\OTPAuthInterface;
//...

class User implements OTPAuthInterface
{
    /**
     * This attribute needs to be stock in Database
     * @var string 
     * @ORM\Column(name="secret_auth_key", type="string", length=255, nullable=true)
     */
    private $secretAuthKey;

    /**
     * This attribute needs to be stocked in Database   
     * @var string
     * @ORM\Column(name="recovery_key", type="string", length=255, nullable=true)
     */
    private $recoveryKey;
    
    
    /**
     * This attribute will permit to do verification on user registration 
     * if he accepts 2Factor Authentication 
     * @var boolean
     */
    private $OTP2Auth;
    
  
    /**
     * !!! DO NOT FORGET TO GENERATE GETTER AND SETTER FOR THESE THREE ATTRIBUTES !!! 
     */
    
    //We'll need email and password for the OTP Authentication reset
    public function getEmail(){}
    public function getPassword(){}
        
}

⚠️ 之后不要忘记进行模式更新: ⚠️

$ php bin/console doctrine:schema:update --force

步骤4:向您的UserFormType添加一个字段

我们为您提供了一个事件订阅者,允许您轻松地在您的UserFormType上添加所需字段。

如果您想允许用户在注册后启用或禁用OTP认证,也可以将其添加到UserEditType

只需这样做

用户注册

<?php
//src/AppBundle/Form/UserType

use LahthonyOTPAuthBundle\Form\EventSubscriber\Add2FactorAuthFieldSubscriber;
//...

class UserType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //...
            ->addEventSubscriber(new Add2FactorAuthFieldSubscriber())
        ;
    }
    //...
}

用户编辑

//src/AppBundle/Form/UserEditType

use LahthonyOTPAuthBundle\Form\EventSubscriber\Add2FactorAuthFieldSubscriber;
//...

class UserType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            //...
            ->addEventSubscriber(new Add2FactorAuthFieldSubscriber())
        ;
    }
    //...
}

步骤5:更新登录表单和主页

现在您需要在您的登录表单中添加一个字段以及重置认证器的链接

<!--login login.html.twig -->
<form>
    <label for="otp">Code OTP(optionnal if you haven't accept 2factorAuth)</label>
    <input type="text" name="otp">
</form>
<a href="{{ path('lahthony_otp_ask_recovery') }}">I've lost my OTP Authenticator.</a>
<!-- homepage index.html.twig -->
    <div class="flash-notice">
        {% for message in app.flashes('2factor') %}
            {{ message|raw }}
        {% endfor %}
        {% for message in app.flashes('reset') %}
            {{ message }}
        {% endfor %}
    </div>

步骤6:导入路由

在您的routing.yml中导入来自我们的包的路由

lahthony_otp_auth_recovery:
    resource: "@LahthonyOTPAuthBundle/Resources/config/routing.xml"

步骤7:享受

  • 现在您可以尝试了。首先创建一个接受双因素认证的用户。

  • 然后,主页上会出现包含二维码恢复密码的闪存消息。

    ⚠️ 如果您想恢复丢失认证器的账户,请务必记下恢复密码。 ⚠️

    使用类似Google Authenticator的otp应用扫描二维码 在这里下载

  • 转到登录页面并使用您的应用中生成的代码进行连接。

  • 现在您可以通过用户编辑来更新它,要求禁用它。

  • 这不神奇吗?!希望您喜欢;请随时给我们反馈并报告错误。我们想了解您的意见。

配置

如果您想重新定义默认配置,请将其添加到您的app/config/config.yml

lahthony_otp_auth:
    digest_algo:
        sha1 #algorithm
    digit:
        6 #the output will generate 6 digit 
    period:
        30 #period for the timer
    issuer:
        'your_website_name'
    image:
         null
    roles: []