uotp/client.py

81 lines
No EOL
2.8 KiB
Python

import requests
import decouple
import pyotp
import argparse
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("action", choices=["list", "create", "verify", "delete"])
arg_parser.add_argument("--name")
arg_parser.add_argument("--realm")
arg_parser.add_argument("--seed")
arg_parser.add_argument("--admin-name")
arg_parser.add_argument("--admin-realm")
arg_parser.add_argument("--admin-seed")
arg_parser.add_argument("--auth-name")
arg_parser.add_argument("--auth-realm")
arg_parser.add_argument("--auth-seed")
args = arg_parser.parse_args()
action = args.action
if action == "list":
assert args.admin_name and args.admin_realm and args.admin_seed, \
"You must pass --admin-name, --admin-realm and --admin-seed"
data = \
{
"admin_name": args.admin_name,
"admin_realm": args.admin_realm,
"admin_token": pyotp.TOTP(args.admin_seed).now()
}
r = requests.post("http://localhost:{}/list".format(decouple.config('PORT')),
json=data)
print(r.content.decode("utf-8"))
elif action == "create":
assert args.name and args.realm and args.admin_name and args.admin_realm and args.admin_seed, \
"You must pass --name, --realm, --admin-name, --admin-realm and --admin-seed"
data = \
{
"name": args.name,
"realm": args.realm,
"admin_name": args.admin_name,
"admin_realm": args.admin_realm,
"admin_token": pyotp.TOTP(args.admin_seed).now()
}
r = requests.post("http://localhost:{}/create".format(decouple.config('PORT')),
json=data)
print(r.content.decode("utf-8"))
elif action == "verify":
assert args.name and args.realm and args.seed and args.auth_name and args.auth_realm and args.auth_seed, \
"You must pass --name, --realm, --seed, --auth-name, --auth-realm and --auth-seed"
data = \
{
"name": args.name,
"realm": args.realm,
"token": pyotp.TOTP(args.seed).now(),
"auth_name": args.auth_name,
"auth_realm": args.auth_realm,
"auth_token": pyotp.TOTP(args.auth_seed).now()
}
r = requests.post("http://localhost:{}/verify".format(decouple.config('PORT')),
json=data)
print(r.content.decode("utf-8"))
elif action == "delete":
assert args.name and args.admin_name and args.admin_realm and args.admin_seed, \
"You must pass --name, --admin-name, --admin-realm and --admin-seed"
data = \
{
"name": args.name,
"admin_name": args.admin_name,
"admin_realm": args.admin_realm,
"admin_token": pyotp.TOTP(args.admin_seed).now()
}
r = requests.post("http://localhost:{}/delete".format(decouple.config('PORT')),
json=data)
print(r.content.decode("utf-8"))