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.
Adding Two-factor Authentication to your application is the easiest way to increase security and trust in your product without unnecessarily burdening your users. This quickstart guides you through building a Java, Spring and AngularJS application that restricts access to a URL. Four Authy API channels are demoed: SMS, Voice, Soft Tokens and Push Notifications.
Ready to protect a tiny app from big hacking efforts?
Create a new Twilio account (you can sign up for a free Twilio trial), or sign into an existing Twilio account.
Once logged in, visit the Authy Console. Click on the red 'Create New Application' (or big red plus ('+') if you already created one) to create a new Authy application then name it something memorable.
You'll automatically be transported to the Settings page next. Click the eyeball icon to reveal your Production API Key.
Copy your Production API Key to a safe place, you will use it during application setup.
This Two-factor Authentication demos two channels which require an installed Authy Client to test: Soft Tokens and Push Notifications. While SMS and Voice channels will work without the client, to try out all four authentication channels download and install Authy Client for Desktop or Mobile:
Clone our Java repository locally, then enter the directory. Install all of the necessary node modules:
gradle build
Next, open the file .env.example
. There, edit the ACCOUNT_SECURITY_API_KEY
, pasting in the API Key from the above step (in the console), and save the file as .env
. Either source the .env file or otherwise set the ACCOUNT_SECURITY_API_KEY
in your environment.
Enter the API Key from the Account Security console and optionally change the port.
1# You can get/create one here :2# https://www.twilio.com/console/authy/applications3export ACCOUNT_SECURITY_API_KEY=ENTER_SECRET_HERE4
Once you have added your API Key, you are ready to run! Launch the app with:
gradle appRun
You should get a message your new app is running!
With your phone (optionally with the Authy client installed) nearby, open a new browser tab and navigate to http://localhost:8080/register/index.html
Enter your information and invent a password, then hit 'Register'. Your information is passed to Twilio (you will be able to see your user immediately in the console), and the application is returned a user_id
.
Now visit http://localhost:8080/login/index.html and login. You'll be presented with a happy screen:
If your phone has the Authy Client installed, you can immediately enter a Soft Token from the client to Verify. Additionally, you can try a Push Notification by pushing the labeled button.
If you do not have the Authy Client installed, the SMS and Voice channels will also work in providing a token. To try different channels, you can logout to start the process again.
1package com.twilio.accountsecurity.services;23import com.authy.AuthyApiClient;4import com.authy.OneTouchException;5import com.authy.api.*;6import com.twilio.accountsecurity.controllers.requests.VerifyTokenRequest;7import com.twilio.accountsecurity.repositories.UserRepository;8import com.twilio.accountsecurity.exceptions.TokenVerificationException;9import com.twilio.accountsecurity.models.UserModel;10import org.slf4j.Logger;11import org.slf4j.LoggerFactory;12import org.springframework.beans.factory.annotation.Autowired;13import org.springframework.stereotype.Service;1415@Service16public class TokenService {1718private static final Logger LOGGER = LoggerFactory.getLogger(TokenService.class);1920private AuthyApiClient authyClient;21private UserRepository userRepository;2223@Autowired24public TokenService(AuthyApiClient authyClient, UserRepository userRepository) {25this.authyClient = authyClient;26this.userRepository = userRepository;27}282930public void sendSmsToken(String username) {31Hash hash = authyClient32.getUsers()33.requestSms(getUserAuthyId(username));3435if(!hash.isOk()) {36logAndThrow("Problem sending token over SMS. " + hash.getMessage());37}38}3940public void sendVoiceToken(String username) {41UserModel user = userRepository.findFirstByUsername(username);4243Hash hash = authyClient.getUsers().requestCall(user.getAuthyId());44if(!hash.isOk()) {45logAndThrow("Problem sending the token on a call. " + hash.getMessage());46}47}4849public String sendOneTouchToken(String username) {50UserModel user = userRepository.findFirstByUsername(username);5152try {53ApprovalRequestParams params = new ApprovalRequestParams54.Builder(user.getAuthyId(), "Login requested for Account Security account.")55.setSecondsToExpire(120L)56.addDetail("Authy ID", user.getAuthyId().toString())57.addDetail("Username", user.getUsername())58.addDetail("Location", "San Francisco, CA")59.addDetail("Reason", "Demo by Account Security")60.build();61OneTouchResponse response = authyClient62.getOneTouch()63.sendApprovalRequest(params);6465if(!response.isSuccess()) {66logAndThrow("Problem sending the token with OneTouch");67}68return response.getApprovalRequest().getUUID();69} catch (OneTouchException e) {70logAndThrow("Problem sending the token with OneTouch: " + e.getMessage());71}72return null;73}7475public void verify(String username, VerifyTokenRequest requestBody) {76Token token = authyClient77.getTokens()78.verify(getUserAuthyId(username), requestBody.getToken());7980if(!token.isOk()) {81logAndThrow("Token verification failed. " + token.getError().toString());82}83}8485public boolean retrieveOneTouchStatus(String uuid) {86try {87return authyClient88.getOneTouch()89.getApprovalRequestStatus(uuid)90.getApprovalRequest()91.getStatus()92.equals("approved");93} catch (OneTouchException e) {94logAndThrow(e.getMessage());95}96return false;97}9899private void logAndThrow(String message) {100LOGGER.warn(message);101throw new TokenVerificationException(message);102}103104private Integer getUserAuthyId(String username) {105UserModel user = userRepository.findFirstByUsername(username);106return user.getAuthyId();107}108}109
And there you go, Authy Two-factor Authentication is on and your Java app is protected!
Now that you are keeping the hackers out of this demo app using Two-factor Authentication with Twilio Authy, you can find all of the detailed descriptions for options and API calls in our Authy API Reference. If you're also building a registration flow, also check out our Verify product and the phone verification quickstart which uses this codebase.
For additional guides and tutorials on account security and other products, in Node.js and in our other languages, take a look at the Docs.