-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_curl_call.py
executable file
·73 lines (52 loc) · 1.8 KB
/
generate_curl_call.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
#! /bin/python3
import argparse
from duo_hmac import duo_hmac
import check_credentials as cc
def get_arguments(parser):
parser.add_argument(
"-m", choices=["get", "post"], default="get", help="HTTP method"
)
parser.add_argument("-a", help="API path", required=True)
parser.add_argument(
"-p",
default=[],
help="API call parameters as k=v pairs",
metavar="KEY=VALUE",
nargs="*",
)
args = parser.parse_args()
args_dict = {
"method": args.m.upper(),
"path": args.a,
"params": {p[0]: p[1] for p in [item.split("=") for item in args.p]},
}
return args_dict
def main():
ikey, skey, host = cc._read_config()
parser = argparse.ArgumentParser(
prog="Duo API call generator for curl",
description="""Generates a curl call for a Duo API call.
Provide the HTTP method (default 'get'),
the API path, and the call parameters as
key=value pairs""",
epilog="""CLI flags: -m <HTTP method> -a <api path>
-p key1=value1 key2=value2 ...""",
)
args_dict = get_arguments(parser)
hmac = duo_hmac.DuoHmac(ikey, skey, host)
uri, body, headers = hmac.get_authentication_components(
args_dict["method"],
args_dict["path"],
args_dict["params"],
)
headers_list = [f"{key}: {value}" for key, value in headers.items()]
curl_command = "curl "
curl_command = curl_command + f' -X {args_dict["method"]} '
for header in headers_list:
curl_command = curl_command + f' -H "{header}" '
if body:
curl_command = curl_command + f" -d '{body}' "
curl_command = curl_command + f" https://{uri}"
print(curl_command)
if __name__ == "__main__":
main()