Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/venv
__pycache__
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Bandwidth Samples
Copyright (c) 2022 Bandwidth Samples

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
65 changes: 35 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
# 2FA CLI Python
<a href="http://dev.bandwidth.com"><img src="https://s3.amazonaws.com/bwdemos/BW-VMP.png"/></a>
</div>
# Multi-Factor Auth CLI

<a href="https://dev.bandwidth.com/docs/mfa">
<img src="./icon-mfa.svg" title="Multi-Factor Auth About Page" alt="Multi-Factor Auth About Page"/>
</a>

# Table of Contents

<!-- TOC -->
* [Description](#description)
* [Pre-Requisites](#pre-requisites)
* [Running the Application](#running-the-application)
* [Environmental Variables](#environmental-variables)

- [2FA CLI Python](#2fa-cli-python)
- [Description](#description)
- [Bandwidth](#bandwidth)
- [Environmental Variables](#environmental-variables)
- [Development Environment Setup](#development-environment-setup)
- [Run The App](#run-the-app)
# Description

<!-- /TOC -->
This app allows you to enter your phone number in E164 format to receive a multi-factor auth code either by voice or sms. After entering your phone number and selecting the mfa method, you will receive a text message or phone call with your authentication code. Entering this code in the final prompt will allow you to verify the code. Note that you will need separate applications for voice and messaging depending on which method you would like to use. More information about the application setup can be found in the [Pre-Requisites](#pre-requisites) section.

# Description
A small CLI sample app that creates a 2FA code request, and a 2FA code validation request via Bandwidth's 2FA API
This app also demonstrates basic API Error handling. By entering an invalid code (e.g. 1), you will receive a `400 Bad Request` from the API, which is handled by the `except` statement at the end of the app.

# Bandwidth
# Pre-Requisites

In order to use the Bandwidth 2FA API, users need to have their applications setup. Please reach out to your account manager to set this up.
In order to use the Bandwidth API users need to set up the appropriate application at the [Bandwidth Dashboard](https://dashboard.bandwidth.com/) and create API tokens.

For more information about API credentials see [here](https://dev.bandwidth.com/guides/accountCredentials.html#top)
To create an application log into the [Bandwidth Dashboard](https://dashboard.bandwidth.com/) and navigate to the `Applications` tab. Fill out the **New Application** form selecting the service (Messaging or Voice) that the application will be used for.

# Environmental Variables
The sample app uses the below environmental variables.
```sh
BW_ACCOUNT_ID # Your Bandwidth Account Id
BW_USERNAME # Your Bandwidth API Token
BW_PASSWORD # Your Bandwidth API Secret
BW_NUMBER # Your The Bandwidth Phone Number
BW_VOICE_APPLICATION_ID # Your Voice Application Id created in the dashboard
BW_MESSAGING_APPLICATION_ID # Your Messaging Application Id created in the dashboard
```
For more information about API credentials see our [Account Credentials](https://dev.bandwidth.com/docs/account/credentials) page.

# Development Environment Setup
# Running the Application

```
To install the required packages for this app, run the command:

```sh
pip install -r requirements.txt
```

# Run The App
Use the following command to run the application:

```sh
python main.py
```
python app.py

# Environmental Variables

The sample app uses the below environmental variables.

```sh
BW_ACCOUNT_ID # Your Bandwidth Account Id
BW_USERNAME # Your Bandwidth API Username
BW_PASSWORD # Your Bandwidth API Password
BW_NUMBER # The Bandwidth phone number involved with this application
BW_VOICE_APPLICATION_ID # Your Voice Application Id created in the dashboard
BW_MESSAGING_APPLICATION_ID # Your Messaging Application Id created in the dashboard
```
95 changes: 0 additions & 95 deletions app.py

This file was deleted.

1 change: 1 addition & 0 deletions icon-mfa.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import os
import re

# TODO: Needs to be changed when the SDK becomes python package.
import sys
sys.path.insert(0, 'C:/Users/ckoegel/Documents/sdks/bandwidth_python')
import bandwidth_python
from bandwidth_python.api.mfa_api import MFAApi
from bandwidth_python.model.two_factor_code_request_schema import TwoFactorCodeRequestSchema
from bandwidth_python.model.two_factor_verify_request_schema import TwoFactorVerifyRequestSchema
from bandwidth_python.exceptions import ApiException
# ---------------------------------------------------


BW_ACCOUNT_ID = os.environ.get('BW_ACCOUNT_ID')
BW_USERNAME = os.environ.get('BW_USERNAME')
BW_PASSWORD = os.environ.get('BW_PASSWORD')
BW_NUMBER = os.environ.get('BW_NUMBER')
BW_VOICE_APPLICATION_ID = os.environ.get('BW_VOICE_APPLICATION_ID')
BW_MESSAGING_APPLICATION_ID = os.environ.get('BW_MESSAGING_APPLICATION_ID')


configuration = bandwidth_python.Configuration( # TODO: # Configure HTTP basic authorization: httpBasic
username=BW_USERNAME,
password=BW_PASSWORD
)


api_client = bandwidth_python.ApiClient(configuration) # TODO: package name
mfa_api_instance = MFAApi(api_client) # TODO: package name

recipient_phone_number = input("\nPlease enter your phone number in E164 format (+19195551234): ")
while True:
if re.match(r"^\+[1-9]\d{4,14}$", recipient_phone_number):
break
else:
recipient_phone_number = input("Invalid phone number. Please enter your phone number in E164 format (+19195551234): ")



delivery_method = input("\nPlease select your MFA method.\nEnter 0 for voice or 1 for messaging: ")
while True:
if re.match(r"^[0-1]$", delivery_method):
break
else:
delivery_method = input("Invalid selection. Enter 0 for voice or 1 for messaging: ")


if bool(int(delivery_method)):

body = TwoFactorCodeRequestSchema(
_from = BW_NUMBER,
to = recipient_phone_number,
application_id = BW_MESSAGING_APPLICATION_ID,
scope = "scope",
digits = 6.0,
message = "Your temporary {NAME} {SCOPE} code is {CODE}"
)
mfa_api_instance.messaging_two_factor(BW_ACCOUNT_ID, body)

code = input("\nPlease enter your received code: ")

body = TwoFactorVerifyRequestSchema(
to = recipient_phone_number,
application_id = BW_MESSAGING_APPLICATION_ID,
scope = "scope",
code = code,
expiration_time_in_minutes = 3.0
)
else:
body = TwoFactorCodeRequestSchema(
_from = BW_NUMBER,
to = recipient_phone_number,
application_id = BW_VOICE_APPLICATION_ID,
scope = "scope",
digits = 6.0,
message = "Your temporary {NAME} {SCOPE} code is {CODE}"
)
mfa_api_instance.voice_two_factor(BW_ACCOUNT_ID, body)

code = input("\nPlease enter your received code: ")

body = TwoFactorVerifyRequestSchema(
to = recipient_phone_number,
application_id = BW_VOICE_APPLICATION_ID,
scope = "scope",
code = code,
expiration_time_in_minutes = 3.0
)

try:
response = mfa_api_instance.verify_two_factor(BW_ACCOUNT_ID, body)

if response.valid:
print("Success!")
else:
print("Incorrect Code")

except ApiException as e:
print(e)