メニュー

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?

Authy、Python、Flaskを使用したアカウント検証

Ready to add Authy user account verification to your Flask application? Don't worry, this will be the easiest thing you do all day.

下記が、ひととおりの仕組みの概要になります:

  1. A user begins the registration process by entering their information (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 phone number.
  3. The user enters that one-time password into a form to complete registration.
  4. The user sees a success page and receives an SMS alerting them that their account has been created!

ビルディングブロック

これを行うには、以下の Twilio 駆動型 API を操作します。

Authy REST API

  • Authy Docs: Find quickstarts, documentation, and info on the helper libraries.

Twilio REST API

  • Messages Resource: We will use Twilio directly to send our users a confirmation message after they creates their accounts.
        
        
        
        
        account_verification_flask/__init__.py

        Initialize Flask application

        account_verification_flask/__init__.py

        AuthyとTwilioのシンプルさとパワーで、これらすべてのことは30分以内に完了します。  さぁ、はじめましょう!

        Click me to move on to the next step!

        アプリケーションの設定

        For this application we'll be using the The Twilio Python Helper Library and the Python Client for Authy API. We require some configuration from your side before we can begin.

        Edit the DevelopmentConfig class constant values located in the account_verification_flask/config.py file:

        AUTHY_KEY = 'your_authy_key'
        
        TWILIO_ACCOUNT_SID = 'your_twilio_account_sid'
        TWILIO_AUTH_TOKEN = 'your_twilio_auth_token'
        TWILIO_NUMBER = 'your_twilio_phone_number'
        
        SQLALCHEMY_DATABASE_URI = 'sqlite://'
        

        Note that you have to replace the placeholders your_twilio_account_sid, your_twilio_auth_token, your_twilio_phone_number and your_authy_key with your information. You can find all of those parameters (and more) in your Twilio Account Console and your Authy dashboard.

              
              
              
              
              account_verification_flask/config.py

              Configure your application with Twilio and Authy

              account_verification_flask/config.py

              ひながたのセットアップが終わったので、ここで User モデルについて見ていきましょう。

              Visit our user model

              ユーザーモデル

              The User Model for this tutorial is pretty straight-forward. Note the new variable authy_user_id, which is implemented for storing the user's Authy identification token.

              We'll be using the Flask-Login library for our user session management. To integrate this library into our code we need to implement a few properties and methods, then we're good to go.

                    
                    
                    
                    
                    account_verification_flask/models/models.py

                    User model with DB configuration and model methods for Flask-Login

                    account_verification_flask/models/models.py

                    Pretty simple User model, right? Next we're going to visit the registration form on the client side.

                    Take a look at the user registration form

                    登録フォーム

                    In order to validate the user's account and register the user, we need a mobile number with a country code. We can then use Authy to send a verification code via SMS.

                    この例では、WTFormsライブラリーを使用して、フォームを検証およびレンダリングしています。 これにより、フォームをPythonのクラスとして定義できます。

                          
                          
                          
                          
                          account_verification_flask/templates/register.html

                          User registration form

                          account_verification_flask/templates/register.html

                          That's it for the client side. Now let's look at what happens when the user submits the form.

                          Let's look at the form code on the server side

                          登録のサーバーサイドの実装

                          Next our controller stores the new user, registers them with Authy's API, and requests a new verification code.

                                
                                
                                
                                
                                account_verification_flask/views.py

                                The registration view route

                                account_verification_flask/views.py

                                Next we'll set up our application to complete our user verification.

                                検証コードを受信するとどうなるでしょうか?

                                Verifying a User in Python

                                On the server we first check that the email belongs to a user that we haven't yet verified.

                                そして、プロセスには2つの重要なステップがあります。

                                1. Communicate with Authy's API to check if the given code is correct.

                                2. Send a confirmation SMS to the user using Twilio's API.

                                After that (assuming a success!) we redirect the user to a success page.

                                      
                                      
                                      
                                      
                                      account_verification_flask/views.py

                                      Verify a user's code and send a success SMS

                                      account_verification_flask/views.py

                                      What happens if the message was never sent, didn't arrive, or can't be found? Let's look at how to handle those scenarios next.

                                      例外処理についてはどうでしょうか。

                                      Re-sending a Verification Code

                                      The form for re-sending the code is a single line, so let's skip that detail for this tutorial. Instead, let's just take a look at the controller function for resending verifications.

                                      This controller loads the User associated with the request and then uses the same Authy API method we used earlier to resend the code. Pretty straightforward, right?

                                            
                                            
                                            
                                            
                                            account_verification_flask/views.py

                                            Re-send a verification code

                                            account_verification_flask/views.py

                                            Let's take a step back and see how we can use Authy to resend a verification code to an unverified user.

                                            アカウントの作成時にトークンを送信する

                                            アカウント作成時のトークンの送信

                                            In order to end up with a cleaner and decoupled design we'll encapsulate all of Authy's related features in an AuthyService. This class will hold a shared class instance of the AuthyApiClient class.

                                            ユーザーがauthyIdを手に入れたなら、検証コードをそのユーザーの携帯電話に送信することができるようになります。

                                                  
                                                  
                                                  
                                                  
                                                  account_verification_flask/services/authy_services.py

                                                  Send Authy token via SMS

                                                  account_verification_flask/services/authy_services.py

                                                  When our user is created successfully via the form we have implemented, we send a token to the user's mobile phone asking them to verify their account in our controller. When the code is sent, we redirect our users to another page where they can enter the received token, completing the verification process.

                                                  Next: Verifying the user's code with Authy

                                                  コードの検証

                                                  Authy provides us with a tokens.verify method that allows us to pass a user id and token. In this case we just need to check that the API request was successful and, if so, set a verified flag on the user.

                                                        
                                                        
                                                        
                                                        
                                                        account_verification_flask/services/authy_services.py

                                                        Verify a user's token with AuthyServices

                                                        account_verification_flask/services/authy_services.py

                                                        That's it for token verification! Let's provide a nice user onboarding experience, and send a confirmation message to our new user.

                                                        快い確認メッセージで締めくくる

                                                        確認メッセージの送信

                                                        Authyクライアントで行ったように、Twilio REST APIヘルパーの単一のインスタンスを作成します。 この例では、twilio_clientが呼び出されます。

                                                        After that, it's straightforward - send an SMS using the Twilio Python helper library to the same number we used in messages.create().

                                                              
                                                              
                                                              
                                                              
                                                              account_verification_flask/services/twilio_services.py

                                                              Send a registration success SMS from the TwilioServices class

                                                              account_verification_flask/services/twilio_services.py

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

                                                              Now you're an expert on Authy. Where to next?

                                                              次はどこでしょうか?

                                                              In one simple tutorial, we've implemented account verification with Authy and Twilio, allowing your users to confirm accounts with their phone number! Now it's on you - let us know what you build on Twitter, and check out these other tutorials:

                                                              アポイントメント リマインダー

                                                              Twilio を使用して、近く予定されているアポイントメントに先立ち、お客様へ接触するプロセスを自動化します。

                                                              Authy を使った 二要素認証

                                                              Twilioと、そしてTwilioのパワーを駆使したAuthyを使用して、Webアプリケーションに二要素認証を実装しました。

                                                              Kevin Whinnery Jose Oliveros David Prothero Kat King Hector Ortega Paul Kamp Andrew Baker
                                                              Rate this page:

                                                              ヘルプが必要ですか?

                                                              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.