1
- import base64
2
1
import json
3
- import hashlib
4
- import hmac
5
- import time
6
- import urllib
7
2
import urllib2
8
- import uuid
9
3
10
4
import data
11
5
@@ -24,36 +18,22 @@ def __init__(self, hub_url, terminal_id, terminal_secret, container_id, user_id)
24
18
25
19
def get_payload (self ):
26
20
payload_url = self .hub_url + 'api/v1/containers/payload?id=' + self .container_id
27
- hmac_header = Hub .generate_hmac_header (
28
- urllib .quote (payload_url , safe = '' ),
29
- self .terminal_id ,
21
+ auth_header = Hub .generate_authentication_header (
30
22
self .terminal_secret ,
31
23
self .user_id
32
24
)
33
25
34
26
headers = {
35
- "Authorization" : "hmac " + hmac_header
27
+ "Authorization" : "FR8-TOKEN " + auth_header
36
28
}
37
29
request = urllib2 .Request (payload_url , headers = headers )
38
30
contents = urllib2 .urlopen (request ).read ()
39
31
return data .PayloadDTO .from_fr8_json (json .loads (contents ))
40
32
41
33
@staticmethod
42
- def generate_hmac_header (url , terminal_id , terminal_secret , user_id , content = bytearray ()):
43
- timestamp = str (int (time .time ()))
44
- nonce = uuid .uuid4 ()
34
+ def generate_authentication_header (terminal_secret , user_id ):
35
+ result = 'key=' + terminal_secret
36
+ if user_id :
37
+ result += ', user=' + user_id
45
38
46
- m = hashlib .md5 ()
47
- m .update (content )
48
- md5_digest = m .digest ()
49
- md5_base64 = base64 .b64encode (md5_digest )
50
-
51
- raw = terminal_id + url + timestamp + str (nonce ) + md5_base64 + user_id
52
- key_bytes = bytearray (str (terminal_secret ))
53
- message_bytes = bytearray (raw , 'utf-8' )
54
-
55
- hmac_digest = hmac .new (key_bytes , message_bytes , hashlib .sha512 ).digest ()
56
- hmac_base64 = base64 .b64encode (hmac_digest )
57
-
58
- result = terminal_id + ":" + hmac_base64 + ":" + str (nonce ) + ":" + timestamp + ":" + user_id
59
39
return result
0 commit comments