Skip to contentSkip to navigationSkip to topbar
On this page

Authy TOTP Ruby Quickstart


(warning)

Warning

As of November 2022, Twilio no longer provides support for Authy SMS/Voice-only customers. Customers who were also using Authy TOTP or Push prior to March 1, 2023 are still supported. The Authy API is now closed to new customers and will be fully deprecated in the future.

For new development, we encourage you to use the Verify v2 API.

Existing customers will not be impacted at this time until Authy API has reached End of Life. For more information about migration, see Migrating from Authy to Verify for SMS(link takes you to an external page).


Overview

overview page anchor

Authy is a REST API that does all the heavy lifting, so you can add two-factor authentication to your website or app in just a few hours. We have a number of resources, such as JavaScript helpers and API libraries for most languages. This guide will make use of these resources as we build a web app using the Authy API.

Full source code for this app can be found here(link takes you to an external page)


You will find everything you need in on GitHub(link takes you to an external page)


There are two main parts to implement an Authy API integration:

  • Adding new users: involves creating an HTML form that takes the user's phone number / email and submits it to the Authy API.
  • Verifying users: involves adding a field to the login form were the user enters the token which is in turn submitted to Authy for verification.

Part 1: Adding a new user

part-1-adding-a-new-user page anchor

First we are going to create a page in the users control panel (we will call it /enableauthy). On this page, we'll create a form that collects the phone number and area code.

This form uses the Authy JavaScript samples found in the /authy/authy-javascripts GitHub repository(link takes you to an external page). The HTML IDs authy-cellphone and authy-countries are special IDs recognized by this JavaScript.

1
%label
2
Cellphone
3
= text_field "cellphone", :placeholder => "Enter your cellphone", :id => "authy-cellphone"
4
5
%label
6
Country
7
= text_field "country_code", :id => "authy-countries", :placeholder => "Enter your country"
8
9
= submit "Submit", :class => "btn"

JavaScript ID tag helpers

javascript-id-tag-helpers page anchor

The scripts use IDs to help you build this form. These are the IDs used

  • authy-cellphone: If set on the text-field were the user inputs his cellphone, it will automatically run phone validations on the client-side.
  • authy-countries: If set on the text-field were the user inputs his area code, it will produce an auto-complete drop down with a list of countries and their area-codes. This simplifies the user entering his area-code.

At this point you can send the users email, phone number and country code to Authy.

1
def register_authy
2
@authy_user = Authy::API.register_user(
3
:email => current_user.email,
4
:cellphone => params[:user][:cellphone],
5
:country_code => params[:user][:country_code]
6
)
7
8
if @authy_user.ok?
9
current_user.authy_id = @authy_user.id
10
current_user.save
11
else
12
@errors = @authy_user.errors
13
render 'enable_authy'
14
end
15
end

If everything goes well, the API will return the user's Authy ID. This is represented as an integer which you need to store in your users database or directory. In the case of an error, a hash in plain English is returned. The language used is designed for you to display to the user to help them resolve the situation.

Part 2: Verifying users

part-2-verifying-users page anchor

After you have registered the user in the Authy service and stored the resulting Authy ID against the user's profile in your application, the next time you process the user login, you use the Authy ID to verify the two-factor token they should provide.

Implementing the request for a second factor during the login can vary depending on the design of your application and the end user experience you wish to create. In this example we plan to support users who opt-in for two-factor authentication. Therefore there will be a mix of user accounts that are enabled and some that are not. The easiest way to implement this is to separate the authentication process across two screens.

  1. The first screen is your usual login form where the user inputs username and password.
  2. The second screen the user inputs the Authy token, assuming they have opt-ed into Authy and they've gone through the user registration process.

Remember you need to manage the link between your user identities and the Authy user. Your database or user directory should ensure the AuthyID is stored with each user profile that has been registered.

Here's the function from our sample application that validates username and password and redirects to the two-factor authentication page if authy is enabled:

1
def create
2
@user = User.find_by_email(params[:user][:email])
3
4
if @user && @user.authenticate(params[:user][:password])
5
# username and password is correct
6
if(@user.authy_id != 0) #user is using two-factor
7
Authy::API.request_sms(:id => @user.authy_id) # request the API to send and sms.
8
9
# Rails sessions are tamper proof. We can store the ID and that the password was already validated
10
session[:password_validated] = true
11
session[:id] = @user.id
12
redirect_to url_for(:controller => "sessions", :action => "two_factor_auth")
13
else
14
sign_in(@user)
15
flash[:notice] = "Successfully authenticated without two-factor"
16
redirect_to @user
17
end
18
else
19
flash[:error] = "Wrong username, password."
20
@user = User.new
21
render 'new'
22
end
23
end

When the user wants to login, you use their email (or whatever user identifier you are using) to locate their user profile in the database or directory. The authenticate function will check username and password and if the user has been Authy enabled. If so, redirect the user to the second page were they enter the token and complete the authentication. Here's the function for the second factor authentication:

1
def create_two_factor_auth
2
@user = User.find(session[:id])
3
token = params[:token]
4
if @user && session[:password_validated] && @user.verify_token(token)
5
sign_in(@user)
6
@user.authy_used = true
7
@user.save(:validate => false)
8
session[:password_validated] = nil
9
session[:id] = nil
10
flash[:success] = "Securely signed in using Authy"
11
redirect_to @user
12
else
13
flash[:error] = "Wrong token"
14
redirect_to new_session_path
15
end
16
end

Token verification involves sending the token and the id. Authy will respond HTTP status 200 if the token is correct.

1
def verify_token(token)
2
token = Authy::API.verify(:id => self.id, :token => token)
3
4
token.ok?
5
end

The function passes the authy_id and the token the user entered. Then it check if the response is HTTP status 200, if so it returns true.

To prevent users account getting locked down, Authy won't check the tokens until we are certain the user has completed the registration process. If you want to verify token's anyway, you can pass ?force=true to verify.