-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_auth.py
executable file
·50 lines (42 loc) · 1.68 KB
/
http_auth.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
#!/usr/bin/python
import requests
import hmac
import hashlib
import urlparse
import urllib
import argparse
def generate_signature(key, method, url, headers=None):
parsed_url = urlparse.urlparse(url)
queries = urlparse.parse_qsl(parsed_url.query)
queries.sort()
text = ''
text += method + '\n'
text += parsed_url.path
if len(queries) > 0:
text += '?' + urllib.urlencode(queries)
text += '\n'
if headers:
headers = map(lambda x: (x[0].lower(), x[1]), headers.iteritems())
headers = filter(lambda x: x[0].startswith('x-ell-'), headers)
headers.sort()
for header in headers:
text += header[0] + ':' + header[1] + '\n'
return hmac.new(key, text, hashlib.sha512).hexdigest()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Send request to rift.')
parser.add_argument('url', metavar='URL', help='Url for processing')
parser.add_argument('--file', dest='file', action='store', default=None, help='File to send with POST request')
parser.add_argument('--user', dest='user', action='store', default=None, help='File to send with POST request')
parser.add_argument('--token', dest='token', action='store', default=None, help='File to send with POST request')
args = parser.parse_args()
headers = {}
if args.token:
headers['Authorization'] = generate_signature(args.token, 'POST' if args.file else 'GET', args.url)
if not args.file:
r = requests.get(args.url, headers=headers)
else:
with open(args.file) as f:
data = f.read()
r = requests.post(args.url, data, headers=headers)
print r.status_code
print r.content