ucloud-api/helper.py

40 lines
1,007 B
Python

import binascii
import requests
from decouple import config
from pyotp import TOTP
def check_otp(name, realm, token):
try:
data = {
"auth_name": config("AUTH_NAME", ""),
"auth_token": TOTP(config("AUTH_SEED", "")).now(),
"auth_realm": config("AUTH_REALM", ""),
"name": name,
"realm": realm,
"token": token,
}
except binascii.Error:
return 400
response = requests.post(
"{OTP_SERVER}{OTP_VERIFY_ENDPOINT}".format(
OTP_SERVER=config("OTP_SERVER", ""),
OTP_VERIFY_ENDPOINT=config("OTP_VERIFY_ENDPOINT", "verify/"),
),
data=data,
)
return response.status_code
def add_otp_args(parser):
parser.add_argument("name", required=True)
parser.add_argument("realm", required=True)
parser.add_argument("token", required=True)
return parser
def add_vmid_args(parser):
parser.add_argument("vmid", required=True)
return parser