承接 pragmarx/google2fa-laravel 相关项目开发

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

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

pragmarx/google2fa-laravel

Composer 安装命令:

composer require pragmarx/google2fa-laravel

包简介

A One Time Password Authentication package, compatible with Google Authenticator.

README 文档

README

Latest Stable Version License Tests

Downloads PHP

Google Two-Factor Authentication Package for Laravel

Google2FA is a PHP implementation of the Google Two-Factor Authentication Module, supporting the HMAC-Based One-time Password (HOTP) algorithm specified in RFC 4226 and the Time-based One-time Password (TOTP) algorithm specified in RFC 6238.

This package is a Laravel bridge to Google2FA's PHP package.

The intent of this package is to create QRCodes for Google2FA and check user typed codes. If you need to create backup/recovery codes, please check below.

Recovery/Backup codes

if you need to create recovery or backup codes to provide a way for your users to recover a lost account, you can use the Recovery Package.

Demos, Example & Playground

Please check the Google2FA Package Playground.

playground

Here's an demo app showing how to use Google2FA: google2fa-example.

You can scan the QR code on this (old) demo page with a Google Authenticator app and view the code changing (almost) in real time.

Compatibility

Version 3.x

Laravel PHP
5.4.36+ 7.0 - 7.3
6.x 7.2 - 8.0
7.x 7.3 - 8.1
8.x 7.3 - 8.2
9.x 8.0 - 8.2
10.x 8.1 - 8.3
11.x 8.2 - 8.3
12.x 8.2 - 8.4
13.x 8.3 - 8.5

Version 2.x (Legacy)

If you need PHP 5 support, use version 2.x:

Laravel PHP
5.2-5.8 5.6 - 7.3
6.x 7.1 - 7.3
7.x 7.2 - 7.4

Install with: composer require pragmarx/google2fa-laravel:^2.0

Historical Compatibility

Laravel Google2FA Google2FA-Laravel
4.2 <= 1.0.1
5.0-5.1 <= 1.0.1
5.2-10.x >= 2.0.0 >= 0.2.0

Before Google2FA 2.0 (Laravel 5.1) you have to install pragmarx/google2fa:~1.0, because this package was both a Laravel package and a PHP (agnostic).

Demo

Click here to see the middleware demo:

middleware

Installing

Use Composer to install it:

Latest version (3.x - requires PHP 7.0+):

composer require pragmarx/google2fa-laravel

Legacy version (2.x - supports PHP 5):

composer require pragmarx/google2fa-laravel:^2.0

Installing on Laravel

Laravel 5.5 and above

You don't have to do anything else, this package autoloads the Service Provider and create the Alias, using the new Auto-Discovery feature.

Laravel 5.4 and below

Add the Service Provider and Facade alias to your app/config/app.php (Laravel 4.x) or config/app.php (Laravel 5.x):

PragmaRX\Google2FALaravel\ServiceProvider::class,

'Google2FA' => PragmaRX\Google2FALaravel\Facade::class,

Publish the config file

php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"

Using It

Use the Facade

use Google2FA;

return Google2FA::generateSecretKey();

In Laravel you can use the IoC Container

$google2fa = app('pragmarx.google2fa');

return $google2fa->generateSecretKey();

Middleware

This package has a middleware which will help you code 2FA on your app. To use it, you just have to:

Add the middleware to your Kernel.php:

protected $routeMiddleware = [
    ...
    '2fa' => \PragmaRX\Google2FALaravel\Middleware::class,
];

Using it in one or more routes:

Route::get('/admin', function () {
    return view('admin.index');
})->middleware(['auth', '2fa']);

QRCode

This package uses the Google2FA-QRCode package, please check it for more info on how to configure the proper QRCode generators for your use case.

Imagick QRCode Backend

There are three available: imagemagick (default), svg and eps.

You can change it via config:

/*
 * Which image backend to use for generating QR codes?
 *
 * Supports imagemagick, svg and eps
 */
'qrcode_image_backend' => \PragmaRX\Google2FALaravel\Support\Constants::QRCODE_IMAGE_BACKEND_IMAGEMAGICK,

Or runtime:

Google2FA::setQRCodeBackend('svg');

Configuring the view

You can set your 'ask for a one time password' view in the config file (config/google2fa.php):

/**
 * One Time Password View
 */
'view' => 'google2fa.index',

And in the view you just have to provide a form containing the input, which is also configurable:

/**
 * One Time Password request input name
 */
'otp_input' => 'one_time_password',

Here's a form example:

    <form action="/google2fa/authenticate" method="POST">
        <input name="one_time_password" type="text">

        <button type="submit">Authenticate</button>
    </form>

One Time Password Lifetime

Usually an OTP lasts forever, until the user logs off your app, but, to improve application safety, you may want to re-ask, only for the Google OTP, from time to time. So you can set a number of minutes here:

/**
* Lifetime in minutes.
* In case you need your users to be asked for a new one time passwords from time to time.
*/

'lifetime' => 0, // 0 = eternal

Keep in mind that this uses the Laravel sessions in the background. If this number exceeds the value set in config('session.lifetime') you will still be logged out, even if your OTP lifetime has not expired.

And you can decide whether your OTP will be kept alive while your users are browsing the site or not:

/**
 * Renew lifetime at every new request.
 */

'keep_alive' => true,

Manually logging out from 2Fa

This command will logout your user and redirect he/she to the 2FA form on the next request:

Google2FA::logout();

If you don't want to use the Facade, you may:

use PragmaRX\Google2FALaravel\Support\Authenticator;

(new Authenticator(request()))->logout();

Throttling / Lockout after X attempts

Unless you need something really fancy, you can probably use Laravel's route throttle middleware for that:

Route::get('/admin', function () {
    return view('admin.index');
})->middleware(['auth', '2fa', 'throttle']);

Stateless usage

$authenticator = app(Authenticator::class)->bootStateless($request);

if ($authenticator->isAuthenticated()) {
    // otp auth success!
}

You can also use a stateless middleware:

protected $routeMiddleware = [
    ...
    '2fa' => \PragmaRX\Google2FALaravel\MiddlewareStateless::class,
];

2FA and Laravel login via remember

When Laravel login via remember is activated, the session is renovated and the 2FA code is required again. To solve this, add the LoginViaRemember listener in your App\Providers\EventServiceProvider:

use Illuminate\Auth\Events\Login;
use PragmaRX\Google2FALaravel\Listeners\LoginViaRemember;

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Login::class => [
            LoginViaRemember::class,
        ],
    ];
...

Events

The following events are fired:

  • EmptyOneTimePasswordReceived
  • LoggedOut
  • LoginFailed
  • LoginSucceeded
  • OneTimePasswordExpired
  • OneTimePasswordRequested

Documentation

Check the ReadMe file in the main Google2FA repository.

Tests

The package tests were written with phpspec.

Author

Antonio Carlos Ribeiro

License

Google2FA is licensed under the MIT License - see the LICENSE file for details

Contributing

Pull requests and issues are more than welcome.

pragmarx/google2fa-laravel 适用场景与选型建议

pragmarx/google2fa-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.26M 次下载、GitHub Stars 达 1.05k, 最近一次更新时间为 2017 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 17.26M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1055
  • 点击次数: 40
  • 依赖项目数: 70
  • 推荐数: 3

GitHub 信息

  • Stars: 1045
  • Watchers: 23
  • Forks: 195
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-20