Plivo 2 Factor Authentication (2FA) App
tl;dr
Click the button below to deploy the Plivo 2FA app directly to your heroku account:
About
This example shows how Plivo APIs can be used to integrate a two factor authentication system into your own web application. This example is built in Python using Flask application framework but the concept behind it language agnostic. So, be it Python, PHP, Ruby or Node, the concept remains the same.
The next section explains how the application works and in-turn how Plivo as a platform works. There is a separate section on deployment which explains how to deploy this application on Heroku.
How to use it
Here is a live demo of this sample application where you can try out how it works. This application verifies your phone number using the two factor authentication system. In the application, enter your phone number in E.164 format and click on 'Send Verification Code'. This sends an SMS to that number with a random security code in it. The application now shows a text box to enter this code to verify your mobile number. Once you get the code in the SMS, enter the code in the text box and click 'Check'. This will tell you whether the code you entered is correct or not. If you enter the correct code, then the application knows that the phone number belongs to you and thus the number is verified.
Requirements
- Git
- Python 2.7 with PIP and Virtualenv
- Redis Server
Requirements
- Git
- Python 2.7 with PIP and Virtualenv
- Redis Server
Running the application locally
You can run the app locally for testing by following these steps:
-
From a terminal window, run
git clone https://github.com/cachrisman/Plivo-2FA-App.git
-
Removing the current
.git
folder in the project root usingrm -rf .git/
. -
Create a
.env
file in the project root with the following contents:PLIVO_AUTH_ID=XXXXXXXXXXXXXXXXXXXX PLIVO_AUTH_TOKEN=YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY PLIVO_NUMBER=14155551234
PLIVO_AUTH_ID
andPLIVO_AUTH_TOKEN
can be found in the Plivo Dashboard homepage andPLIVO_NUMBER
should be set to a valid Plivo Number in your account from which you want to send the verification SMS. -
Install the redis server on your local machine:
- OS X with Homebrew: Run
brew install redis
to install, thenbrew services start redis
to start the server - OS X without Homebrew: follow the quickstart instructions on the redis website or google for
os x install redis -homebrew
for guides - Ubuntu: run
sudo apt-get update;sudo apt-get install redis-server
to install, thensudo service redis-server start
to start the server
- Create a virtual environment by running
virtualenv venv
. NOTE: if you don't havevirtualenv
installed, run the commandsudo pip install virtualenv
to installvirtualenv
- Activate the
virtualenv
usingsource ./venv/bin/activate
. - Install all the application's dependencies specified in
requirements.txt
usingpip install -r requirements.txt
. - Now you can run the app locally by running this command
python app.py
in the project root and browsing to http://localhost:5000 to see if it works properly. When done testing, pressCTRL+C
to stop and exit the python server.
Deployment on Heroku
Initial Setup
This section explains how to prepare your system to deploy this app to Heroku.
-
Create an account on Heroku (its free!).
-
Verify your Heroku account and add a credit card. This app doesn't require any paid addons, but you still need to add your credit card to use free 3rd party Heroku addons.
-
Install the Heroku toolbelt.
-
Login to heroku from the toolbelt using the
heroku login
command. If you do not have an ssh public key in your system, it prompts to automatically create it. Hit 'Y' when prompted.$ heroku login Enter your Heroku credentials. Email: charlie@plivo.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/charlie/.ssh/id_rsa.pub
-
Once this is done, you are ready to deploy the application.
The Actual Deployment
Click the button below to deploy directly to your heroku account:
or follow these instructions to deploy to heroku:
- Run
heroku create
to create an application on Heroku and adds it as a remote to the local git repo. - Run
heroku addons:create redistogo:nano -a YOUR_APP_NAME
(replace YOUR_APP_NAME with the name of the app created in step 1) to install the redistogo addon for this application. - Push the local code to the heroku repo for deployment using
git push heroku master
. - To run one web process as specified in the
Procfile
, run theheroku ps:scale web=1
command. - You can see if it is running by using the
heroku ps
command. It should return something likeweb.1: up for 5s
. - You can check (and watch) the application logs using
heroku logs -t
command. PressCTRL+C
to exit. - To open the application in the web browser, type
heroku open
and hitENTER
. - You'll see this application in the web browser.
Find the live demo of the application here. For more detailed information on deployment on heroku, visit the official heroku documentation. More information about Plivo APIs can be found in the offical API docs.
Helper libraries for various languages are available on the Plivo github page.