lp2/laravel-passport-2fa

使用laravel passport配合2fa进行工作

dev-master 2022-03-27 20:14 UTC

This package is auto-updated.

Last update: 2024-09-28 02:12:34 UTC


README

与laravel passport一起工作的双因素认证包

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

       use TwoFactorAuthentication

运行迁移

        php artisan migrate

它们将向用户表添加三个字段,也添加到可填充中

        '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应用或备份码中获得的代码。

这应该可以工作。当一个访问请求到来时,我们检查2FA是否已启用。如果 "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"

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