Two factor authentication (2FA) docs
| Python | Node | Ruby |2FA for Python
⏱ 20 minutes build time || Difficulty Level: Intermediate || Github RepoConfiguration file
Create aconfig.cfg file in your project directory. Flask will load this at startup. First, use this guide to provision an SMS number and messaging profile, and create an API key. Then add those to the config file.
Token storage
We’ll use a class to store tokens in memory for the purposes of this example. In a production environment, a traditional database would be appropriate. Create a class calledTokenStorage with three methods. This class will store uppercase tokens as keys, with details about those tokens as values, and expose check and delete methods.
Server initialization
Setup a simple Flask app, load the config file, and configure the telnyx library. We’ll also serve anindex.html page, the full source of this is available on GitHub, but it includes a form that collects a phone number for validation.
Token generation
We’ll start with a simple method,get_random_token_hex, that generates a random string of hex characters to be used as OTP tokens.
token_hex method accepts a number of bytes, so we need to divide by two and and round up in order to ensure we get enough characters (two characters per byte), and then finally trim by the actual desired length. This allows us to support odd numbered token lengths.
Next, handle the form on the /request route. First this method normalizes the phone number.
Token verification
Theverify.html file includes a form that collects the token and sends it back to the server. If the token is valid, we’ll clear it from the datastore and serve the success page.
Finishing up
At the end of the file, run the server.python otp_demo.py from within the virtualenv.
2FA for Node
⏱ 20 minutes build time || Difficulty Level: Intermediate || Github RepoConfiguration
Create aconfig.json file in your project directory. Express will load this at startup. First, use this guide to provision an SMS number and messaging profile, and create an API key. Then add those to the config file.
Token storage
We’ll use a class to store tokens in memory for the purposes of this example. In a production environment, a traditional database would be appropriate. Create a class calledTokenStorage with three methods. This class will store uppercase tokens as keys, with details about those tokens as values, and expose check and delete methods.
Server initialization
Setup a simple Express app that watches the templates directory withNunjucks, load the config file, and configure the telnyx library.
Collect user input
Create a simple HTML form, index.html, which collects the phone number for validation. The full HTML source can be found at our Github repo, and we’ll serve the rootToken generation
We’ll start with a simple method,get_random_token_hex, that generates a random string of hex characters to be used as OTP tokens.
randomBytes method accepts a number of bytes, so we need to divide by two and and round up in order to ensure we get enough characters (two characters per byte),and then finally trim by the actual desired length. This allows us to support odd numbered token lengths.
Next, handle the form on the /request route. First this method normalizes the phone number.
Token verification
Theverify.html file includes a form that collects the token and sends it back to the server. If the token is valid, we’ll clear it from the datastore and serve the success page.
Finishing up
At the end of the file, run the server.node index.js.
2FA for Ruby
⏱ 20 minutes build time || Difficulty Level: Intermediate || Github RepoConfiguration
Create aconfig.cfg file in your project directory. Flask will load this at startup. First, use this guide to provision an SMS number and messaging profile, and create an API key. Then add those to the config file.
Token storage
We’ll use a class to store tokens in memory for the purposes of this example. In a production environment, a traditional database would be appropriate. Create a class calledTokenStorage with three methods. This class will store uppercase tokens as keys, with details about those tokens as values, and expose check and delete methods.
Server initialization
Setup a simple Flask app, load the config file, and configure the telnyx library. We’ll also serve anindex.html page, the full source of this is available on GitHub, but it includes a form that collects a phone number for validation.
Token generation
We’ll start with a simple method,get_random_token_hex, that generates a random string of hex characters to be used as OTP tokens. We’ll use the SecureRandom gem for this, as it comes pre-installed in Ruby.
SecureRandom.hex method accepts a number of bytes, so we need to divide by two and and round up in order to ensure we get enough characters (two characters per byte), and then finally trim by the actual desired length. This allows us to support odd numbered token lengths.
Next, handle the form on the /request route. First this method normalizes the phone number.
Token verification
Theverify.html file includes a form that collects the token and sends it back to the server. If the token is valid, we’ll clear it from the datastore and serve the success page.
Finishing up
To start the application, runruby 2fa.rb.