-
Notifications
You must be signed in to change notification settings - Fork 721
Expand file tree
/
Copy pathcbpro_auth.py
More file actions
41 lines (36 loc) · 1.35 KB
/
cbpro_auth.py
File metadata and controls
41 lines (36 loc) · 1.35 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
import hmac
import hashlib
import time
import base64
from requests.auth import AuthBase
class CBProAuth(AuthBase):
# Provided by CBPro: https://docs.pro.coinbase.com/#signing-a-message
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = (
timestamp
+ request.method
+ request.path_url
+ (request.body or b"").decode()
)
request.headers.update(get_auth_headers(timestamp, message,
self.api_key,
self.secret_key,
self.passphrase))
return request
def get_auth_headers(timestamp, message, api_key, secret_key, passphrase):
message = message.encode('ascii')
hmac_key = base64.b64decode(secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest()).decode('utf-8')
return {
'Content-Type': 'Application/JSON',
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': api_key,
'CB-ACCESS-PASSPHRASE': passphrase
}