Skip to content

Commit 6baf3b1

Browse files
authored
Merge pull request #1 from yutingzeng/master
feat: 增加构造 userbuf
2 parents c8fca88 + 9cf8996 commit 6baf3b1

File tree

2 files changed

+189
-24
lines changed

2 files changed

+189
-24
lines changed

TLSSigAPITest.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import (
77

88
const (
99
sdkappid = 1400000000
10-
key = "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e"
10+
key = "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e"
1111
)
1212

13-
func main() {
14-
sig, err := tencentyun.GenSig(sdkappid, key, "xiaojun", 86400*180)
13+
func main() {
14+
sig, err := tencentyun.GenUserSig(sdkappid, key, "xiaojun", 86400*180)
1515
if err != nil {
1616
fmt.Println(err.Error())
1717
} else {
1818
fmt.Println(sig)
1919
}
20-
sig, err = tencentyun.GenSigWithUserBuf(sdkappid, key, "xiaojun", 86400*180, []byte("abc"))
20+
sig, err = tencentyun.GenPrivateMapKey(sdkappid, key, "xiaojun", 86400*180, 10000, 255)
21+
if err != nil {
22+
fmt.Println(err.Error())
23+
} else {
24+
fmt.Println(sig)
25+
}
26+
sig, err = tencentyun.GenPrivateMapKeyWithStringRoomID(sdkappid, key, "xiaojun", 86400*180, "1000000040", 255)
2127
if err != nil {
2228
fmt.Println(err.Error())
2329
} else {

tencentyun/TLSSigAPI.go

Lines changed: 179 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,183 @@ import (
1111
"time"
1212
)
1313

14+
/**
15+
*【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
16+
*
17+
*【参数说明】
18+
* sdkappid - 应用id
19+
* key - 计算 usersig 用的加密密钥,控制台可获取
20+
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
21+
* expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
22+
*/
23+
24+
func GenUserSig(sdkappid int, key string, userid string, expire int) (string, error) {
25+
return genSig(sdkappid, key, userid, expire, nil)
26+
}
27+
28+
/**
29+
*【功能说明】
30+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
31+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
32+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
33+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
34+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
35+
*
36+
*【参数说明】
37+
* sdkappid - 应用id。
38+
* key - 计算 usersig 用的加密密钥,控制台可获取。
39+
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
40+
* expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
41+
* roomid - 房间号,用于指定该 userid 可以进入的房间号
42+
* privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
43+
* - 第 1 位:0000 0001 = 1,创建房间的权限
44+
* - 第 2 位:0000 0010 = 2,加入房间的权限
45+
* - 第 3 位:0000 0100 = 4,发送语音的权限
46+
* - 第 4 位:0000 1000 = 8,接收语音的权限
47+
* - 第 5 位:0001 0000 = 16,发送视频的权限
48+
* - 第 6 位:0010 0000 = 32,接收视频的权限
49+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
50+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
51+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
52+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
53+
*/
54+
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, "")
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)
87+
return genSig(sdkappid, key, userid, expire, userbuf)
88+
}
89+
func genUserBuf(account string, dwSdkappid int, dwAuthID uint32,
90+
dwExpTime int, dwPrivilegeMap uint32, dwAccountType uint32, roomStr string) []byte {
91+
92+
offset := 0
93+
length := 1 + 2 + len(account) + 20 + len(roomStr)
94+
if len(roomStr) > 0 {
95+
length = length + 2
96+
}
97+
98+
userBuf := make([]byte, length)
99+
100+
//ver
101+
if len(roomStr) > 0 {
102+
userBuf[offset] = 1
103+
} else {
104+
userBuf[offset] = 0
105+
}
106+
107+
offset++
108+
userBuf[offset] = (byte)((len(account) & 0xFF00) >> 8)
109+
offset++
110+
userBuf[offset] = (byte)(len(account) & 0x00FF)
111+
offset++
112+
113+
for ; offset < len(account)+3; offset++ {
114+
userBuf[offset] = account[offset-3]
115+
}
116+
117+
//dwSdkAppid
118+
userBuf[offset] = (byte)((dwSdkappid & 0xFF000000) >> 24)
119+
offset++
120+
userBuf[offset] = (byte)((dwSdkappid & 0x00FF0000) >> 16)
121+
offset++
122+
userBuf[offset] = (byte)((dwSdkappid & 0x0000FF00) >> 8)
123+
offset++
124+
userBuf[offset] = (byte)(dwSdkappid & 0x000000FF)
125+
offset++
126+
127+
//dwAuthId
128+
userBuf[offset] = (byte)((dwAuthID & 0xFF000000) >> 24)
129+
offset++
130+
userBuf[offset] = (byte)((dwAuthID & 0x00FF0000) >> 16)
131+
offset++
132+
userBuf[offset] = (byte)((dwAuthID & 0x0000FF00) >> 8)
133+
offset++
134+
userBuf[offset] = (byte)(dwAuthID & 0x000000FF)
135+
offset++
136+
137+
//dwExpTime now+300;
138+
currTime := time.Now().Unix()
139+
var expire = currTime + int64(dwExpTime)
140+
userBuf[offset] = (byte)((expire & 0xFF000000) >> 24)
141+
offset++
142+
userBuf[offset] = (byte)((expire & 0x00FF0000) >> 16)
143+
offset++
144+
userBuf[offset] = (byte)((expire & 0x0000FF00) >> 8)
145+
offset++
146+
userBuf[offset] = (byte)(expire & 0x000000FF)
147+
offset++
148+
149+
//dwPrivilegeMap
150+
userBuf[offset] = (byte)((dwPrivilegeMap & 0xFF000000) >> 24)
151+
offset++
152+
userBuf[offset] = (byte)((dwPrivilegeMap & 0x00FF0000) >> 16)
153+
offset++
154+
userBuf[offset] = (byte)((dwPrivilegeMap & 0x0000FF00) >> 8)
155+
offset++
156+
userBuf[offset] = (byte)(dwPrivilegeMap & 0x000000FF)
157+
offset++
158+
159+
//dwAccountType
160+
userBuf[offset] = (byte)((dwAccountType & 0xFF000000) >> 24)
161+
offset++
162+
userBuf[offset] = (byte)((dwAccountType & 0x00FF0000) >> 16)
163+
offset++
164+
userBuf[offset] = (byte)((dwAccountType & 0x0000FF00) >> 8)
165+
offset++
166+
userBuf[offset] = (byte)(dwAccountType & 0x000000FF)
167+
offset++
168+
169+
if len(roomStr) > 0 {
170+
userBuf[offset] = (byte)((len(roomStr) & 0xFF00) >> 8)
171+
offset++
172+
userBuf[offset] = (byte)(len(roomStr) & 0x00FF)
173+
offset++
174+
175+
for ; offset < length; offset++ {
176+
userBuf[offset] = roomStr[offset-(length-len(roomStr))]
177+
}
178+
}
179+
180+
return userBuf
181+
}
14182
func hmacsha256(sdkappid int, key string, identifier string, currTime int64, expire int, base64UserBuf *string) string {
15183
var contentToBeSigned string
16184
contentToBeSigned = "TLS.identifier:" + identifier + "\n"
17185
contentToBeSigned += "TLS.sdkappid:" + strconv.Itoa(sdkappid) + "\n"
18186
contentToBeSigned += "TLS.time:" + strconv.FormatInt(currTime, 10) + "\n"
19187
contentToBeSigned += "TLS.expire:" + strconv.Itoa(expire) + "\n"
20-
if nil != base64UserBuf {
21-
contentToBeSigned += "TLS.userbuf:" + *base64UserBuf + "\n"
22-
}
188+
if nil != base64UserBuf {
189+
contentToBeSigned += "TLS.userbuf:" + *base64UserBuf + "\n"
190+
}
23191

24192
h := hmac.New(sha256.New, []byte(key))
25193
h.Write([]byte(contentToBeSigned))
@@ -35,14 +203,14 @@ func genSig(sdkappid int, key string, identifier string, expire int, userbuf []b
35203
sigDoc["TLS.sdkappid"] = sdkappid
36204
sigDoc["TLS.expire"] = expire
37205
sigDoc["TLS.time"] = currTime
38-
var base64UserBuf string
39-
if nil != userbuf {
40-
base64UserBuf = base64.StdEncoding.EncodeToString(userbuf)
41-
sigDoc["TLS.userbuf"] = base64UserBuf
42-
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, &base64UserBuf)
43-
} else {
44-
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, nil)
45-
}
206+
var base64UserBuf string
207+
if nil != userbuf {
208+
base64UserBuf = base64.StdEncoding.EncodeToString(userbuf)
209+
sigDoc["TLS.userbuf"] = base64UserBuf
210+
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, &base64UserBuf)
211+
} else {
212+
sigDoc["TLS.sig"] = hmacsha256(sdkappid, key, identifier, currTime, expire, nil)
213+
}
46214

47215
data, err := json.Marshal(sigDoc)
48216
if err != nil {
@@ -55,12 +223,3 @@ func genSig(sdkappid int, key string, identifier string, expire int, userbuf []b
55223
w.Close()
56224
return base64urlEncode(b.Bytes()), nil
57225
}
58-
59-
func GenSig(sdkappid int, key string, identifier string, expire int) (string, error) {
60-
return genSig(sdkappid, key, identifier, expire, nil)
61-
}
62-
63-
func GenSigWithUserBuf(sdkappid int, key string, identifier string, expire int, userbuf []byte) (string, error) {
64-
return genSig(sdkappid, key, identifier, expire, userbuf)
65-
}
66-

0 commit comments

Comments
 (0)