2017 © Pedro Peláez
 

library authy

Plugin for enabling two-factor authentication in Laravel applications

image

srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  • Tuesday, September 5, 2017
  • by srmklive
  • Repository
  • 8 Watchers
  • 41 Stars
  • 11,350 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 15 Versions
  • 14 % Grown

The README.md

Laravel Two-Factor Authentication

Software License Latest Version on Packagist Total Downloads StyleCI Scrutinizer Code Quality SensioLabsInsight, (*1)

, (*2)

Introduction

This plugins allows you to enable two-factor authentication in your Laravel applications., (*3)

Only Laravel 5.1 or greater supported, (*4)

, (*5)

Installation

  • Use following command to install:
composer require srmklive/authy
  • Add the service provider to your $providers array in config/app.php file like:
Srmklive\Authy\Providers\AuthyServiceProvider::class
  • Add the alias to your $aliases array in config/app.php file like:
'Authy' => Srmklive\Authy\Facades\Authy::class
  • Run the following command to publish configuration:
php artisan vendor:publish --provider "Srmklive\Authy\Providers\AuthyServiceProvider"
  • Run the following command to migrate user table changes to database:
php artisan migrate
  • Add the following lines in your User model (e.g App\User.php), (*6)

    • Before the class declaration, add these lines:
use Srmklive\Authy\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatableContract;
  • Now the change the class declaration. For example, if your class declaration is
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract

then change it to this:, (*7)

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract,
                                    TwoFactorAuthenticatableContract
  • Now change the import traits line accordingly in user model file. For example if the line is:
use Authenticatable, Authorizable, CanResetPassword;

to, (*8)

use Authenticatable, Authorizable, CanResetPassword, TwoFactorAuthenticatable;
  • Lastly, add/update $hidden variable to hide 'two_factor_options' field from any DB call for user detail:
protected $hidden = [
    'two_factor_options'
];

, (*9)

Modifying Login Workflow

  • You need to add the following code to your app\Http\Controllers\Auth\AuthController.php.

