brians84/two-factor-auth-laravel

处理2FA认证的包

dev-main 2023-06-22 12:55 UTC

This package is auto-updated.

Last update: 2024-09-22 15:42:42 UTC


README

一个简单的使用Google Authenticator的2FA。

关于项目

此项目使用以下三个包

显然,您可以安装这三个包并自行操作,但这是一种快速简单的方法。

简而言之,此包在用户登录后立即请求用户使用Google Authenticator验证其凭据。

  • 如果用户从未注册2FA,则显示设置页面以执行此操作。

screenshot-1

  • 如果用户已经完成了,则显示验证表单。

screenshot-2

此外,如果您在登录表单中有“记住”输入,我们会注意到这一点,并在验证成功后添加一个cookie。因此,下次用户访问网站时,我们不再要求进行2FA验证。一旦用户注销,我们就会删除cookie。

安装

  1. 使用composer安装此项目

       composer require maurohmartinez/two-factor-auth-laravel
  2. 运行迁移

       php artisan migrate
  3. 发布配置、视图和公共文件,并根据需要(如果需要)进行自定义

       php artisan vendor:publish --provider="MHMartinez\TwoFactorAuth\app\Providers\TwoFactorAuthServiceProvider"
  4. [可选] 调整中间件组名称

    此包自动将中间件应用于“admin”路由,但您可以通过更新配置文件进行调整

    'middleware_route' => 'admin'

    您还可以手动添加中间件 MHMartinez\TwoFactorAuth\app\Http\Middleware\TwoFactorAuthMiddleware 到您需要的地方。

  5. [可选] 如果您只想让某些用户验证2FA,则您的 User 模型应该实现接口 MHMartinez\TwoFactorAuth\app\Interfaces\TwoFactorAuthInterface。这将要求您添加一个新的方法 shouldValidateWithTwoFactorAuth,它应该返回一个布尔值,表示中间件是否应该跳过该特定用户。

    您的 User 模型类的示例

    use MHMartinez\TwoFactorAuth\app\Interfaces\TwoFactorAuthInterface;
    
    class User extends Authenticate implements TwoFactorAuthInterface

    shouldValidateWithTwoFactorAuth() 方法的示例

    public function shouldValidateWithTwoFactorAuth(): bool
     {
         // do your logic here
         
         return true; // or false :)
     }
  6. [可选] 通过在 .env 中添加 TWO_FACTOR_AUTH_ENABLED=false 来在本地环境中禁用此包

  7. [可选] 在配置文件中设置一次性密码过期天数。请注意,0表示它永不过期 '2fa_expires' => 0,

联系

项目链接:https://github.com/maurohmartinez/two-factor-auth-laravel