Google2FA for Laravel
, (*1)
Google Two-Factor Authentication for PHP 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., (*2)
This package is a Laravel bridge to Google2FA's PHP package., (*3)
Demos, Example & Playground
Please check the Google2FA Package Playground., (*4)
, (*5)
Here's an demo app showing how to use Google2FA: google2fa-example., (*6)
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., (*7)
Requirements
Compatibility
This package is compatible with, (*8)
Installing
Use Composer to install it:, (*9)
composer require pragmarx/google2fa-laravel
If you prefer inline QRCodes instead of a Google generated url, you'll need to install BaconQrCode:, (*10)
composer require "bacon/bacon-qr-code":"~1.0"
Installing on Laravel
Laravel 5.5
You don't have to do anything else, this package autoloads the Service Provider and create the Alias, using the new Auto-Discovery feature., (*11)
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):, (*12)
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:, (*13)
Demo
Click here to see the middleware demo:, (*14)
, (*15)
Using the middleware
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']);
Configuring the view
You can set your 'ask for a one time password' view in the config file (config/google2fa.php):, (*16)
/**
* 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:, (*17)
/**
* One Time Password request input name
*/
'otp_input' => 'one_time_password',
Here's a form example:, (*18)
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:, (*19)
/**
* 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
And you can decider whether your OTP will be kept alive while your users are browsing the site or not:, (*20)
/**
* Renew lifetime at every new request.
*/
'keep_alive' => true,
Manually logging out from 2Fa
This command wil logout your user and redirect he/she to the 2FA form on the next request:, (*21)
Google2FA::logout();
If you don't want to use the Facade, you may:, (*22)
use PragmaRX\Google2FALaravel\Support\Authenticator;
(new Authenticator(request()))->logout();
Documentation
Check the ReadMe file in the main Google2FA repository., (*23)
Tests
The package tests were written with phpspec., (*24)
Author
Antonio Carlos Ribeiro, (*25)
License
Google2FA is licensed under the BSD 3-Clause License - see the LICENSE file for details, (*26)
Contributing
Pull requests and issues are more than welcome., (*27)