在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):antonioribeiro/google2fa-laravel开源软件地址(OpenSource Url):https://github.com/antonioribeiro/google2fa-laravel开源编程语言(OpenSource Language):PHP 99.8%开源软件介绍(OpenSource Introduction):Google2FA for LaravelGoogle Two-Factor Authentication Package for LaravelGoogle2FA 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 codesif 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 & PlaygroundPlease 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. Compatibility
Before Google2FA 2.0 (Laravel 5.1) you have to install DemoClick here to see the middleware demo: InstallingUse Composer to install it:
Installing on LaravelLaravel 5.5 and aboveYou 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 belowAdd the Service Provider and Facade alias to your PragmaRX\Google2FALaravel\ServiceProvider::class,
'Google2FA' => PragmaRX\Google2FALaravel\Facade::class, Publish the config filephp artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider" Using ItUse the Facadeuse Google2FA;
return Google2FA::generateSecretKey(); In Laravel you can use the IoC Container$google2fa = app('pragmarx.google2fa');
return $google2fa->generateSecretKey(); MiddlewareThis 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']); QRCodeThis 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 BackendThere 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 viewYou 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 LifetimeUsually 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 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 2FaThis 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(); Throttling / Lockout after X attemptsUnless 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,
]; EventsThe following events are fired:
DocumentationCheck the ReadMe file in the main Google2FA repository. TestsThe package tests were written with phpspec. AuthorLicenseGoogle2FA is licensed under the MIT License - see the LICENSE file for details ContributingPull requests and issues are more than welcome. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论