Generate Bypass Code

This is a Python script that generates a single bypass code for an individual user. You must have the my_duo_keys on the same directory of the script you want to run. Check this article if you need help in how to obtain the integration keys: My Duo Keys for Admin API using Python.


import duo_client
import pytz
from pytz import timezone
from datetime import datetime, timedelta
from colorama import init
from colorama import Fore, Back, Style

from my_duo_keys import *
admin_api = duo_client.Admin(ikey=DUO_IKEY, skey=DUO_SKEY, host=DUO_APIHOSTNAME)
init()
print(" ")
tz_PST = timezone('Asia/Singapore')
def getnow():
    sa_time = datetime.now(tz_PST)
    current_time = (sa_time.strftime('%Y-%m-%d %H:%M:%S') + " " + str(tz_PST))
    print(Fore.GREEN + "Time: " + current_time)
    
def printline(): print(Fore.CYAN + "-------------------------------------------------------------------")
printline()
print(Fore.GREEN + "************ GENERATING BYPASS CODE FOR INDIVIDUAL USER ************")
printline()
print(Fore.GREEN )
username=input("Enter Duo Username: ")
userdata=admin_api.get_users_by_name(username=username)
if not userdata: 
    print(Fore.RED + "User not found in Duo! Please get the correct username and retry.")
else:
    print(" ")
    print(Fore.GREEN + "Bypass Code Parameters:")
    valid_secs=input("Total Number of Seconds Valid (Default: 86400/1 day): ")
    if not valid_secs: valid_secs="86400 (Default)"
    valid_days=int(valid_secs) / 86400
    remaining_uses=input("Number of Use (Default: 0/Unlimited): ")
    if not remaining_uses: remaining_uses="0"
    numberofusage=remaining_uses
    if remaining_uses == "0": numberofusage="Unlimited (Default)"
    print(" ")
    print(Fore.GREEN + "Generating bypass code for " + Fore.CYAN + username + Fore.GREEN + "...")
    for u in userdata: user_id=u['user_id']
    try:
        setbypass = admin_api.add_user_bypass_codes(user_id=user_id,valid_secs=valid_secs,remaining_uses=remaining_uses,count="1")
        newbypasscode = str(setbypass)
        newbypasscode = newbypasscode.replace("[","")
        newbypasscode = newbypasscode.replace("]","")
        newbypasscode = newbypasscode.replace("'","")
        print(Fore.GREEN + "SUCCESS! " + "New Bypass Code: " + Fore.CYAN + newbypasscode)
        print(Fore.GREEN + "[Parameters]")
        print(Fore.GREEN + "* Number of Day(s) Valid: " + Fore.CYAN + str(valid_days) )
        print(Fore.GREEN + "* Number of Usage: " + Fore.CYAN + numberofusage)
        print(Fore.GREEN + "* Number of Codes to Generate: " + Fore.CYAN + "1 (Default)")
    except Exception as e:
        print(Fore.RED + "FAILED!")
print(" ")            
printline()
getnow()
printline()

Sample Output: