uotp/helper.py

34 lines
918 B
Python
Raw Normal View History

2019-10-07 17:13:42 +00:00
import pyotp
2019-10-08 18:07:28 +00:00
import decouple
import requests
2019-10-07 17:13:42 +00:00
from os.path import join
def is_valid_otp(etcd_client, name, realm, token):
_key = join(decouple.config("BASE_PREFIX"), name)
2019-10-07 17:13:42 +00:00
entry = etcd_client.get(_key, value_in_json=True)
if entry:
if realm not in entry.value["realm"]:
return False
2019-10-08 18:07:28 +00:00
totp = pyotp.TOTP(entry.value["seed"])
2019-10-07 17:13:42 +00:00
try:
is_token_valid = totp.verify(token)
except:
return False
else:
return is_token_valid
2019-10-08 18:07:28 +00:00
return False
def create_admin_if_dont_exists(etcd_client):
_key = join(decouple.config("BASE_PREFIX"), "admin")
2019-10-08 18:07:28 +00:00
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")],
}
2019-10-08 18:07:28 +00:00
etcd_client.put(_key, _value, value_in_json=True)