-
Notifications
You must be signed in to change notification settings - Fork 5
/
api-call.py
executable file
·62 lines (50 loc) · 2.07 KB
/
api-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
#!/usr/bin/python
import argparse
import urllib.error
import urllib.request
import requests
from requests.auth import HTTPBasicAuth
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
def get_www_authenticate_header(api_url):
try:
resp = urllib.request.urlopen(api_url)
response = resp.read()
except urllib.error.HTTPError as error:
response = error.info()['Www-Authenticate']
return response
def get_token(user, password, service, scope, realm):
data = {"scope":scope, "service":service, "account":user}
r = requests.post(realm, auth=HTTPBasicAuth(user, password), data=data)
token=find_between(str(r.content), 'token":"', '"')
return token
def get_result(api_url, token):
r = requests.get(api_url, headers={'Authorization':'Bearer ' + token})
return r.content
def main():
#get the Www-Authenticate header
params=get_www_authenticate_header(args.api_url)
#parse the params required for the token
if params:
realm=find_between(params, 'realm="', '"')
service=find_between(params, 'service="', '"')
scope=find_between(params, 'scope="', '"')
# retrieve token
token = get_token(args.user, args.password, service, scope, realm)
# Do the API call as an authenticated user
print("Response:")
print(get_result(args.api_url, token))
else:
print("404 Not Found")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Tool for making api calls to a private docker registry that requires authentication through a cesanta/docker_auth server.')
parser.add_argument('--user', required=True, help='Username of the account used to make the request')
parser.add_argument('--password', required=True, help='Password of the account used to make the request')
parser.add_argument('--api_url', required=True, help='The API url that you want to access.')
args = parser.parse_args()
main()