-
Notifications
You must be signed in to change notification settings - Fork 10
/
app_token.py
153 lines (109 loc) · 4.64 KB
/
app_token.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import base64
import io
import struct
import time
import util.sign_utils
import util.bytes_utils
import util.compress_utils
import random
from app_token_options import AppTokenOptions
from service import Service
VERSION_LENGTH = 3
VERSION_0 = "000"
class AppToken:
def __init__(self, app_id, app_key, timestamp, issue_timestamp=None, salt=None,
service=None,
options=None, signature=None):
self.app_id = app_id
self.app_key = app_key
self.timestamp = timestamp
self.issue_timestamp = int(time.time()) if issue_timestamp is None else issue_timestamp
self.salt = random.randint(1, self.issue_timestamp) if salt is None else salt
self.service = service
self.options = options
self.signature = signature
def set_service(self, service):
self.service = service
def set_options(self, options):
self.options = options
def build(self):
if self.app_key is None:
raise ValueError('missing secretKey')
if self.service is None:
raise ValueError('missing service')
signKey = util.sign_utils.generate_sign(self.app_key, self.issue_timestamp, self.salt)
bufBody = io.BytesIO()
app_id_bytes = self.app_id.encode('utf-8')
bufBody.write(struct.pack('>I', len(app_id_bytes)))
bufBody.write(app_id_bytes)
bufBody.write(struct.pack('>I', self.issue_timestamp))
bufBody.write(struct.pack('>I', self.salt))
bufBody.write(struct.pack('>I', self.timestamp))
bufBody.write(self.service.pack())
if self.options is None:
self.options = AppTokenOptions()
bufBody.write(self.options.pack())
# FIXME
bufBodyFixedLength = util.bytes_utils.get_fixed_length_bytes(bufBody.getvalue())
signature = util.sign_utils.sign(signKey, bufBodyFixedLength)
buf = io.BytesIO()
buf.write(struct.pack('>I', len(signature)))
buf.write(signature)
buf.write(bufBodyFixedLength)
bufFixedLength = util.bytes_utils.get_fixed_length_bytes(buf.getvalue())
return "{}{}".format(VERSION_0, base64.b64encode(util.compress_utils.compress(bufFixedLength)).decode('utf-8'))
@classmethod
def parse(cls, token):
if token is None:
raise ValueError('empty appToken')
if not token.startswith(VERSION_0):
raise ValueError('unsupported version')
data = util.compress_utils.decompress(base64.b64decode(token[VERSION_LENGTH:]))
buf = io.BytesIO(data)
signature_length = struct.unpack('>I', buf.read(4))[0]
signature = buf.read(signature_length)
app_id_length = struct.unpack('>I', buf.read(4))[0]
app_id = buf.read(app_id_length).decode('utf-8')
issue_timestamp = struct.unpack('>I', buf.read(4))[0]
salt = struct.unpack('>I', buf.read(4))[0]
timestamp = struct.unpack('>I', buf.read(4))[0]
service = Service.unpack(buf)
options = AppTokenOptions.unpack(buf)
return AppToken(app_id, None, timestamp, issue_timestamp, salt, service, options, signature)
def validate(self, app_key):
if app_key is None:
raise ValueError('missing appKey')
signKey = util.sign_utils.generate_sign(app_key, self.issue_timestamp, self.salt)
buf = io.BytesIO()
app_id_bytes = self.app_id.encode('utf-8')
buf.write(struct.pack('>I', len(app_id_bytes)))
buf.write(app_id_bytes)
buf.write(struct.pack('>I', self.issue_timestamp))
buf.write(struct.pack('>I', self.salt))
buf.write(struct.pack('>I', self.timestamp))
buf.write(self.service.pack())
if self.options is None:
self.options = AppTokenOptions()
buf.write(self.options.pack())
signature = util.sign_utils.sign(signKey, util.bytes_utils.get_fixed_length_bytes(buf.getvalue()))
return signature == self.signature
if __name__ == "__main__":
app_id = "appId"
app_key = "appKey"
expired_ts = int(time.time()) + 12 * 60 * 60 # expired after 12h
appToken = AppToken(app_id, app_key, expired_ts)
channel_id = 'channelId'
user_id = 'userId'
service = Service(channel_id, user_id)
service.add_audio_publish_privilege()
appToken.set_service(service)
engine_options = {}
for i in range(1,10):
engine_options["key-{}".format(i)] = "value-{}".format(i)
options = AppTokenOptions()
options.set_engine_options(engine_options)
appToken.set_options(options)
token = appToken.build()
print(token)
appToken = AppToken.parse(token)
print(appToken.validate(app_key))