-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdp_api_auth.py
More file actions
59 lines (48 loc) · 1.65 KB
/
sdp_api_auth.py
File metadata and controls
59 lines (48 loc) · 1.65 KB
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
'''
SDP API authentication plugin for HTTPie.
'''
from httpie.plugins import AuthPlugin
from urllib.parse import urlparse, urlunparse
from time import time
import hmac
class SDPAuth:
def __init__(self, api_key, private_key):
self.api_key = api_key
self.private_key = private_key
def __call__(self, r):
hash_url = self.extend(r.url, {
'ts': str(int(time())),
'apikey': self.api_key
})
auth_url = self.extend(hash_url, {
'hash': hmac.new(bytes(self.private_key.encode('utf-8')),
bytes(hash_url.encode('utf-8'))).hexdigest()
})
r.url = auth_url
return r
def extend(self, url, params=None):
'''
Return a url extended with name/value parameters added to the
query string.
Arguments:
url -- a URL to to parse into its components
params -- a dict of name/value parameters to add
'''
if not params:
params = {}
u = urlparse(url)
p = lambda name_val: name_val[0] + '=' + str(name_val[1])
query = '&'.join(map(p, params.items()))
if len(u.query) > 0:
query = u.query + '&' + query
path = u.path
if ' ' in path:
path = quote(path)
parts = (u.scheme, u.netloc, path, None, query, None)
return urlunparse(parts)
class SDPAuthPlugin(AuthPlugin):
name = 'SDP API authentication'
auth_type = 'sdp'
description = 'Hash URLs for the SDP API using api_key and secret.'
def get_auth(self, api_key, private_key):
return SDPAuth(api_key, private_key)