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.
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.
Please check the Google2FA Package 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.
Laravel | Google2FA | Google2FA-Laravel |
---|---|---|
4.2 | <= 1.0.1 | |
5.0-5.1 | <= 1.0.1 | |
5.2-8.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).
Click here to see the middleware demo:
Use Composer to install it:
composer require pragmarx/google2fa-laravel
You don't have to do anything else, this package autoloads the Service Provider and create the Alias, using the new Auto-Discovery feature.
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,
php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"
use Google2FA;
return Google2FA::generateSecretKey();
$google2fa = app('pragmarx.google2fa');
return $google2fa->generateSecretKey();
This package has a middleware which will help you code 2FA on your app. To use it, you just have to:
protected $routeMiddleware = [
...
'2fa' => \PragmaRX\Google2FALaravel\Middleware::class,
];
Route::get('/admin', function () {
return view('admin.index');
})->middleware(['auth', '2fa']);
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.
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');
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>
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,
This command wil 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();
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']);
$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,
];
The following events are fired:
Check the ReadMe file in the main Google2FA repository.
The package tests were written with phpspec.
Google2FA is licensed under the MIT License - see the LICENSE
file for details
Pull requests and issues are more than welcome.
本来准备给大家得瑟一下的!突然发现登录还不行! 我们整顿大军,来处理一下登录的,情况,既然,要做,就做的漂亮一点,哈哈。 这个跳转的位置是:http://www.laravel.net/index.php/auth/login 那么一样的逻辑,先按照这个几个步骤来: 第一:路由 第二:模版 第三:控制器 貌似发现,第一个路由不需要我们设置了,因为这个有点是 认证的意思,就是自动跳转过来的,好,那就
关于Laravel前端脚手架的状态,存在很多争论,戏剧性和最终的困惑。我不会关注任何戏剧-谁对谁错是不重要的。 什么是重要的是确保人们理解的-相对复杂-前端脚手架的状态。初学者和经验丰富的开发人员似乎都对该前端“生态系统”的某些部分感到困惑。 因此,本文将尝试作为Laravel前端脚手架的完整指南。 注意:如果您只是想快速了解要使用的内容,而又不对不同解决方案的用途进行解释,请跳到文章结尾。 可用
Laravel文档 Laravel文档地址 Lumen框架 为速度而生的 Laravel 框架 官网地址 热门开发包 Laravel API Document Generator -Laravel API 文档生成器 官方地址 Laravel Menu - Html导航菜单生成器 官方地址 Debug Bar - 在 Laravel 中集成 PHP 的调试条 官方地址 Laravel 5 Log
问题内容: 我在控制器的函数中。 因此,从表单中,我得到了一个变量的值,说: 然后,我需要在WHERE语句中嵌入该变量(即它的值)。如果我对值进行硬编码,它将带来正确的结果,但是我已经尝试了各种方法来插入该变量而没有成功。好吧,假设我设法使用了该变量,那么我将不得不研究绑定以避免SQL注入,但是到目前为止,我要说的是,看看该变量是否可以在查询中使用。 我已经试过了,双引号,串联。$ vx。,花括号
问题内容: 我需要解密从服务器接收到的一些数据,编写API的程序员将我定向到此Encrypter类,以查看他过去用于加密的内容。 现在基于该类,我发现所使用的算法是AES128 CBC,并且我接收到的字符串是Base64编码的,并且包含其他数据,而不仅仅是密文。 即,如果我收到以下字符串: 经过base64解码后,我得到: 基于的类(),I执行另一解码的base64上和,并得到了一个长度为16的那
问题内容: 我已经将laravel存储库克隆到了CentOS 7盒中。当我尝试运行它时,出现500错误,但未显示任何内容。 因此,我进行了检查,发现有一些权限错误: 我做了以下尝试来克服这些问题: 所以现在显示如下: 但这没有用。 有趣的是,我之前输入了一些错误的命令,这些命令似乎将日志添加到日志文件中。 问题答案: 我找到了答案,解决了我的问题。 通过关闭命令来证明这是问题所在 这应该允许写入,
问题内容: 数据库表中有一个类型列。但它没有按我预期的那样工作。 问题是 我需要在列中放入零,但每次尝试添加带有的记录时,它的价值都会增加。 我在这里发现了一个问题。。但是答案并没有说明问题的原因。很高兴有人可以解释这个问题。 问题答案: 具有类型字段意味着您每次插入/更新该字段时都需要使用原始值作为解决方法。 这是因为默认情况下,PDO将绑定这些值,并将它们视为字符串,因此将导致: 我建议将其更
问题内容: 所以我在laravel文件夹上使用了我的cmd,然后尝试这样做(php artisan migration:install)。出现2个错误。 [PDOException] SQLSTATE [HY000] [2006] MySQL服务器已消失 [ErrorException] PDO :: __ construct():MySQL服务器已消失 谁能解释我做错了什么? 问题答案: 这不是
问题内容: 我想通过表达式从表中获取所有行: 但是,如果该列包含日期时间,可以这样说: 但是如果我这样做: 它不会排。 我猜这是因为$ date = 2014-07-10,这使MySQL假设它是2014-07-10 00:00:00。 在普通的MySQL中,我会做 使用Laravel的口才相当于什么? 问题答案: Laravel 4+为您提供这些方法:,,(#3946)和(#6879)。 他们为您