smgc commited on
Commit
647ab21
·
verified ·
1 Parent(s): 8792305

Update bearer_token.py

Browse files
Files changed (1) hide show
  1. bearer_token.py +20 -55
bearer_token.py CHANGED
@@ -1,61 +1,26 @@
1
- import hmac
2
- import hashlib
3
  import base64
4
- import requests
 
 
5
  from datetime import datetime, timezone
6
 
7
  class BearerTokenGenerator:
8
- def __init__(self, secret_key, secret_auth_prefix, path, base_url, api_version):
9
- self.secret_key = secret_key
10
- self.secret_auth_prefix = secret_auth_prefix
11
- self.path = path
12
- self.base_url = base_url
13
- self.api_version = api_version
14
-
15
- def generate_signature(self, to_sign):
16
- hmac_obj = hmac.new(self.secret_key, to_sign, hashlib.sha256)
17
- return base64.b64encode(hmac_obj.digest()).decode('utf-8')
18
-
19
- def get_bearer_token(self, body):
20
- # 获取当前UTC时间戳
21
- timestamp = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
22
- prefix = f"POST:{self.path}:{timestamp}\n".encode('utf-8')
23
-
24
- # 生成签名
25
- signature = self.generate_signature(prefix + body)
26
-
27
- # 编码secretAuthPrefix
28
- encoded_secret_auth_prefix = base64.b64encode(self.secret_auth_prefix).decode('utf-8')
29
-
30
- # 构建Bearer Token
31
- bearer_token = f"Bearer {encoded_secret_auth_prefix}.{signature}"
32
-
33
  return bearer_token, timestamp
34
 
35
- def make_request(self, body):
36
- bearer_token, timestamp = self.get_bearer_token(body)
37
- headers = {
38
- "Date": timestamp,
39
- "Client-time-zone": "-04:00",
40
- "Authorization": bearer_token,
41
- "User-Agent": f"ChatOn_Android/{self.api_version}",
42
- "Accept-Language": "en-US",
43
- "X-Cl-Options": "hb",
44
- "Content-Type": "application/json; charset=UTF-8"
45
- }
46
- response = requests.post(self.base_url + self.path, headers=headers, data=body)
47
- return response.text
48
-
49
- # 示例使用
50
- if __name__ == "__main__":
51
- secret_key = bytes([14, 94, 79, 102, 38, 245, 11, 65, 100, 43, 115, 94, 15, 241, 14, 16, 66, 129, 248, 226, 98, 109, 235, 60, 62, 41, 78, 29, 72, 181, 47, 8])
52
- secret_auth_prefix = bytes([252, 137, 185, 155, 127, 94, 106, 81, 69, 242, 189, 184, 26, 228, 174, 239])
53
- path = "/chats/stream"
54
- base_url = "https://example.com/api" # 替换为实际的BaseURL
55
- api_version = "1.0"
56
-
57
- body = '{"key": "value"}'.encode('utf-8')
58
-
59
- generator = BearerTokenGenerator(secret_key, secret_auth_prefix, path, base_url, api_version)
60
- response = generator.make_request(body)
61
- print(response)
 
 
 
1
  import base64
2
+ import hashlib
3
+ import hmac
4
+ import time
5
  from datetime import datetime, timezone
6
 
7
  class BearerTokenGenerator:
8
+ SECRET_AUTH_PREFIX = bytes([252, 137, 185, 155, 127, 94, 106, 81, 69, 242, 189, 184, 26, 228, 174, 239])
9
+ SECRET_KEY = bytes([14, 94, 79, 102, 38, 245, 11, 65, 100, 43, 115, 94, 15, 241, 14, 16, 66, 129, 248, 226, 98, 109, 235, 60, 62, 41, 78, 29, 72, 181, 47, 8])
10
+
11
+ @staticmethod
12
+ def get_bearer(body: str, path: str = "/chats/stream") -> tuple:
13
+ timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
14
+ prefix = f"POST:{path}:{timestamp}\n".encode()
15
+ to_sign = prefix + body.encode()
16
+
17
+ signature = BearerTokenGenerator.generate_signature(to_sign)
18
+ auth_prefix_base64 = base64.b64encode(BearerTokenGenerator.SECRET_AUTH_PREFIX).decode()
19
+
20
+ bearer_token = f"Bearer {auth_prefix_base64}.{signature}"
 
 
 
 
 
 
 
 
 
 
 
 
21
  return bearer_token, timestamp
22
 
23
+ @staticmethod
24
+ def generate_signature(to_sign: bytes) -> str:
25
+ h = hmac.new(BearerTokenGenerator.SECRET_KEY, to_sign, hashlib.sha256)
26
+ return base64.b64encode(h.digest()).decode()