2020-01-03 10:02:39 +00:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
import binascii
|
|
|
|
|
|
|
|
from pyotp import TOTP
|
|
|
|
from os.path import join as join_path
|
2020-01-06 07:25:59 +00:00
|
|
|
from uncloud.common.settings import settings
|
2020-01-03 10:02:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_otp_parser():
|
|
|
|
otp_parser = argparse.ArgumentParser('otp')
|
2020-01-10 11:39:40 +00:00
|
|
|
try:
|
|
|
|
name = settings['client']['name']
|
|
|
|
realm = settings['client']['realm']
|
|
|
|
seed = settings['client']['seed']
|
|
|
|
except Exception:
|
|
|
|
otp_parser.add_argument('--name', required=True)
|
|
|
|
otp_parser.add_argument('--realm', required=True)
|
|
|
|
otp_parser.add_argument('--seed', required=True, type=get_token, dest='token', metavar='SEED')
|
|
|
|
else:
|
|
|
|
otp_parser.add_argument('--name', default=name)
|
|
|
|
otp_parser.add_argument('--realm', default=realm)
|
|
|
|
otp_parser.add_argument('--seed', default=seed, type=get_token, dest='token', metavar='SEED')
|
|
|
|
|
2020-01-03 10:02:39 +00:00
|
|
|
return otp_parser
|
|
|
|
|
|
|
|
|
|
|
|
def load_dump_pretty(content):
|
|
|
|
if isinstance(content, bytes):
|
|
|
|
content = content.decode('utf-8')
|
|
|
|
parsed = json.loads(content)
|
|
|
|
return json.dumps(parsed, indent=4, sort_keys=True)
|
|
|
|
|
|
|
|
|
|
|
|
def make_request(*args, data=None, request_method=requests.post):
|
|
|
|
r = request_method(join_path(settings['client']['api_server'], *args), json=data)
|
|
|
|
try:
|
|
|
|
print(load_dump_pretty(r.content))
|
|
|
|
except Exception:
|
|
|
|
print('Error occurred while getting output from api server.')
|
|
|
|
|
|
|
|
|
|
|
|
def get_token(seed):
|
|
|
|
if seed is not None:
|
|
|
|
try:
|
|
|
|
token = TOTP(seed).now()
|
|
|
|
except binascii.Error:
|
|
|
|
raise argparse.ArgumentTypeError('Invalid seed')
|
|
|
|
else:
|
|
|
|
return token
|