/** * Send the post-authentication response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return \Illuminate\Http\Response */ protected function authenticated(Request $request, Authenticatable $user) { if (Authy::getProvider()->isEnabled($user)) { return $this->logoutAndRedirectToTokenScreen($request, $user); } return redirect()->intended($this->redirectPath()); } /** * Generate a redirect response to the two-factor token screen. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return \Illuminate\Http\Response */ protected function logoutAndRedirectToTokenScreen(Request $request, Authenticatable $user) { // Uncomment this line for Laravel 5.2+ //auth($this->getGuard())->logout(); // Uncomment this line for Laravel 5.1 // auth()->logout(); $request->session()->put('authy:auth:id', $user->id); return redirect(url('auth/token')); } /** * Show two-factor authentication page * * @return \Illuminate\Http\Response|\Illuminate\View\View */ public function getToken() { return session('authy:auth:id') ? view('auth.token') : redirect(url('login')); } /** * Verify the two-factor authentication token. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postToken(Request $request) { $this->validate($request, ['token' => 'required']); if (! session('authy:auth:id')) { return redirect(url('login')); } // Uncomment these lines for use in Laravel 5.2+ //$guard = config('auth.defaults.guard'); //$provider = config('auth.guards.' . $guard . '.provider'); //$model = config('auth.providers.' . $provider . '.model'); // Uncomment the line below for use in Laravel 5.1 // $model = config('auth.model'); $user = (new $model)->findOrFail( $request->session()->pull('authy:auth:id') ); if (Authy::getProvider()->tokenIsValid($user, $request->token)) { // Uncomment this line for Laravel 5.2+ //auth($this->getGuard())->login($user); // Uncomment this line for Laravel 5.1 //auth()->login($user); return redirect()->intended($this->redirectPath()); } else { return redirect(url('login'))->withErrors('Invalid two-factor authentication token provided!'); } }
  • Add route to verify two-factor authentication token
Route::get('auth/token','Auth\AuthController@getToken');
Route::post('auth/token','Auth\AuthController@postToken');
  • Create view file in resources/views/auth/token.blade.php. Change this accordingly for your application. I have used code from AdminLTE theme here.
@extends('layouts.app')

@section('content')






    <div class="register-box-body">
        <p class="login-box-msg">Validate your two-factor authentication token</p>
        <form method="POST" action="{{url('auth/token')}}">
            {!! csrf_field() !!}

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <div class="form-group has-feedback">
                <input type="type" name="token" class="form-control" placeholder="Token">
                <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-7"></div><!-- /.col -->
                <div class="col-xs-5">
                    <button type="submit" class="btn btn-primary btn-block btn-flat">Verify Token</button>
                </div><!-- /.col -->
            </div>
        </form>
    </div><!-- /.form-box -->
@endsection

, (*10)

Usage

  • Registering User
$phone = '405-342-5699';
$code = 1;

$user = User::find(1);

$user->setAuthPhoneInformation(
    $code, $phone
);

try {
   Authy::getProvider()->register($user);

   $user->save();
} catch (Exception $e) {
   app(ExceptionHandler::class)->report($e);

   return response()->json(['error' => ['Unable To Register User']], 422);
}
  • Send token via SMS
$user = User::find(1);

try {
   Authy::getProvider()->sendSmsToken($user);
} catch (Exception $e) {
   app(ExceptionHandler::class)->report($e);

   return response()->json(['error' => ['Unable To Send 2FA Login Token']], 422);
}
  • Send token via phone call
$user = User::find(1);

try {
   Authy::getProvider()->sendPhoneCallToken($user);
} catch (Exception $e) {
   app(ExceptionHandler::class)->report($e);

   return response()->json(['error' => ['Unable To Send 2FA Login Token']], 422);
}
  • Validating two-factor token
$user = User::find(1);

try {
   Authy::getProvider()->tokenIsValid($user, $token);
} catch (Exception $e) {
   app(ExceptionHandler::class)->report($e);

   return response()->json(['error' => ['Invalid 2FA Login Token Provided']], 422);
}
  • Deleting User
$user = User::find(1);

try {
   Authy::getProvider()->delete($user);

   $user->save();
} catch (Exception $e) {
   app(ExceptionHandler::class)->report($e);

   return response()->json(['error' => ['Unable to Delete User']], 422);
}

, (*11)

Add a new TwoFactor Authentication Provider

Currently this package uses two-factor authentication services from Authy. You can also implement another two-factor authentication provider by doing the following:, (*12)

<?php

namespace App\Services;

use Exception;
use GuzzleHttp\Client as HttpClient;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Provider as BaseProvider;
use Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable as TwoFactorAuthenticatable;

class MyAuthProvider implements BaseProvider
{
    /**
     * Array containing configuration data.
     *
     * @var array $config
     */
    private $config;

    /**
     * Authy constructor.
     */
    public function __construct()
    {
        // Add your configuration code here
    }

    /**
     * Determine if the given user has two-factor authentication enabled.
     *
     * @param  \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
     * @return bool
     */
    public function isEnabled(TwoFactorAuthenticatable $user)
    {
        // Add your code here
    }

    /**
     * Register the given user with the provider.
     *
     * @param  \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
     * @param boolean $sms
     * @return void
     */
    public function register(TwoFactorAuthenticatable $user, $sms = false)
    {
        // Add your code here
    }

    /**
     * Determine if the given token is valid for the given user.
     *
     * @param  \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
     * @param  string  $token
     * @return bool
     */
    public function tokenIsValid(TwoFactorAuthenticatable $user, $token)
    {
        // Add your code here
    }

    /**
     * Delete the given user from the provider.
     *
     * @param  \Srmklive\Authy\Contracts\Auth\TwoFactor\Authenticatable $user
     * @return bool
     */
    public function delete(TwoFactorAuthenticatable $user)
    {
        // Add your code here
    }
}

, (*13)

Demo Application

I have also implemented this package in a simple laravel application. You can view installation instructions here. Through this application, you can do:, (*14)

  • User login & registration.
  • Enable/Disable two-factor authentication for a user.

The Versions

05/09 2017

dev-master

9999999-dev https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

01/09 2017

v0.5.9

0.5.9.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

09/02 2017

v0.5.8

0.5.8.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

15/08 2016

v0.5.7

0.5.7.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

15/08 2016

v0.5.6

0.5.6.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

14/08 2016

v0.5.5

0.5.5.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

19/04 2016

v0.4.0

0.4.0.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

12/04 2016

v0.3.1

0.3.1.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

30/12 2015

v0.3.0

0.3.0.0 https://github.com/srmklive/laravel-twofactor-authentication

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

17/12 2015

v0.2.1

0.2.1.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

17/12 2015

v0.2.0

0.2.0.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

17/12 2015

v0.1.3

0.1.3.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

13/12 2015

v0.1.2

0.1.2.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

07/12 2015

v0.1.1

0.1.1.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication

04/12 2015

v0.1

0.1.0.0 https://github.com/srmklive/authy

Plugin for enabling two-factor authentication in Laravel applications

  Sources   Download

MIT

The Requires

 

by Raza Mehdi

api authentication two-factor authy laravel two-factor authentication