-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.py
37 lines (32 loc) · 1.11 KB
/
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
import requests.auth
from hashlib import sha256
from urllib.parse import urlparse
import time
import base64
class QtradeAuth(requests.auth.AuthBase):
def __init__(self, key):
self.key_id, self.key = key.split(":")
def __call__(self, req):
# modify and return the request
timestamp = str(int(time.time()))
url_obj = urlparse(req.url)
request_details = req.method + "\n"
request_details += url_obj.path + url_obj.params + "\n"
request_details += timestamp + "\n"
if req.body:
if isinstance(req.body, str):
request_details += req.body + "\n"
else:
request_details += req.body.decode("utf8") + "\n"
else:
request_details += "\n"
request_details += self.key
hsh = sha256(request_details.encode("utf8")).digest()
signature = base64.b64encode(hsh)
req.headers.update(
{
"Authorization": f"HMAC-SHA256 {self.key_id}:{signature.decode()}",
"HMAC-Timestamp": timestamp,
}
)
return req