uotp/helper.py

30 lines
822 B
Python

import pyotp
import decouple
from os.path import join
def is_valid_otp(etcd_client, name, realm, token):
_key = join(decouple.config("BASE_PREFIX"), realm, name)
entry = etcd_client.get(_key, value_in_json=True)
if entry:
totp = pyotp.TOTP(entry.value["seed"])
try:
is_token_valid = totp.verify(token)
except:
return False
else:
return is_token_valid
return False
def create_admin_if_dont_exists(etcd_client):
_key = join(
decouple.config("BASE_PREFIX"),
decouple.config("ADMIN_REALM"),
"admin",
)
if etcd_client.get(_key) is None:
print("admin does not exists!. So, creating one")
_value = {"seed": pyotp.random_base32()}
etcd_client.put(_key, _value, value_in_json=True)