承接 astrotomic/laravel-auth-recovery-codes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

astrotomic/laravel-auth-recovery-codes

Composer 安装命令:

composer require astrotomic/laravel-auth-recovery-codes

包简介

This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package.

README 文档

README

Latest Version MIT License Offset Earth Larabelles

GitHub Workflow Status StyleCI Total Downloads

This package provides Laravel bindings and a Eloquent/Model trait for pragmarx/recovery package. It allows you to easily handle recovery codes, needed for 2FA setups and only care about the app logic.

Installation

You can install the package via composer:

composer require astrotomic/laravel-auth-recovery-codes

And publish the config via artisan:

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

Usage

Model

You will have to add the Recoverable trait to your model you want to have recovery codes and should add a json or array cast to the attribute holding the recovery codes.

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

class User extends Model
{
    use Recoverable;

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

By default the trait uses a recovery_codes attribute/column - you can change this by setting $recoveryCodesName property:

class User extends Model
{
    use Recoverable;

    protected string $recoveryCodesName = 'mfa_recovery_codes';

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

To set the new recovery codes to your model you should use the setRecoveryCodes() method because this method automatically hashes the recovery codes, if not already hashed. This step is important for security because with this step only the user has access to the recovery codes and no one else. The following snippet is an example of a possible controller action

  • generating the codes
  • setting and saving the codes on the user model
  • responding with the codes to the user (the one and only time anyone can get/read the plaintext recovery codes)
$codes = User::generateRecoveryCodes();

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

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

If you want to use the default model attribute without the need to use setRecoveryCodes() method you should add your own accessor and mutator, keep in mind to call the Recoverable::hashRecoveryCodes() method on set and that you have to do the JSON casting by your own.

Migration

After setting up your model you will have to add the new column to your database table, there aren't much requirements - the json column type would only help to prevent invalid content, but the recovery codes JSON isn't really queryable (only an array of hashes), but the column should be nullable if you don't setup recovery codes on user create/register.

<?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');
        });
    }
}

Recovery

Now that you have setup your app to generate and store recovery codes you should add the logic to recover an account. The Recoverable trait comes with two methods to help you with this task.

  • isValidRecoveryCode() return a bool and tells you if any of the saved recovery codes matches the input
  • useRecoveryCode() removes the matching hash from the array and sets the array of remaining recovery codes
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);
        }
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details. You could also be interested in CODE OF CONDUCT.

Security

If you discover any security related issues, please check SECURITY for steps to report it.

Credits

License

The MIT License (MIT). Please see License File for more information.

Treeware

You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees at offset.earth/treeware

Read more about Treeware at treeware.earth

astrotomic/laravel-auth-recovery-codes 适用场景与选型建议

astrotomic/laravel-auth-recovery-codes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.92k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2020 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「Authentication」 「laravel」 「tfa」 「2fa」 「Two Factor Authentication」 「Multi Factor Authentication」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 astrotomic/laravel-auth-recovery-codes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 astrotomic/laravel-auth-recovery-codes 我们能提供哪些服务?
定制开发 / 二次开发

基于 astrotomic/laravel-auth-recovery-codes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-31