kohaku1907/lara2step

这是我创建的包 lara2step

v1.0.0 2023-11-07 09:56 UTC

This package is auto-updated.

Last update: 2024-09-07 18:09:57 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Lara2Step 是一个 Laravel 包,为您的 Laravel 应用程序提供两步验证。

安装

使用 composer 安装此包

composer require kohaku1907/lara2step

使用以下命令发布和运行迁移

php artisan vendor:publish --tag="2step-migrations"
php artisan migrate

使用以下命令发布配置文件

php artisan vendor:publish --tag="2step-config"

这是已发布的配置文件的内容

return [
    'default_channel' => 'email', // email, sms
    'table_name' => 'two_step_auths', // table name
    'code_length' => 4, // code length
    'numeric_code' => false, // numeric code only
    'confirm_key' => '_2fa', // session key name
    'timeout' => 300, // timeout of verifed session in minutes
    'max_attempts' => 5, // max attempts
    'exceed_countdown_minutes' => 1440, // exceed countdown in minutes
    'resend_code_seconds' => 60, // resend code in seconds
];

可选地,您可以使用以下命令发布视图

php artisan vendor:publish --tag="2step-views"

用法

通过以下步骤将 Lara2Step 包集成到您的 Laravel 应用程序中

  1. User 模型中实现 TwoStepAuthenticatable 接口
  2. TwoStepAuthentication 特性添加到 User 模型中

以下是一个 User 模型的示例

use Kohaku1907\Lara2step\Contracts\TwoStepAuthenticatable;
use Kohaku1907\Lara2step\TwoStepAuthentication;

class User extends Authenticatable implements TwoStepAuthenticatable {
    use TwoStepAuthentication;

    public function registerTwoStepAuthentication(): void
    {
        $this->configureForceEnable('email');
        $this->configureCodeFormat(length: 4, numericCode: true);
    }
}

registerTwoStepAuthentication 方法中,您可以配置用户的两步验证设置。以下方法可用

  • configureForceEnable(string $channel):强制为用户启用两步验证。用户将无法禁用两步验证。
  • configureCodeFormat(int $length, bool $numericCode):配置用户的代码格式。可以配置代码长度以及代码是否应为数字。
  1. 将别名中间件添加到需要受两步验证保护的路由中
Route::get('/dashboard', function () {
    // Only verified users...
})->middleware('2step');

默认情况下,如果用户未经验证,中间件将重定向用户到名为 2step.confirm 的路由。Lara2step 包含了 TwoStepController 和默认视图,以方便快速开始。您可以使用 php artisan vendor:publish --tag="2step-views" 命令发布视图,并根据需要自定义它们。

use Kohaku1907\Lara2step\Http\Controllers\TwoStepController;
use Illuminate\Support\Facades\Route;

Route::get('2fa-confirm', [TwoStepController::class, 'form'])
    ->name('2step.confirm');
Route::post('2fa-confirm', [TwoStepController::class, 'confirm']);
Route::post('2fa-resend', [TwoStepController::class, 'resend'])
    ->name('2step.resend');

更新日志

请参阅 CHANGELOG 了解最近的变化。

贡献

请参阅 CONTRIBUTING 了解详细信息。

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。