Two-factor authentication using Ruby

    Two-factor authentication (2FA) can play a key role in securing your applications against password data breaches. Authentication with a one-time password (OTP) delivered to your users over SMS is an effective approach to implementing two-factor authentication. Plivo’s premium direct routes guarantee the highest possible delivery rates and the shortest possible delivery times for your 2FA SMS messages.

    This guide shows how to set up SMS-based two-factor authentication using either PHLO or traditional API development. PHLO lets you create and deploy workflows from an intuitive graphical canvas in few clicks.

    To implement Two-factor use case, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for SMS you send/receive, and building with PHLO is free.

    Implementation

    In this section, we will guide you to create a PHLO to implement Two-factor use-case.

    Prerequisites

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number(Optional): To send messages to the United States and Canada, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. This number will also help you receive incoming SMS as you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    • Github account(Optional): To get the code from the repository, clone it. Otherwise, since the repository is open to the public, we can download the code and run it locally.

    Create the PHLO

    With PHLO, you can quickly create a workflow that suits your use case. To use PHLO, make sure to register and log on to Plivo Console. There is already a prototype for this use-case; all you need to do is select the PHLO and give it a friendly name.

    PHLO Setup

    Set up Your Ruby Dev Environment

    You must set up and install Ruby and Plivo’s Ruby SDK to make a bulk call. Here’s how.

    Install Ruby

    Operating SystemInstructions
    macOS & LinuxYou would already have Ruby installed, you can check this by running the command ruby --version in the terminal. If you don't have it installed, you can install it using homebrew.
    WindowsTo install Ruby on Windows you can download it from here and install.

    Set Up the Demo app locally

    • Clone the repository from Github
     $ git clone https://github.com/plivo/2fa-ruby-demo.git
    • Change your working directory to 2fa-ruby-demo
     $ cd 2fa-ruby-demo
    • Install the dependencies using the Gemfile file. You can use the below command.
     $ bundle install
    • Change the placeholders in the config.yaml file. You should replace the PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, PLIVO_NUMBER & PHLO_IDplaceholders. Configuration file
    Note: Enter your phone number in E.164 format.
    • Turn on the redis server by entering the following command in your terminal
     $ redis-server

    Redis Server

    • The different steps that are involved in this app are as follows:

    Step 1 : Generate the OTP

    Generate an exclusive six-digit authentication code (OTP). To create the OTP, we will use the Time Based OTP generation algorithm. here’s how it’s done in Ruby.

    1
    
    code = rand(999_999)
    

    Step 2 : Send SMS & Make a call

    A single function help us to trigger to Send SMS and Make call via PHLO and the rest is done by the PHLO in your console. The main argument which tells PHLO to trigger call or an SMS is mode the values passed within are sms & call.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    def initiate_phlo(dst_number, mode)
        code = rand(999_999)
        begin
          phlo = @phloclient.phlo.get(@phlo_id)
          # parameters set in PHLO - params
          params = {
            from: @app_number,
            to: dst_number,
            otp: code,
            mode: mode
          }
          phlo.run(params)
          code
        rescue PlivoRESTError => e
          puts 'Exception: ' + e.message
        end
      end
    

    Step 3 : Verify the OTP

    Once the user enters the OTP received to their handset, the code will be verified and here’s how it’s done in Ruby.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    get '/verify/:number' do
      number = params['number']
      ##
      # verify(number) accepts a number and initiates verification for it.
    #
      code = if config['phlo_id'].nil?
               twofactor.send_verification_code_sms(number, 'Your verification code is __code__. Code will expire in 1 minute.')
             else
               twofactor.initiate_phlo(number, 'sms')
             end
      r.setex('number:%s:code' % number, 60, code) # Verification code is valid for 1 min
      content_type :json
      { status: 'success', message: 'verification initiated' }.to_json
    end
    

    Test and Validate

    In order to run the app, run the following command.

    $ ruby app.rb
    

    You check the app in action on https://3b3e783f.ngrok.io/

    The finished app should look like this. Two-Factor Authentication

    Note: If you are using a Plivo Trial account for this example, you can only send SMS to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    To implement two Factor Authentication use-case in the traditional API way, you can refer to the instructions in the below section to begin your implementation.

    Implementation

    In this section, we will guide you in setting up an app using Plivo’s API to implement two factor authentication. First, let’s make sure you meet these prerequisites before we dive into the code.

    Prerequisites

    • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    • Plivo Phone Number(Optional): To send messages to the United States and Canada, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. This number will also help you receive incoming SMS as you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    • Github account(Optional): To get the code from the repository, clone it. Otherwise, since the repository is open to the public, we can download the code and run it locally.

    Set up Your Ruby Dev Environment

    You must set up and install Ruby and Plivo’s Ruby SDK to implement Send SMS use-case. Here’s how.

    Install Ruby

    Operating SystemInstructions
    macOS & LinuxYou would already have Ruby installed, you can check this by running the command ruby --version in the terminal. If you don't have it installed, you can install it using homebrew.
    WindowsTo install Ruby on Windows you can download it from here and install.

    Set Up the Demo app locally

    • Clone the repository from Github
     $ git clone https://github.com/plivo/2fa-ruby-demo.git
    • Change your working directory to 2fa-ruby-demo
     $ cd 2fa-ruby-demo
    • Install the dependencies using the Gemfile file. You can use the below command.
     $ bundle install
    • Change the placeholders in the config.yaml file. You should replace the PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN & PLIVO_NUMBER placeholders. Configuration file
  • Note: Enter your phone number in E.164 format.
  • Note: In case if you wouldn't like to you use PHLO then update the value as PHLO_ID = null.
    • The different steps that are involved in this app are as follows:

    Step 1 : Generate the OTP

    Generate an exclusive six-digit authentication code (OTP). To create the OTP, we will use the Time Based OTP generation algorithm. here’s how it’s done in Ruby.

    1
    
    code = rand(999_999)
    

    Step 2 : Send SMS message with OTP

    Send SMS with OTP to the user’s registered mobile number using Plivo’s Send Message API.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    def send_verification_code_sms(dst_number, message)
        code = rand(999_999)
        @client.messages.create(
          @app_number,
          [dst_number],
          message.gsub('__code__', code.to_s)
        )
        code
      rescue PlivoRESTError => e
        puts 'Exception: ' + e.message
      end
    

    Step 3 : Make a phone call with OTP(Failover)

    When messages aren’t deliverable for a variety of reasons, the user can choose voice OTP, and here’s how it’s done in Ruby.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    def send_verification_code_call(dst_number)
        code = rand(999_999)
        @client.calls.create(
          @app_number,
          [dst_number],
          "https://twofa-answerurl.herokuapp.com/answer_url/#{code}"
        )
        code
      rescue PlivoRESTError => e
        puts 'Exception: ' + e.message
      end
    

    Step 4 : Verify the OTP

    Once the user enters the OTP received to their handset, the code will be verified and here’s how it’s done in Ruby.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    get '/verify/:number' do
      number = params['number']
      ##
      # verify(number) accepts a number and initiates verification for it.
    #
      code = if config['phlo_id'].nil?
               twofactor.send_verification_code_sms(number, 'Your verification code is __code__. Code will expire in 1 minute.')
             else
               twofactor.initiate_phlo(number, 'sms')
             end
      r.setex('number:%s:code' % number, 60, code) # Verification code is valid for 1 min
      content_type :json
      { status: 'success', message: 'verification initiated' }.to_json
    end
    

    Test and Validate

    In order to run the app, run the following command.

    $ ruby app.rb
    

    You check the app in action on https://3b3e783f.ngrok.io/

    The finished app should look like this. Two-Factor Authentication

    Note: If you are using a Plivo Trial account for this example, you can only send SMS to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.