An example of how to implement two factor authentication using Duo Security and Laravel 4.
Once complete, a user will be asked for their username and password, which is authenticated by Laravel, then if successful, they will be shown a prompt by Duo Security which will require a second kind of authentication. If that is also authenticated, the user will be logged in by Laravel and redirected to the homepage.
There are therefore 3 stages:
- Laravel login page
- Duo Security login page
- Authenticated homepage
-
Duo Security is a service that offers a way to protect a site using two factor authentication. You can find their PHP code here which this example repo extends in a minor way.
I am not affiliated in any way to either.
This repo is based on a fresh version of Laravel 4, so to recreate this implementation, i would recommend you start with the same from here and follow the steps listed below. This repo is a tutorial rather than a finished product to plug in.
-
Stage One 
-
Stage Two 
-
iOS Notification 
-
Stage Three 
-
Sign up for a Duo Security account then create a new Web SDK integration. Note the following which you will require later
- Integration key
- Secret key
- API hostname
-
Clone a new instance of Laravel
-
Run the following in Terminal
composer install -
Set up some kind of database (I used mySQL) and add the relevant credentials to
app/config/database.php -
Run the following artisan command to generate the migration for a Users table, which we will use to authenticate our user against -
php artisan migrate:make create_users_table -
Open
app/database/migrations/<the date you ran the command>_create_users_table.phpand add the code shown in the file of the same name from this repo which scaffolds a users table. -
Run the following artisan command
php artisan migrate -
Open
app/database/seeds/DatabaseSeeder.phpand add the code shown in the file of the same name from this repo which prepares the file to add one user we can authenticate against -
Run the following artisan command
php artisan db:seed- If you check your DB now, there should be one user in the users table, with a username ofsam@laravelduo.co.uk, and a password ofpassword(which has been helpfully hashed by Laravel) -
Add a new folder at
app/LaravelDuoand add the two files from this repo from the same location,Duo.php(available here) andLaravelDuo.php -
Open
app/LaravelDuo/LaravelDuo.phpand add add the Intergration keyIKEY, Secret KeySKEYand HostHOSTvalues from your Duo Security account, and create an Application KeyAKEY -
Open
composer.jsonand addapp/LaravelDuoto theclassmaplist as shown in thecomposer.jsonof this repo -
Run the following artisan command
composer dumpautoload -
Open
app/routes.php, delete the standard routing for ('/') and add the followingRoute::controller('/', 'HomeController');. This will RESTfully route our various page requests throughapp/controllers/HomeController.php -
Create
app/views/layouts/master.blade.phpand add the code shown in the file of the same name from this repo. This uses Laravel's Blade syntax and is the outer structure for every page. -
Create
app/views/pages/login.blade.phpandapp/views/pages/duologin.blade.phpand copy the code show in the files of the same name from this repo.login.blade.phpis the page shown initally in which we authenticate a user against theUserstable of our database.duologin.blade.phpis the page shown subsequently which allows authentication with Duo. -
Create
public/assets/css/style.cssand add any styling you want, andapp/assets/js/Duo-Web-v1.bundled.min.js(available here) -
Open
app/controllers/HomeController.phpand add the code from file of the same name from this repo. -
Open
app/models/User.phpand add thegetIdFromEmail()static method from the file of the same name from this repo. -
Browse to your webroot (in my case
http://localhost:8888/LaravelDuo/public) and entersam@laravelduo.co.ukin the email field andpasswordin the password field. -
Follow the Duo security instructions to authenticate using their service
-
Win