33 lines
918 B
Python
33 lines
918 B
Python
import pyotp
|
|
import decouple
|
|
import requests
|
|
from os.path import join
|
|
|
|
|
|
def is_valid_otp(etcd_client, name, realm, token):
|
|
_key = join(decouple.config("BASE_PREFIX"), name)
|
|
entry = etcd_client.get(_key, value_in_json=True)
|
|
if entry:
|
|
if realm not in entry.value["realm"]:
|
|
return False
|
|
|
|
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"), "admin")
|
|
if etcd_client.get(_key) is None:
|
|
print("admin does not exists!. So, creating one")
|
|
_value = {
|
|
"seed": pyotp.random_base32(),
|
|
"realm": [decouple.config("ADMIN_REALM")],
|
|
}
|
|
etcd_client.put(_key, _value, value_in_json=True)
|