astrotomic/laravel-auth-recovery-codes

该软件包提供了Laravel绑定和Eloquent/Model特性,用于pragmarx/recovery软件包。

0.2.0 2021-03-30 08:39 UTC

This package is auto-updated.

Last update: 2024-09-10 18:20:41 UTC


README

Latest Version MIT License Offset Earth Larabelles

GitHub Workflow Status StyleCI Total Downloads

此软件包提供了Laravel绑定和Eloquent/Model特性,用于pragmarx/recovery软件包。它允许您轻松处理恢复代码,这些代码对于2FA设置是必需的,您只需关注应用程序逻辑即可。

安装

您可以通过composer安装此软件包

composer require astrotomic/laravel-auth-recovery-codes

并通过artisan发布配置

php artisan vendor:publish --provider="Astrotomic\AuthRecoveryCodes\AuthRecoveryCodesServiceProvider" --tag=config

用法

模型

您需要将Recoverable特性添加到您想要具有恢复代码的模型中,并且应该将jsonarray类型转换添加到存储恢复代码的属性。

use Illuminate\Database\Eloquent\Model;
use Astrotomic\AuthRecoveryCodes\Recoverable;

class User extends Model
{
    use Recoverable;

    protected $casts = [
        'recovery_codes' => 'array',
    ];
}

默认情况下,特性使用recovery_codes属性/列 - 您可以通过设置$recoveryCodesName属性来更改此设置

class User extends Model
{
    use Recoverable;

    protected string $recoveryCodesName = 'mfa_recovery_codes';

    protected $casts = [
        'mfa_recovery_codes' => 'array',
    ];
}

要将新的恢复代码设置到您的模型中,您应该使用setRecoveryCodes()方法,因为此方法会自动对恢复代码进行哈希处理(如果尚未进行哈希处理)。这一步对于安全性非常重要,因为只有用户可以访问恢复代码,其他人则不能。以下是一个可能的控制器操作的示例

  • 生成代码
  • 在用户模型上设置和保存代码
  • 将代码响应给用户(任何人都可以获取/读取明文恢复代码的唯一时间)
$codes = User::generateRecoveryCodes();

$user->setRecoveryCodes($codes)->save();

return response()->json($codes);

如果您想使用默认的模型属性而不需要使用setRecoveryCodes()方法,您应该添加自己的访问器和修改器,请记住在设置时要调用Recoverable::hashRecoveryCodes()方法,并且您必须自己进行JSON转换。

迁移

在设置您的模型后,您必须在您的数据库表中添加新的列,没有太多要求 - json列类型可以帮助防止无效内容,但恢复代码JSON实际上是不可查询的(仅哈希值的数组),但如果您在用户创建/注册时没有设置恢复代码,则列应该是可空的。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddRecoveryCodesToUsersTable extends Migration
{
    public function up(): void
    {
        Schema::table('users', static function (Blueprint $table): void {
            $table->json('recovery_codes')->nullable();
        });
    }

    public function down(): void
    {
        Schema::table('users', static function (Blueprint $table): void {
            $table->dropColumn('recovery_codes');
        });
    }
}

恢复

现在您已经设置好应用程序以生成和存储恢复代码,您应该添加恢复账户的逻辑。Recoverable特性提供了两个方法来帮助您完成这项任务。

  • isValidRecoveryCode()返回一个布尔值,告诉您是否任何已保存的恢复代码与输入匹配
  • useRecoveryCode()从数组中删除匹配的哈希值,并设置剩余的恢复代码数组
use Astrotomic\AuthRecoveryCodes\Recoverable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpFoundation\Response;

class RecoverController
{
    public function __invoke(Request $request)
    {
        /** @var Model|Recoverable $user */
        $user = User::whereEmail($request->email)->firstOrFail();

        abort_unless(Hash::check($request->password, $user->password), Response::HTTP_NOT_FOUND);

        abort_unless($user->isValidRecoveryCode($request->recovery_code), Response::HTTP_NOT_FOUND);

        // do something to allow the user to recover the account
        // - log them in and redirect to account/security settings
        // - disable 2FA
        // - send an email with a signed link to do something

        $user->useRecoveryCode($request->recovery_code)->save();

        // you should check if user has remaining recovery codes
        // if not you should re-generate some and tell the user
        // for sure you can trigger this before all codes are used
        // or remind the user on regular login to generate new ones
        // if he's running out of remaining ones
        if(empty($user->getRecoveryCodes())) {
            $codes = User::generateRecoveryCodes();

            $user->setRecoveryCodes($codes)->save();

            return response()->json($codes);
        }
    }
}

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献以获取详细信息。您可能还感兴趣行为准则

安全

如果您发现任何与安全相关的错误,请参阅安全以获取报告步骤。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可文件

开源软件

您可以使用此软件包,但如果它进入了您的生产环境,我将非常感激您为世界买一棵树。

现在人们普遍认为,应对气候危机并防止气温升高超过1.5摄氏度的最佳工具之一是植树。如果您为我的森林做出贡献,您将为当地家庭创造就业机会,并恢复野生动物栖息地。

您可以在offset.earth/treeware购买树木。

有关Treeware的更多信息,请访问treeware.earth