listra/twofa

使用laravel passport与2fa一起工作

2.0.0 2024-08-12 19:22 UTC

This package is auto-updated.

Last update: 2024-09-12 19:33:54 UTC


README

与laravel passport一起工作,用于两步验证的包

在你的用户模型中,你应该添加

       use TwoFactorAuthentication

运行迁移

        php artisan migrate

它们将在用户表中添加三个字段,并将它们添加到fillable中

        'google2fa_secret',
        'google2fa_recovery_codes',
        'google2fa_enable',

你现在有这些路由可用,路由默认由Route::middleware('auth:api')保护

        POST      two-factor-authentication 
        DELETE    two-factor-authentication/{user} 
        GET|HEAD  two-factor-qr-code 
        GET|HEAD  two-factor-recovery-codes

工作原理

我们总是得到一个唯一的ik-svu,直到它被启用。使用从应用程序的Google授权中获得的代码来启用它

        GET|HEAD  two-factor-qr-code 

启用和禁用2FA,但可以使用备份代码来禁用它。

        POST      two-factor-authentication
        DELETE    two-factor-authentication/{user} 

备份代码列表,它们有状态,用于在前端显示。

      GET|HEAD  two-factor-recovery-codes

        {
        "code": "DiAHiXyqsV-PZ2grE1huc",
        "active": false
        },
        {
        "code": "jqzDmcYYmA-xCQiU75dyJ",
        "active": true
        },

现在,当你通过passport进行授权时,你必须在请求体中添加一个代码字段。这是来自Google应用或备份代码的代码。

这应该会工作。当访问请求到来时,我们检查2AF是否已启用。如果"status2FA"启用为true,则返回。

在请求体中进一步发送

auth url oauth/token的示例

    "grant_type" : "password",
    "client_id" : "2",
    "client_secret" : "6wAGwcP98VT90S5biQZjREIq4udQ7EhmwNrUzBkV",
    "username": "amosciski@example.com",
    "password": "password",
    "scope": "",
    "code":"DiAHiXyqsV-PZ2grE1huc"

然后你可以获取一个用于进一步工作的令牌。