Skip to content

Commit 70eee07

Browse files
author
yutingzeng
committed
增加使用字符串房间号生成privateMapKey
1 parent a250725 commit 70eee07

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

TLSSigAPITest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ func main() {
2323
} else {
2424
fmt.Println(sig)
2525
}
26+
sig, err = tencentyun.GenPrivateMapKeyWithStringRoomID(sdkappid, key, "xiaojun", 86400*180, "agjk", 255)
27+
if err != nil {
28+
fmt.Println(err.Error())
29+
} else {
30+
fmt.Println(sig)
31+
}
2632
}

tencentyun/TLSSigAPI.go

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,55 @@ func GenUserSig(sdkappid int, key string, userid string, expire int) (string, er
5252
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
5353
*/
5454
func GenPrivateMapKey(sdkappid int, key string, userid string, expire int, roomid uint32, privilegeMap uint32) (string, error) {
55-
var userbuf []byte = genUserBuf(userid, sdkappid, roomid, expire, privilegeMap, 0)
55+
var userbuf []byte = genUserBuf(userid, sdkappid, roomid, expire, privilegeMap, 0, "")
56+
return genSig(sdkappid, key, userid, expire, userbuf)
57+
}
58+
59+
/**
60+
*【功能说明】
61+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
62+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
63+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
64+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
65+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
66+
*
67+
*【参数说明】
68+
* sdkappid - 应用id。
69+
* key - 计算 usersig 用的加密密钥,控制台可获取。
70+
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
71+
* expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
72+
* roomStr - 字符串房间号,用于指定该 userid 可以进入的房间号
73+
* privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
74+
* - 第 1 位:0000 0001 = 1,创建房间的权限
75+
* - 第 2 位:0000 0010 = 2,加入房间的权限
76+
* - 第 3 位:0000 0100 = 4,发送语音的权限
77+
* - 第 4 位:0000 1000 = 8,接收语音的权限
78+
* - 第 5 位:0001 0000 = 16,发送视频的权限
79+
* - 第 6 位:0010 0000 = 32,接收视频的权限
80+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
81+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
82+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
83+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
84+
*/
85+
func GenPrivateMapKeyWithStringRoomID(sdkappid int, key string, userid string, expire int, roomStr string, privilegeMap uint32) (string, error) {
86+
var userbuf []byte = genUserBuf(userid, sdkappid, 0, expire, privilegeMap, 0, roomStr)
5687
return genSig(sdkappid, key, userid, expire, userbuf)
5788
}
5889
func genUserBuf(account string, dwSdkappid int, dwAuthID uint32,
59-
dwExpTime int, dwPrivilegeMap uint32, dwAccountType uint32) []byte {
90+
dwExpTime int, dwPrivilegeMap uint32, dwAccountType uint32, roomStr string) []byte {
6091

6192
offset := 0
62-
length := 1 + 2 + len(account) + 20
93+
94+
length := 1 + 2 + len(account) + 20 + len(roomStr)
6395
userBuf := make([]byte, length)
6496

65-
userBuf[offset] = 0
97+
//ver
98+
if len(roomStr) > 0 {
99+
userBuf[offset] = 1
100+
} else {
101+
userBuf[offset] = 0
102+
}
103+
66104
offset++
67105
userBuf[offset] = (byte)((len(account) & 0xFF00) >> 8)
68106
offset++
@@ -124,6 +162,18 @@ func genUserBuf(account string, dwSdkappid int, dwAuthID uint32,
124162
offset++
125163
userBuf[offset] = (byte)(dwAccountType & 0x000000FF)
126164
offset++
165+
166+
if len(roomStr) > 0 {
167+
userBuf[offset] = (byte)((len(roomStr) & 0xFF00) >> 8)
168+
offset++
169+
userBuf[offset] = (byte)(len(roomStr) & 0x00FF)
170+
offset++
171+
172+
for ; offset < length; offset++ {
173+
userBuf[offset] = account[offset-(length-len(roomStr))]
174+
}
175+
}
176+
127177
return userBuf
128178
}
129179
func hmacsha256(sdkappid int, key string, identifier string, currTime int64, expire int, base64UserBuf *string) string {

0 commit comments

Comments
 (0)