Two-factor Authentication (2024)

View Repo

SMS Two-factor Authentication is a microservice that allows you to add an extra layer of security to your application. With this service you can generate an authorization code, and then send that code by SMS to a specified recipient. The recipient must then respond with that code to to allow access to that application. For example, the recipient might need to respond with the code on a web page to access that site. You can also customize the code length, allotted time to respond, and the number of permitted retries before disallowing access. The service uses a SQLite backend and exposes an API endpoint which generates and validates verification requests using two methods: POST and GET.

  • POST generates the authorization code, which includes a customizable expiration date, code length, number of retries, and custom message before dispatching an SMS to the specified recipient.
  • GET validates the request data; after running verification on the returned authorization code, either returns a response success or an error message.You will need your Access Key, Secret Key, and your SMS-enabled Flowroute number. If you do not know your Flowroute information:
Requirements

1. Run git clone and create a credentials.py file

  1. Open a terminal window.
  2. If needed, create a directory where you want to deploy the SMS Verification service.
  3. Change to the directory, and run the following:
    git clone https://github.com/flowroute/sms-verification.git
    The git clone command clones the sms-verification repository as a sub directory within the parent folder.
  4. Create a credentials.py file that includes your Flowroute credentials.
    This is done to help protect against committing private information to a remote repository.
  5. Add the required environment variables to your host, or modify **settings.py** to include them.
    Using a text editor — for example, Sublime Text — add the following lines to a new file, replacing the Access Key, Secret Key, and Flowroute phone number with the information from your Flowroute account:
  6. FLOWROUTE_ACCESS_KEY = "Your Access Key"FLOWROUTE_SECRET_KEY = "Your Secret Key"FLOWROUTE_NUMBER = "Your 11-digit, 1XXXXXXXXXX-formatted Flowroute phone number."
    Save the file as **credentials.py** in the sms_auth_service directory.
  7. Deploy the service.

2. Deploy SMS Identify Authorization

Deploying the service can be done by either building and running a Docker container as specified by the provided Dockerfile, or by running the application locally with Flask's built-in web server. You can first run the application in test mode before running in production mode.

DEBUG mode

During development DEBUG_MODE should be set to True to use the auto-generated test database. Testing can be performed on this database, which drops data in the tables each time the test module is run. Once the development cycle is over, set DEBUG_MODE to False in order to use the production database. Tests cannot be run when the production database is active.

To run the application using Docker:
  1. Run the following at the project's top level to build the service:
  2. $ docker build -t sms_auth_api:0.0.1 .

    -t tags the image, allowing you to reference it by name instead of by image ID.

  3. Next, run the following:
  4. $ docker run -p 8000:8000 sms_auth_api:0.0.1

    -p binds the container port to the Docker host port. When using a virtualization layer, such as Docker-machine, the API should now be exposed on that host — for example, http://192.168.99.100:8000.

    By default, the run command spawns four Gunicorn workers listening on port 8000. To modify the run command, edit the settings in the Docker entry file located in the project root.

To run the application locally:
  1. From your sms_verification directory, run:
  2. pip install -r requirements.txt
  3. Run the following to install the service dependencies at the root level of the project:
  4. pip install .
  5. Finally, run:
  6. python -m sms_auth_service.api

    The service is set up at the root level.

Flask

See the Flask documentation for more information about the web framework.

3. Configure SMS Identity Authorization

With the service now deployed, configure authorization settings by either customizing and running settings.py or by running client.py.

settings.py

settings.py allows you to customize the authorization code, including the code length, expiration time, number of retries, company name, and message.

To configure the authorization settings:
  1. In the sms_auth_service directory, open settings.py.
  2. Modify any of the following values as needed:
  3. CODE_LENGTH = 4CODE_EXPIRATION = 3600 # 1 Hour expirationRETRIES_ALLOWED = 3ORG_NAME="Your Organization Name"AUTH_MESSAGE=("{{}}\n" # Placeholder for authorization code."Welcome to {}! Use this one-time code to ""complete your signup.").format(ORG_NAME)

    The following table describes the parameters that can be changed in settings.py:

    Variable Data type Constraint
    CODE_LENGTH integer Sets the length of the generated authorization code. There is neither a minimum nor maximum number of allowed digits. The default length is 4 (four) digits.
    CODE_EXPIRATION integer The length of time, in seconds and including retries, before the authorization code expires. There is no limit on the time. The default value is 3600 seconds (one hour).
    RETRIES_ALLOWED integer The number of retries allowed before the code is invalid. There is no limit on the number of retries you can set. The default retry is 3 times
    ORG_NAME string The name you want to display in the authorization message within the enclosing quotes (""). Alphanumeric characters are supported. There are no disallowed characters, and there is no limit on the number of characters. The default name is Your Organization Name.
    AUTH_MESSAGE string The message sent with the code. There is no limit on the number of alphanumeric characters that can be used, and there are no disallowed characters. If the message exceeds 160 characters, however, it will be broken up into multiple messages. See for more information on message length.
  4. Save the file.
