Menu

Rate this page:

Thanks for rating this page!

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

Account Verification with Authy, Java and Servlets

Download the Code

Ready to implement user account verification in your application? Here's how it works at a high level:

  1. The users begin the registration process by entering their data, including a phone number, into a signup form.
  2. The authentication system sends a one-time password to the user's mobile phone to verify the possession of that phone number.
  3. The user enters the one-time password into a form before completing registration.
  4. The user opens a success page and receives an SMS indicating that their account has been created.

Building Blocks

To get this done, you'll be working with the following Twilio-powered APIs:

Authy REST API

  • Authy Docs: Find quick starts, documentation, and all about the helper libraries.

Twilio REST API

  • Messages Resource: We will use Twilio directly to send our user a confirmation message after they create an account.
Loading Code Sample...
      
      
          
          
          
          
        
      src/main/webapp/WEB-INF/web.xml

      Deployment descriptor

      src/main/webapp/WEB-INF/web.xml

      Let's get started!

      Click here to move on to the next step!

      The User Model

      The User Model for this use-case is pretty straightforward and JPA offers us some tools to make it even simpler. If you have already read through the 2FA tutorial this one probably looks very similar. We need to make sure that our User model contains a phone number, country code so that the user can be verified with Authy.

      Loading Code Sample...
          
          
              
              
              
              
            
          src/main/java/com/twilio/verification/model/User.java

          User model definition and JPA mappings

          src/main/java/com/twilio/verification/model/User.java

          Next we will see how to handle the new user form.

          Create a new user

          New User Form

          When we create a new user, we ask for a name, e-mail address, and a password. In order to validate a new account we also ask the user for a mobile number with a country code. We will use Authy to send a one-time password via SMS to this phone number.

          It is now the servlet's responsibility to verify that the user provides the necessary information to create a new user. If the user is created successfully, they will be logged into the system automatically.

          Loading Code Sample...
              
              
                  
                  
                  
                  
                
              src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

              Handle registration of new accounts

              src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

              Now the user is logged in but not verified. In the next steps we'll learn how to verify the user using Authy.

              Configure your application to work with Authy

              Configuring Authy

              In .environment we list configuration parameters for the application. These are pulled from system environment variables, which is a helpful way to access sensitive values (like API keys). This prevents us from accidentally checking them into source control. We use the System.getenv method to load the key and inject the AuthyApiClient into the RegistrationServlet class.

              Now we need our Authy production key (sign up for Authy here). When you create an Authy application the production key is found on the dashboard.

              Authy dashboard

              Loading Code Sample...
                  
                  
                      
                      
                      
                      
                    
                  src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

                  Register your AuthyAPIClient with your Authy API key

                  src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

                  Now let's check out the Servlet handling a new user registration and see how it sends a token upon account creation.

                  Send a token to our new user

                  Sending a Token on Account Creation

                  Once the user has an authyId we can actually send a verification code to that user's mobile phone using the Java Client for Authy.

                  When our user is created successfully via the form we implemented, we send a token to the user's mobile phone to verify their account in our servlet.

                  Loading Code Sample...
                      
                      
                          
                          
                          
                          
                        
                      src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

                      Handle registration of new accounts

                      src/main/java/com/twilio/verification/servlet/RegistrationServlet.java

                      When the code is sent we redirect to another page where the user can enter the token they received, therefore completing the verification process.

                      Let's check out the code for token verification!

                      Verifying the Code

                      This servlet method handles the submission form. It needs to:

                      • Get the current user.
                      • Verify the code that was entered by the user.
                      • If the code entered was valid, flip a boolean flag on the user model to indicate the account was verified.

                      Verifying the Code

                      The Authy client provides us with a verify() method that allows us to pass a user id and a token. In this case we just need to check that the API request was successful and, if so, set the User's verified field to true.

                      Loading Code Sample...
                          
                          
                              
                              
                              
                              
                            
                          src/main/java/com/twilio/verification/servlet/VerifyCodeServlet.java

                          Verify an Authy code

                          src/main/java/com/twilio/verification/servlet/VerifyCodeServlet.java

                          That's all for token verification! However, our verification form wouldn't be very usable if there wasn't a way to resend a verification code if the message didn't arrive at the end user's handset.

                          Re-send a missing verification code

                          Re-sending the Code

                          Since the form for re-sending the code is very simple, we're going to skip that for this tutorial. Let's just look at the servlet.

                          This method loads the user associated with the request and then uses the same Authy API method we used earlier to resend the code.

                          Loading Code Sample...
                              
                              
                                  
                                  
                                  
                                  
                                
                              src/main/java/com/twilio/verification/servlet/ResendTokenServlet.java

                              Re-send an Authy verification code

                              src/main/java/com/twilio/verification/servlet/ResendTokenServlet.java

                              To wrap things up, let's implement the last step where we confirm that the user's account has been verified with a text message.

                              Send a confirmation SMS to a verified user

                              Sending the Confirmation Message

                              In this example, we create a single instance of the Twilio REST API helper, called client.

                              Then we need to get the account, the messageFactory, and finally use its sendMessage method in order to send an SMS to the user's phone.

                              Loading Code Sample...
                                  
                                  
                                      
                                      
                                      
                                      
                                    
                                  src/main/java/com/twilio/verification/lib/Sender.java

                                  Send a confirmation with a TwilioRestClient wrapper

                                  src/main/java/com/twilio/verification/lib/Sender.java

                                  Congratulations! You've successfully verified new user accounts with Authy. Where can we take it from here?

                                  What's next?

                                  Where to Next?

                                  If you're a Java developer working with Twilio, you might want to check out these other tutorials:

                                  Click-To-Call

                                  Put a button on your web page that connects visitors to live support or salespeople via telephone.

                                  Automated Survey

                                  Instantly collect structured data from your users with a survey conducted over a voice call or SMS text messages.

                                  Did this help?

                                  Thanks for checking out this tutorial! If you have any feedback to share with us, we'd love to hear it. Reach out to us on Twitter and let us know what you build!

                                  Kat King

                                  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 browsing the Twilio tag on Stack Overflow.

                                  Loading Code Sample...