Skip to content

Commit f91496e

Browse files
author
yutingzeng
committed
增加使用字符串房间号生成privateMapKey
1 parent 2427b4d commit f91496e

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

TLSSigAPIv2.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,19 @@ def __init__(self, sdkappid, key):
4747
# @param dwExpTime 过期时间:该权限加密串的过期时间,实际过期时间:当前时间+dwExpTime
4848
# @param dwPrivilegeMap 用户权限,255表示所有权限
4949
# @param dwAccountType 用户类型,默认为0
50+
# @param roomStr 字符串房间号,数字房间号非0时存在
5051
# @return userbuf {string} 返回的userbuf
5152
#/
52-
def _gen_userbuf(self,account, dwAuthID, dwExpTime,
53-
dwPrivilegeMap, dwAccountType):
53+
def _gen_userbuf(self, account, dwAuthID, dwExpTime,
54+
dwPrivilegeMap, dwAccountType, roomStr):
5455
userBuf = b''
5556

57+
if len(roomStr) > 0 :
58+
userBuf += bytearray([1])
59+
else :
60+
userBuf += bytearray([0])
61+
5662
userBuf += bytearray([
57-
0,
5863
((len(account) & 0xFF00) >> 8),
5964
(len(account) & 0x00FF),
6065
])
@@ -100,6 +105,12 @@ def _gen_userbuf(self,account, dwAuthID, dwExpTime,
100105
((dwAccountType & 0x0000FF00) >> 8),
101106
(dwAccountType & 0x000000FF),
102107
])
108+
if len(roomStr) > 0 :
109+
userBuf += bytearray([
110+
((len(roomStr) & 0xFF00) >> 8),
111+
(len(roomStr) & 0x00FF),
112+
])
113+
userBuf += bytearray(map(ord, roomStr))
103114
return userBuf
104115
def __hmacsha256(self, identifier, curr_time, expire, base64_userbuf=None):
105116
""" 通过固定串进行 hmac 然后 base64 得的 sig 字段的值"""
@@ -171,17 +182,48 @@ def genUserSig(self, userid, expire=180*86400):
171182
#/
172183
def genPrivateMapKey(self, userid, expire, roomid, privilegeMap):
173184
""" 带 userbuf 生成签名 """
174-
userbuf = self._gen_userbuf(userid,roomid,expire,privilegeMap,0)
185+
userbuf = self._gen_userbuf(userid,roomid,expire,privilegeMap,0,"")
175186
print(userbuf)
176187
return self.__gen_sig(userid, expire, userbuf)
177188

189+
##
190+
#【功能说明】
191+
# 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
192+
# PrivateMapKey 需要跟 UserSig 一起使用,但比 UserSig 有更强的权限控制能力:
193+
# - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
194+
# - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
195+
# 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中“启动权限密钥”。
196+
#
197+
#【参数说明】
198+
# userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
199+
# expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
200+
# roomstr - 字符串房间号,用于指定该 userid 可以进入的房间号
201+
# privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限:
202+
# - 第 1 位:0000 0001 = 1,创建房间的权限
203+
# - 第 2 位:0000 0010 = 2,加入房间的权限
204+
# - 第 3 位:0000 0100 = 4,发送语音的权限
205+
# - 第 4 位:0000 1000 = 8,接收语音的权限
206+
# - 第 5 位:0001 0000 = 16,发送视频的权限
207+
# - 第 6 位:0010 0000 = 32,接收视频的权限
208+
# - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
209+
# - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
210+
# - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
211+
# - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
212+
#/
213+
def genPrivateMapKeyWithStringRoomID(self, userid, expire, roomstr, privilegeMap):
214+
""" 带 userbuf 生成签名 """
215+
userbuf = self._gen_userbuf(userid,0,expire,privilegeMap,0,roomstr)
216+
print(userbuf)
217+
return self.__gen_sig(userid, expire, userbuf)
178218

179219
def main():
180220
api = TLSSigAPIv2(1400000000, '5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e')
181221
sig = api.genUserSig("xiaojun")
182222
print(sig)
183223
sig = api.genPrivateMapKey("xiaojun", 86400*180,10000,255)
184224
print(sig)
225+
sig = api.genPrivateMapKeyWithStringRoomID("xiaojun", 86400*180,"adbjkl",255)
226+
print(sig)
185227

186228

187229
if __name__ == "__main__":

0 commit comments

Comments
 (0)