Menu

Expand
Rate this page:

Verify TOTP Quickstart

Verify TOTP is in Pilot

This Verify feature is currently in the Pilot maturity stage, which means that:

1. We're actively looking for early-adopter customers to try it out and give feedback. That could be you!

2. You'll need to contact sales to request access to the API.

3. We're actively developing this feature and it could change/break unexpectedly; please only use in a test environment, not in production.

Verify TOTP adds the standards-compliant TOTP (Soft Token) channel to the Verify API, so that you can let your users use authenticator apps like Authy, Google Authenticator, or your own custom app for authentication. It’s great for businesses looking for a more secure, private, and lower-cost user authentication option compared to SMS OTP. Unlike the existing Authy API TOTP feature, no PII will be required and more registration options beyond QR code are supported.

There are three main steps to use Verify TOTP:

  • Register a user by generating an RFC-6238 compliant seed.
  • Verify that the user correctly added the seed (for example via QR code) to their Authenticator App
  • To verify a registered user, check that the code a user provided matches the code generated by the unique seed.

This Quickstart will walk you through the entire process step-by-step, starting with setting up your Twilio account all the way through verifying a user.

In this Quickstart, you will learn how to:

  1. Setup
    • Sign up for Twilio
    • Create a Verify Service
  2. Register a user and TOTP seed
    • Generate a TOTP seed
    • Generate a QR code
    • Verify a successful registration
  3. Verify a user
    • Create a Challenge
    • Update the Challenge

By the end of this Quickstart, you’ll have a solid foundation for implementing Verify TOTP within your app and backend to verify users at login, transaction, and other sensitive actions.

Want a technical overview first?

Technical Overview

Check out the Verify TOTP Technical Overview to view its data model and sequence diagrams

Sign up for - or sign in to - Twilio

Already have a Twilio account? Go ahead and skip this section.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity.
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, an authentication token, create a Verify Service and more.

Create a Verify Service

Go to Twilio Console > Verify > Services and create a new Service:

Screen Shot 2020-10-02 at 11.19.35 AM (1).jpg

Alternatively, you can select any existing Service. However, we recommend creating a new service for testing so you don't accidentally interfere with a production environment.

Register a user and TOTP seed

To use Verify TOTP, you first register a user by generating an RFC-6238 compliant seed. Then you verify that they've correctly added it to their Authenticator App for generating codes.

Create a new TOTP factor

The security of TOTP relies on a shared secret between your app and your user, and this is known as the seed. Register a user by creating a TOTP factor, which generates a seed based on a set of default configs and stores it. A corresponding Entity representing your user is also auto-created if it didn’t exist.

When creating a TOTP factor, you can optionally pass your own seed (useful for migration scenarios) and modify the default configs. See the Factor resource for reference. Some default configs are also adjustable at the Service resource level, such that all future Factors use them. Type or paste the code sample for this step.

        
        
        

        Do not use Personally Identifiable Information for identity. Use an immutable user identifier like a UUID, GUID, or SID.
        Verify TOTP uses identity as a unique identifier of a user. You should not use directly identifying information (aka personally identifiable information or PII) like a person's name, home address, email or phone number, etc., as identity because the systems that will process this attribute assume it is not directly identifying information.

        Use caution when changing default configs
        The default configs for the TOTP code (Config.CodeLength, Config.Skew, Config.TimeStep, and Config.Alg) have been chosen for a typical TOTP use case with a 6-digit length code. Please use caution when changing these configs, as it could reduce the security of the code. For example, reducing the length of the TOTP code (Config.CodeLength) makes the code easier to guess and more vulnerable to a brute force attack. While a shorter length may be necessary for your use case, consider compensating security enhancements, such as limiting the rate at which codes can be checked, reducing Config.Skew, and reducing Config.TimeStep. Another factor to consider is that publicly available authenticator apps that your users might have don't support non-default configs for all mobile OSs; see this 3rd-party analysis of authenticator apps for details. Contact Twilio for further guidance.

        Create a QR code

        The user needs to add the seed, and optionally metadata like Issuer and Account Name, to an authenticator app like Google Authenticator, Twilio Authy, or a custom client app that you’ve provided to your user. This authenticator app needs to securely store the seed and generate TOTP codes using it. A typical way for the user to add the seed is via scanning a QR code. Verify TOTP generates a standard URI (factor.binding.uri) that you can convert into a QR code for this purpose. Verify TOTP does not generate the QR code itself, but there are many free/open-source QR code generators.

        For this quickstart, use qr-code-generator.com to quickly generate a QR code by pasting the URI.

        For a real implementation, you could use one of these open-source code libraries:

        Scan and add this QR with your favorite authenticator app to mimic what a user would do.

        Example

        Using this URI:

        otpauth://totp/Twilio:John%E2%80%99s%20Account%20Name?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=Twilio&algorithm=SHA1&digits=6&period=30

        Generates this QR code using qr-code-generator.com:

        qr_code.png

        Scanning this QR using the Twilio Authy app generates this token:

        IMG_858FC9886A0B-1 (1).jpeg

        How to share the seed without a QR code

        Instead of a QR code, you can pass the seed to the user’s authenticator app by displaying it for manual entry by the user (supported by most commercial authenticator apps), including it in a deep link, or sending it via another authenticated channel.

        Verify that the user has successfully registered

        When the Factor is first created, it's in an “unverified” status. To verify the Factor, the user needs to demonstrate that they are able to generate a correct code based off of the seed.

        Use the code generated by your authenticator app and type or paste the code sample for this step.

        Be sure to update the AuthPayload value with your latest TOTP code before sending the API request. The expiration window (Config.TimeStep) of the TOTP code defaults to 30 seconds.

              
              
              

              The Factor SID (YFXXX..) is included in the response when you create a Factor. If you don't have the Factor SID, you can List Factors for your user’s Entity, and identify the one that is factor_type = totp.

                    
                    
                    

                    Verify a user

                    Congratulations! You've just completed the first Verify TOTP sequence: register user and TOTP seed. The second sequence is to verify (authenticate) a user with their authenticator app, whenever they need to login to your app or perform some other sensitive action. Read on for the step-by-step instructions.

                    Validate a token

                    To verify a user, the user needs to read the latest code generated by their authenticator app and provide it to you. Then you need to create a Challenge and include the code as the AuthPayload. If the code is correct, then the Challenge will be created with a status of approved. Incorrect codes will be created with a status of pending. Type or paste the code sample for this step.

                          
                          
                          

                          Congratulations!

                          Where to next?

                          Now that you've verified your first user, we’d love your feedback on your experience and whether you plan to use Verify TOTP in production. We’d be happy to discuss your plans and offer suggestions. In addition, explore adding Verify's other verification channels beyond TOTP.

                          We can't wait to see what you build!

                          Rate this page:

                          Need some help?

                          We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

                                
                                
                                

                                Thank you for your feedback!

                                We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                                Sending your feedback...
                                🎉 Thank you for your feedback!
                                Something went wrong. Please try again.

                                Thanks for your feedback!

                                Refer us and get $10 in 3 simple steps!

                                Step 1

                                Get link

                                Get a free personal referral link here

                                Step 2

                                Give $10

                                Your user signs up and upgrade using link

                                Step 3

                                Get $10

                                1,250 free SMSes
                                OR 1,000 free voice mins
                                OR 12,000 chats
                                OR more