2019-10-07 17:13:42 +00:00
|
|
|
import pyotp
|
2019-10-08 18:07:28 +00:00
|
|
|
import decouple
|
2019-11-11 17:48:20 +00:00
|
|
|
import requests
|
2019-10-07 17:13:42 +00:00
|
|
|
from os.path import join
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid_otp(etcd_client, name, realm, token):
|
2019-11-11 17:48:20 +00:00
|
|
|
_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:
|
2019-11-11 17:48:20 +00:00
|
|
|
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):
|
2019-11-11 17:48:20 +00:00
|
|
|
_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")
|
2019-11-11 17:48:20 +00:00
|
|
|
_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)
|