client.py

The SMSAuthClient can be imported from client.py and instantiated with the SMS_AUTH_ENDPOINT as its only argument. The SMSAuthClient has two methods, create_auth and authenticate_code, which proxy to the service resource endpoints.

client.py reads the response and returns a success or error message as needed.

The module is located within the sms_auth_service Python package.

4. Test it!

In a test environment, invoke the docker run command with the test argument to run tests and see results. To change the docker run command options, modify the test, coverage, or serve options in the entry script located in the top level sms-verification directory.

Docker Entry Points

To learn more about Docker entry points, see Dockerfile Best Practices.

Run the following:

$ docker run -p 8000:8000 sms_auth_api:0.0.1 test

A py.test command is invoked from within the container. When running coverage, a cov-report directory is created that contains an index.html file detailing test coverage results.

5. Send and validate an authorization code

Once the application is up-and-running, the authorization resources can now be invoked with their respective request types.

Send the code

Use either a curl command or use the client class stored in client.py to send a code.

  • use a curl POST command:
curl -v -X POST -d '{"auth_id": "my_identifier", "recipient": "my_phone_number"}' -H "Content-Type: application/json" localhost:8000

Important

When using the POST method with JSON you must also include the complete Content-Type:application/json" localhost:8000 header.

The statement takes the following arguments:

