2017 © Pedro Peláez
 

library google2fa-laravel

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

image

pragmarx/google2fa-laravel

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

  • Wednesday, August 1, 2018
  • by AntonioCarlosRibeiro
  • Repository
  • 14 Watchers
  • 227 Stars
  • 104,264 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 44 Forks
  • 12 Open issues
  • 11 Versions
  • 45 % Grown

The README.md

Google2FA for Laravel

Latest Stable Version License Code Quality Build , (*1)

Downloads Coverage StyleCI PHP , (*2)

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., (*3)

This package is a Laravel bridge to Google2FA's PHP package., (*4)

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., (*5)

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., (*6)

Demos, Example & Playground

Please check the Google2FA Package Playground., (*7)

playground, (*8)

Here's an demo app showing how to use Google2FA: google2fa-example., (*9)

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., (*10)

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)., (*11)

Demo

Click here to see the middleware demo:, (*12)

middleware, (*13)

Installing

Use Composer to install it:, (*14)

composer require pragmarx/google2fa-laravel

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., (*15)

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):, (*16)

``` php PragmaRX\Google2FALaravel\ServiceProvider::class,, (*17)

'Google2FA' => PragmaRX\Google2FALaravel\Facade::class,, (*18)


## Publish the config file ``` php php artisan vendor:publish --provider="PragmaRX\Google2FALaravel\ServiceProvider"

Using It

Use the Facade

``` php use Google2FA;, (*19)

return Google2FA::generateSecretKey();, (*20)


#### In Laravel you can use the IoC Container ``` php $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:, (*21)

Add the middleware to your Kernel.php:

``` php protected $routeMiddleware = [ ... '2fa' => \PragmaRX\Google2FALaravel\Middleware::class, ];, (*22)


### Using it in one or more routes: ``` php 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., (*23)

Imagick QRCode Backend

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

You can change it via config:, (*25)

``` php /* * 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,, (*26)


Or runtime: ``` php Google2FA::setQRCodeBackend('svg');

Configuring the view

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

``` php /** * One Time Password View */ 'view' => 'google2fa.index',, (*28)


And in the view you just have to provide a form containing the input, which is also configurable: ``` php /** * One Time Password request input name */ 'otp_input' => 'one_time_password',

Here's a form example:, (*29)



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:, (*30)

``` php /** * Lifetime in minutes. * In case you need your users to be asked for a new one time passwords from time to time. */, (*31)

'lifetime' => 0, // 0 = eternal, (*32)

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:

``` php
/**
 * 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:, (*33)

``` php Google2FA::logout();, (*34)


If you don't want to use the Facade, you may: ``` php 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:, (*35)

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:, (*36)

``` php protected $routeMiddleware = [ ... '2fa' => \PragmaRX\Google2FALaravel\MiddlewareStateless::class, ];, (*37)


## 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``: ``` php 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:, (*38)

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

Documentation

Check the ReadMe file in the main Google2FA repository., (*39)

Tests

The package tests were written with phpspec., (*40)

Author

Antonio Carlos Ribeiro, (*41)

License

Google2FA is licensed under the MIT License - see the LICENSE file for details, (*42)

Contributing

Pull requests and issues are more than welcome., (*43)

The Versions

01/08 2018

dev-master

9999999-dev

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

08/03 2018

v0.2.0

0.2.0.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

08/03 2018

dev-analysis-86OmeM

dev-analysis-86OmeM

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

08/03 2018

dev-analysis-XamDK0

dev-analysis-XamDK0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

08/03 2018

dev-analysis-XZmjJP

dev-analysis-XZmjJP

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

27/02 2018

dev-analysis-zdpbLw

dev-analysis-zdpbLw

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

06/12 2017

v0.1.4

0.1.4.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

06/12 2017

v0.1.3

0.1.3.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

23/06 2017

v0.1.2

0.1.2.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

22/06 2017

v0.1.1

0.1.1.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa

20/06 2017

v0.1.0

0.1.0.0

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

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

laravel authentication two factor authentication google2fa