-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck_credentials.py
executable file
·95 lines (68 loc) · 2.47 KB
/
check_credentials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /bin/python3
import configparser
import json
import requests
from duo_hmac import duo_hmac
CONFIG_FILE = "duo.conf"
DUO_SECTION = "duo"
IKEY_KEY = "ikey"
SKEY_KEY = "skey"
API_HOST_KEY = "api_host"
def main():
# Read from config or error out
ikey, skey, api_host = _read_config()
duo = duo_hmac.DuoHmac(ikey, skey, api_host)
# Try to call 'check' and look at return
status_code, json_content = _attempt_api_call(duo, "/auth/v2/check")
if status_code == 200:
print("Your credentials successfully called the Auth API")
return
if not json_content:
print(f"API call failed with status {status_code}")
return
if json_content["code"] != 40301:
print(f"API call failed with status {status_code}: {json_content['message']}")
return
# if 40301, creds are probably admin; try settings endpoint
status_code, json_content = _attempt_api_call(duo, "/admin/v1/settings")
if status_code == 200:
print("Your credentials successfully called the Admin API")
elif json_content:
print(f"API call failed with status {status_code}: {json_content['message']}")
else:
print(f"API call failed with status code {status_code}")
def _attempt_api_call(duo, path):
uri, _, headers = duo.get_authentication_components("GET", path, {}, {})
response = requests.get(f"https://{uri}", headers=headers)
status_code = response.status_code
json_content = json.loads(response.content)
return status_code, json_content
def _read_config():
cp = configparser.ConfigParser()
cp.read(CONFIG_FILE)
if not cp.sections():
raise FileNotFoundError(
f"Config file {CONFIG_FILE} seems to be missing or empty."
)
if "duo" not in cp.sections():
raise ValueError(
f"Config file {CONFIG_FILE} seems to be missing a '{DUO_SECTION}' section."
)
ikey = cp[DUO_SECTION][IKEY_KEY]
if not ikey:
raise ValueError(
f"Missing entry for '{IKEY_KEY}' in {CONFIG_FILE} '{DUO_SECTION}'"
)
skey = cp[DUO_SECTION][SKEY_KEY]
if not skey:
raise ValueError(
f"Missing entry for '{SKEY_KEY}' in {CONFIG_FILE} '{DUO_SECTION}'"
)
api_host = cp[DUO_SECTION][API_HOST_KEY]
if not api_host:
raise ValueError(
f"Missing entry for '{API_HOST_KEY}' in {CONFIG_FILE} '{DUO_SECTION}'"
)
return ikey, skey, api_host
if __name__ == "__main__":
main()