Key: Argument Required Constraint
auth_id: my_dentifier True my_identifier is any user-defined string, limited to 120 characters. For example, this could be a UUID.
recipient: my_phone number True my_phone_number is the phone number identifying the recipient using an 11-digit, number formatted as 1XXXXXXXXXX. Validation is performed to ensure the phone number meets the formatting requirement, but no validation is performed to determine whether or not the phone number itself is valid.
  • use the client class stored in client.py:
  • from client import SMSAuthClient my_client = SMSAuthClient(endpoint="localhost:8000")my_client.create_auth("my_identifier", "my_phone_number")

    A code is auto-generated based on the modifications made to settings.py and sent to the intended recipient. The recipient then receives the message and code at the number passed in the POST method.

    5. Validate the authorization code (GET)

    Run the following:

    curl -X GET "http://localhost:8000?auth_id=my_identifier&code=1234"

    In this example,

    • my_identifier is the auth_id from the POST request.
    • 1234 is the value passed through from user input.

    Important

    URL encoding is required for the GET request.

    The following then occurs:

    1. The auth_id is validated. If the authorization ID is not recognized, a 400 status code is returned.
    2. The code is checked against the expiration time. If the code has expired, the entry is deleted, and no attempts for that auth_id will be recognized. A 400 status code is returned.
    3. If the code has not expired, but the attempt does not match the stored code, retries based on the RETRIES_ALLOWED number set in settings.py begin.
    • If the code matches the stored code, a 200 success message is returned, and the entry is removed from the database.
    • If the number of retries is reached without success, no more retries are allowed, and the entry is removed from the database.

    Entries removed from the database

    Because they are no longer needed, validated and failed authorization entries are removed in order to keep database size down.

    Success and Error response codes

    Success response

    A valid authorization code returns a 200 status code and success message.

    Error response

    The following describe the possible error status codes:

    • a 400 status code for invalid attempts, if the code has expired, or if the auth_id is not recognized. The number of retry attempts remaining is stored in the response data, along with the reason for the failure and the exception type.
    • a 500 status code for an internal error, or if a phone number is not reachable on the network.
    Two-factor Authentication (2024)

    FAQs

    What is two-factor authentication and how does it work? ›

    Two-factor authentication (2FA), sometimes referred to as two-step verification or dual-factor authentication, is a security process in which users provide two different authentication factors to verify themselves. 2FA is implemented to better protect both a user's credentials and the resources the user can access.

    How do I find my two-factor authentication? ›

    Get a code from Settings on your trusted device
    1. Go to Settings > [your name].
    2. Tap Sign-In & Security > Two Factor Authentication.
    3. Tap Get Verification Code.
    Apr 19, 2024

    How to set 2 factor authentication? ›

    Turn on 2-Step Verification
    1. Open your Google Account.
    2. In the navigation panel, select Security.
    3. Under “How you sign in to Google,” select 2-Step Verification. Get started.
    4. Follow the on-screen steps.

    What is an example of two-factor authentication? ›

    Two-factor authentication can work in multiple ways. One of the most common examples of 2FA requires a username/password verification and an SMS text verification. In this example, when the user creates an account for a service they must provide a unique username, a password, and their mobile phone number.

    Do I really need two-factor authentication? ›

    Used on top of the regular username/password verification, 2FA bolsters security by making it more difficult for intruders to gain unauthorized access, even if a perpetrator gets past the first authentication step (e.g., brute forces a username and password).

    Can hackers bypass two step verification? ›

    Most 2FA methods involve sending temporary codes via SMS or emails, but these can be easily intercepted by hackers through account takeover, SIM swapping, and/or MitM attacks. To avoid these vulnerabilities, businesses should use authenticator apps like Google Authenticator or Microsoft Authenticator.

    What is the secret key for two-factor authentication? ›

    The secret key is a unique 16 character alphanumeric code that is required during the set up of the PIN generating tools. The secret key is issued through email by logging on to the CommCell environment.

    Can you turn off two-factor authentication? ›

    If you updated to two-factor authentication inadvertently, you can turn it off within two weeks of enrollment. If you do, your account is less secure and you can't use features that require a higher level of security.

    How safe is two-factor authentication? ›

    When Faced With the Question, Is 2-Step Verification Safe? The answer is a sure yes. However, it is not foolproof. There should be additional measures to further prevent hackers from infiltrating the user's accounts.

    Which two-factor authentication is the best? ›

    Some of the best authenticator apps are Authy, Google Authenticator, and Microsoft Authenticator. Download the authenticator app of your choice, scan the QR codes when adding new accounts (think Amazon, email, banking), and use the generated codes whenever you need to log in anywhere.

    What is two-factor authentication on an iPhone? ›

    Using two-factor authentication helps prevent others from accessing your Apple ID account, even if they know your Apple ID password. When it's on, you need both your Apple ID password and a six-digit verification code to sign into your Apple ID account.

    What triggers two-factor authentication? ›

    To use two-factor authentication, you need at least one trusted phone number on file where you can receive verification codes. If you have a phone number that isn't associated with your trusted device, consider verifying it as an additional trusted phone number.

    What is the difference between verification and two-factor authentication? ›

    The key difference between 2-step verification vs. 2-factor authentication is that 2FA requires two independent forms of authentication from different categories. In contrast, 2SV only requires two pieces of information with no regard for whether they are from the same type of authentication category.

    Top Articles
    Apex Legends Vs. COD Warzone: A Gamer's Dilemma
    Can You Install Fence Posts Without Cement?
    Tiny Tina Deadshot Build
    Tmf Saul's Investing Discussions
    Www.fresno.courts.ca.gov
    David Packouz Girlfriend
    Back to basics: Understanding the carburetor and fixing it yourself - Hagerty Media
    Corporate Homepage | Publix Super Markets
    How Much Is Tj Maxx Starting Pay
    Transfer Credits Uncc
    Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
    National Weather Service Denver Co Forecast
    Navy Female Prt Standards 30 34
    Pekin Soccer Tournament
    Christina Steele And Nathaniel Hadley Novel
    Katie Sigmond Hot Pics
    St Clair County Mi Mugshots
    Jobs Hiring Near Me Part Time For 15 Year Olds
    MyCase Pricing | Start Your 10-Day Free Trial Today
    Jayah And Kimora Phone Number
    Raw Manga 1000
    Violent Night Showtimes Near Amc Dine-In Menlo Park 12
    What Equals 16
    Dei Ebill
    Pain Out Maxx Kratom
    Culver's.comsummerofsmiles
    Jailfunds Send Message
    Delete Verizon Cloud
    James Ingram | Biography, Songs, Hits, & Cause of Death
    Uhaul Park Merced
    Barrage Enhancement Lost Ark
    Help with your flower delivery - Don's Florist & Gift Inc.
    Space Marine 2 Error Code 4: Connection Lost [Solved]
    Pitchfork's Top 200 of the 2010s: 50-1 (clips)
    Manatee County Recorder Of Deeds
    Chuze Fitness La Verne Reviews
    Craigslist Pets Huntsville Alabama
    9781644854013
    When His Eyes Opened Chapter 2048
    Babbychula
    2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
    Panorama Charter Portal
    Noaa Duluth Mn
    Callie Gullickson Eye Patches
    All-New Webkinz FAQ | WKN: Webkinz Newz
    Craigslist/Nashville
    877-552-2666
    Slug Menace Rs3
    Craigslist Cars For Sale By Owner Memphis Tn
    Congressional hopeful Aisha Mills sees district as an economical model
    Estes4Me Payroll
    Zom 100 Mbti
    Latest Posts
    Article information

    Author: Allyn Kozey

    Last Updated:

    Views: 5960

    Rating: 4.2 / 5 (43 voted)

    Reviews: 82% of readers found this page helpful

    Author information

    Name: Allyn Kozey

    Birthday: 1993-12-21

    Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

    Phone: +2456904400762

    Job: Investor Administrator

    Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

    Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.