2019-06-24 10:46:06 +00:00
|
|
|
import binascii
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from decouple import config
|
|
|
|
from pyotp import TOTP
|
2019-06-24 11:39:53 +00:00
|
|
|
from etcd import EtcdKeyNotFound
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-24 12:43:48 +00:00
|
|
|
|
2019-06-24 10:46:06 +00:00
|
|
|
def check_otp(name, realm, seed):
|
|
|
|
try:
|
|
|
|
data = {
|
|
|
|
"auth_name": config('AUTH_NAME', ''),
|
|
|
|
"auth_token": TOTP(config('AUTH_SEED', '')).now(),
|
|
|
|
"auth_realm": config('AUTH_REALM', ''),
|
|
|
|
"name": name,
|
|
|
|
"realm": realm,
|
|
|
|
"token": TOTP(seed).now()
|
|
|
|
}
|
|
|
|
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 get_next_id(client, path):
|
2019-06-24 11:39:53 +00:00
|
|
|
try:
|
|
|
|
r = client.read(path)
|
|
|
|
except EtcdKeyNotFound:
|
|
|
|
return 0
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
max_key_result = max(r.children, key=lambda x: int(strip_nondigit(x.key)))
|
|
|
|
if max_key_result is None:
|
|
|
|
# No key found
|
|
|
|
return 0
|
|
|
|
|
|
|
|
max_key = strip_nondigit(max_key_result.key.split("/")[-1]) # Get the last portion of key
|
|
|
|
|
|
|
|
return int(max_key) + 1
|
|
|
|
|
|
|
|
|
|
|
|
def strip_nondigit(s):
|
|
|
|
return "".join([char for char in s if char.isdigit()])
|