rezkonline/laravel-2fa

为 Laravel 应用程序提供简单的双因素认证。

dev-master 2022-02-09 21:14 UTC

This package is not auto-updated.

Last update: 2024-09-20 08:10:40 UTC


README

Laravel 2fa

为 Laravel 应用程序提供简单的双因素认证。

Total Downloads Latest Stable Version License

安装

通过 Composer 安装此包

要开始使用 Laravel 2FA,请使用 Composer 将此包添加到项目的依赖中

composer require rezkonline/laravel-2fa

或在 composer.json 中的 require 部分添加此行

{
    "require": {
        "rezkonline/laravel-2fa": "^1.1",
    }
}

然后运行 composer install

使用 php artisan migrate 更新数据库

安装包后,您必须运行 php artisan migrate 以将双因素认证字段添加到您的 users 表中。

它将在您的数据库表中添加以下列

|-------- users --------|
|    two_factor_code    |
| two_factor_expires_at |
|-----------------------|

替换 LoginController 中的 AuthenticatesUsers 特质

之后,打开您的 app\Http\Controllers\Auth\LoginController 文件,将 AuthenticatesUsers 特质替换为该包提供的 AuthenticateUsersWithTwoFactor

基本上,它覆盖了 AuthenticatesUsers 上的 authenticated 方法

trait AuthenticateUsersWithTwoFactor
{
    use AuthenticatesUsers;

    /**
     * The user has been successfully authenticated.
     * @param Request $request
     * @param $user
     */
    public function authenticated(Request $request, $user)
    {
        $user->generateTwoFactorCode();
        $user->notify(new TwoFactorCode());
    }
}

然后,只需在您的 User 模型中使用 HasTwoFactorAuthentication 特质

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    use HasTwoFactorAuthentication;
    ...
}

发布包配置

要发布包配置,可以使用以下命令

php artisan vendor:publish --provider="Rezkonline\TwoFactorAuth\TwoFactorAuthServiceProvider" --tag="laravel-2fa-config"

发布后,这是 config/laravel-2fa.php 的样子

<?php

return [
    /*
   |--------------------------------------------------------------------------
   | Tables
   |--------------------------------------------------------------------------
   | Specify the basics authentication tables that you are using.
   | Once you required this package, the following tables are
   | created/modified by default when you run the command
   |
   | php artisan migrate
   |
    */
    "tables" => [
        "users" => "users",
    ],
   
    /*
   |--------------------------------------------------------------------------
   | Two factor code length
   |--------------------------------------------------------------------------
   | Specify the length of your two factor code.
   |
    */
    "code_length" => 8,

     /*
    |--------------------------------------------------------------------------
    | Two factor code expiration time
    |--------------------------------------------------------------------------
    | Specify the duration of your two factor code in minutes.
    |
    */
    "code_expires_in" => 10,

     /*
     |--------------------------------------------------------------------------
     | Redirect to route
     |--------------------------------------------------------------------------
     | Specify the route which users should be redirected to after successfully confirming
     | the two factor auth code.
     |
      */
    "redirect_to_route" => "home"
];

发布包资源

此包使用自定义视图来确认双因素代码。您需要使用以下命令将包资源发布到该视图

php artisan vendor:publish --provider="Rezkonline\TwoFactorAuth\TwoFactorAuthServiceProvider" --tag="laravel-2fa-assets" 

使用方法

要开始使用此包,您需要在 .env 文件中配置您的电子邮件设置。以下是一个示例配置

MAIL_MAILER=your_mailer
MAIL_HOST=your_mailer_host
MAIL_PORT=2525
MAIL_USERNAME=your_mail_username
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=your_mail@your_domain.com
MAIL_FROM_NAME="${APP_NAME}"

现在,您需要将 two_factor 中间件注册到您的 app/Http/Kernel.php 文件中。将其添加到 routeMiddleware 数组中

protected $routeMiddleware = [
    ...
    'two_factor_auth' => TwoFactorAuthMiddleware::class
];

之后,您只需使用 two_factor 中间件保护您的路由即可

Route::middleware('two_factor_auth')->group(function() {
    // Your routes here
});

事件

此包会在确认双因素代码和重新发送双因素代码操作时触发事件。

您可以在您的 EventServiceProvider 中监听这些事件

protected $listen = [
    \Rezkonline\TwoFactorAuth\Events\TwoFactorCodeConfirmed::class => [
        //Your listeners here
    ],
    \Rezkonline\TwoFactorAuth\Events\TwoFactorCodeResent::class => [
        // Your listeners here
    ]
];

在您的路由受保护后,您的用户必须确认双因素认证代码,该代码将在他们使用正确凭据登录后通过电子邮件发送。

贡献

感谢您考虑为 Laravel Invite Codes 包做出贡献!贡献指南可在此处找到:here

测试

运行 composer test 来测试此包。

致谢

许可

Laravel 2FA 包是开源软件,许可协议为 MIT 许可证。请参阅 许可文件 以获取更多信息。