Account Verification with Authy, Java and Servlets
For new development, we encourage you to use the Verify API instead of the Authy API. The Verify API is an evolution of the Authy API with continued support for SMS, voice, and email one-time passcodes, an improved developer experience and new features including:
- Twilio helper libraries in JavaScript, Java, C#, Python, Ruby, and PHP
- Access via the Twilio CLI
- Improved Visibility and Insights
- Push authentication SDK embeddable in your own application
You are currently viewing the Authy API. The Authy API will continue to be maintained, but any new features and development will be on the Verify API. Check out the FAQ for more information and Verify API Reference to get started.
アプリケーションにユーザーアカウント検証を実装する準備はできましたか? どのような仕組みになっているのか概要を以下に示します。
- ユーザーは、各自のデータ(電話番号など)をサインアップフォームに入力することで登録処理を開始します。
- 認証システムは、ワンタイムパスワードをユーザーの携帯電話に送信して、ユーザーがその電話番号を所持していることを確認します。
- ユーザーは登録を完了する前にワンタイムパスワードをフォームに入力します。
- ユーザーは成功ページを開いて、アカウントが作成された旨を知らせるSMSを受信します。
ビルディングブロック
これを行うには、以下の Twilio 駆動型 API を操作します。
Authy REST API
- Authy ドキュメント:ヘルパーライブラリーに関するクイックスタート、ドキュメント、その他すべてを検索します。
Twilio REST API
- メッセージリソース:ユーザーがアカウントを作成した後に、Twilio を直接使用して確認メッセージをユーザーに送信します。
はじめましょう!
ユーザーモデル
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.
Next we will see how to handle the 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.
これでユーザーはログインしましたが、検証はまだです。次のステップでは、Authy を使ってユーザーを検証する方法について学びます。
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.
ここで、Authy プロダクションキーが必要となります(こちらで Authy にサインアップしてください)。Authy アプリケーションを作成する場合、プロダクションキーはダッシュボード上にあります。
Now let's check out the Servlet handling a new user registration and see how it sends a token upon 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.
コードの送信時、ユーザーが受信したトークンを入力できる別のページに遷移させ、 こうして検証プロセスが完了します。
コードの検証
Servletのメソッドは送信フォームを処理します。 これは、以下の目的で必要になります:
- 現在のユーザーを取得します。
- ユーザーによって入力されたコードを検証します。
- 入力されたコードが有効な場合は、ユーザーモデルの boolean フラグを反転して、アカウントが検証されたことを示します。
コードの検証
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.
トークン検証については、これで終わりです。ただし、メッセージがエンドユーザーの電話機に届かない場合に確認コードを再送信する方法がなければ、検証フォームはあまり役立ちません。
コードの再送信
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.
このメソッドは、リクエストに関連付けられている user
をロードし、先に使用したものと同じ Authy API メソッドを使用してコードを再送信します。
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.
確認メッセージの送信
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.
Congratulations! You've successfully verified new user accounts with Authy. Where can we take it from here?
次はどこでしょうか?
Twilio を使う Java 開発者であれば、他のチュートリアルも調べてみてください。
ウェブページのボタンを押して、電話を介して訪問者をライブサポートまたはセールス担当者に接続します。
音声通話または SMS テキストメッセージを介して調査を実施し、ユーザーから構造化データを迅速に収集します。
これは役に立ちましたか?
このチュートリアルをご覧いただき、ありがとうございます。 ご意見やご感想などございましたら、ぜひお聞かせください。 Twitterからご連絡いただき、どんなものを構築されているかお聞かせください!
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。