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 One](http://3cb7c06ccb1e0b84a1cc-164f999ecb835605bbbe5f068924d4ed.r17.cf3.rackcdn.com/Screen Shot 2014-02-19 at 21.37.24.png)
-
Stage Two ![Stage Two](http://3cb7c06ccb1e0b84a1cc-164f999ecb835605bbbe5f068924d4ed.r17.cf3.rackcdn.com/Screen Shot 2014-02-19 at 21.37.48.png)
-
iOS Notification ![iOS Notification](http://3cb7c06ccb1e0b84a1cc-164f999ecb835605bbbe5f068924d4ed.r17.cf3.rackcdn.com/Screen Shot 2014-02-19 at 18.36.10.png)
-
Stage Three ![Stage Three](http://3cb7c06ccb1e0b84a1cc-164f999ecb835605bbbe5f068924d4ed.r17.cf3.rackcdn.com/Screen Shot 2014-02-19 at 21.39.12.png)
-
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.php
and 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.php
and 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 of[email protected]
, and a password ofpassword
(which has been helpfully hashed by Laravel) -
Add a new folder at
app/LaravelDuo
and add the two files from this repo from the same location,Duo.php
(available here) andLaravelDuo.php
-
Open
app/LaravelDuo/LaravelDuo.php
and add add the Intergration keyIKEY
, Secret KeySKEY
and HostHOST
values from your Duo Security account, and create an Application KeyAKEY
-
Open
composer.json
and addapp/LaravelDuo
to theclassmap
list as shown in thecomposer.json
of 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.php
and 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.php
andapp/views/pages/duologin.blade.php
and copy the code show in the files of the same name from this repo.login.blade.php
is the page shown initally in which we authenticate a user against theUsers
table of our database.duologin.blade.php
is the page shown subsequently which allows authentication with Duo. -
Create
public/assets/css/style.css
and add any styling you want, andapp/assets/js/Duo-Web-v1.bundled.min.js
(available here) -
Open
app/controllers/HomeController.php
and add the code from file of the same name from this repo. -
Open
app/models/User.php
and 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 enter[email protected]
in the email field andpassword
in the password field. -
Follow the Duo security instructions to authenticate using their service
-
Win