makelarisjr/laravel-2fa 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

makelarisjr/laravel-2fa

Composer 安装命令:

composer require makelarisjr/laravel-2fa

包简介

README 文档

README

This package provides all the necessary tools for a complete implementation of Google's OTP and Yubikey 2FA.

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

  • PHP: >=7.4
  • Laravel: >=7.0

Installation

Type the following command in your terminal/command line:

composer require makelarisjr/laravel-2fa

The service provider will automatically get registered. You may also manually add the service provider in your config/app.php file:

'providers' => [
    // ...
    MakelarisJR\Laravel2FA\Laravel2FAServiceProvider::class,
];

Publish the configuration file by typing the following command:

php artisan vendor:publish --provider="MakelarisJR\Laravel2FA\Laravel2FAServiceProvider"

For more info about the configuration, please refer to the Wiki article

Next, you have to add the necessary trait to the authenticatable model. This package can be used by any model, and you are not limited only to the User model.

use MakelarisJR\Laravel2FA\Traits\Has2FA;

Now that you have set up the model, create a Route group with all the routes that will require the user to have a valid OTP authorization.

Example:

Route::group(['middleware' => ['auth', 'otp']], function(){
    Route::view('/dashboard', 'dashboard')
        ->name('dashboard');
    Route::view('/devices', 'devices')
        ->name('device.list');
});

You can add the otp middleware alias to any group you like. Furthermore, the middleware will automatically prompt the user to enter the OTP code if the session needs to be refreshed. You don't need to implement your own verification logic if you use the middleware.

Last but not least, run the migrations.

php artisan migrate

This will create the following tables:

  • otp_devices
  • otp_backup_codes
  • otp_remember_tokens

Usage

Adding Devices

Now it's time to add a new device to our model. The trait that we added provides a addDevice method which accepts the following parameters:

function addDevice(string $name, string $otp_secret, string $type = OtpDevice::TYPE_GOOGLE): OtpDevice

For type, you may choose one of the following:

  • OtpDevice::TYPE_GOOGLE
  • OtpDevice::TYPE_YUBIKEY

In case of Yubikey, you may provide the full key which is 44 characters, or the device id which is the first 12 characters. The package will "cut" the string accordingly.

For Google OTP you will need to generate a QR code before or enter the secret manually to your device. Here is a simple example:

$user = User::find(1);

// ['secret' => string, 'qrcode' => string]
$data = $user->generateGoogleQRCode('My Application', $user->email);
$user->addDevice('My iPhone', $data['secret']);

The QRCode is in the form of a base64 string which can be returned to the user and be scanned by the phone.

Verify OTP

As mentioned before, the otp middleware will handle the verification without having to do something yourself. It is possible however to create your very own implementation, especially if you are planning to use another frontend framework like Vue, React, etc...

To verify the code inserted by the user, use the following method provided by the trait:

$user->verifyOtp(string $otp): bool

The string $otp can either be the code provided by the Google authenticator, or the 44 digits code provided by the Yubikey, or one of the backup codes. The method will return true if the verification is successful or false if it's not.

Backup Codes

It is also possible to generate backup codes that can be used in case you lose your device. This is a recommended safety practise because in the authentication device is lost or destroyed you won't be able to sign in to your account.

To generate the codes, you may use the following method:

$user->generateBackupCodes(int $total = 8, bool $force = false): array

The first numeric parameter designates the total number of codes that will be generated. The default is 8. The second parameter, force, designates whether new codes will be generated. Once the backup codes are created, it is not possible to create them again, unless you set force to true which in that case, the old ones will be deleted and a new batch will be created.

Extended Documentation

For the extended documentation please refer to the Wiki

Security Vulnerabilities

If you discover a security vulnerability within Laravel2FA, please send an e-mail to makelarisjr via makelarisjr@hackthebox.eu. All security vulnerabilities will be promptly addressed.

License

The Laravel2FA is open-sourced software licensed under the MIT license.

makelarisjr/laravel-2fa 适用场景与选型建议

makelarisjr/laravel-2fa 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.21k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 09 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 makelarisjr/laravel-2fa 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-06