Skip to content

Commit eda76e9

Browse files
author
yutingzeng
committed
增加使用字符串房间号生成privateMapKey
1 parent 6a78ce1 commit eda76e9

File tree

2 files changed

+83
-30
lines changed

2 files changed

+83
-30
lines changed

TLSSigAPITest.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ var api = new TLSSigAPIv2.Api(1400000000, "5bd2850fff3ecb11d7c805251c51ee463a257
44
var sig = api.genUserSig("xiaojun", 86400 * 180);
55
console.log("sig " + sig);
66
var sig = api.genPrivateMapKey("xiaojun", 86400 * 180, 10000, 255);
7-
console.log("sig with userbuf " + sig);
7+
console.log("sig with userbuf " + sig);
8+
var sig = api.genPrivateMapKeyWithStringRoomID("xiaojun", 86400 * 180, "1000000040", 255);
9+
console.log("sig with userbuf string room " + sig);

TLSSigAPIv2.js

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,28 @@ Api.prototype._hmacsha256 = function (identifier, currTime, expire, base64UserBu
6868
* @param dwExpTime 过期时间:该权限加密串的过期时间,建议300秒,实际过期时间:now+dwExpTime
6969
* @param dwPrivilegeMap 用户权限,255表示所有权限
7070
* @param dwAccountType 用户类型,默认为0
71+
* @param roomstr 字符串房间号
7172
* @return userbuf {string} 返回的userbuf
7273
*/
7374
Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
74-
dwPrivilegeMap, dwAccountType) {
75+
dwPrivilegeMap, dwAccountType, roomstr) {
7576

7677
let accountLength = account.length;
78+
let roomstrlength = 0;
79+
let length = 1 + 2 + accountLength + 20 ;
80+
if (null != roomstr)
81+
{
82+
roomstrlength = roomstr.length;
83+
length = length + 2 + roomstrlength;
84+
}
7785
let offset = 0;
78-
let userBuf = new Buffer.alloc(1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4);
86+
let userBuf = new Buffer.alloc(length);
7987

8088
//cVer
81-
userBuf[offset++] = 0;
89+
if (null != roomstr)
90+
userBuf[offset++] = 1;
91+
else
92+
userBuf[offset++] = 0;
8293

8394
//wAccountLen
8495
userBuf[offset++] = (accountLength & 0xFF00) >> 8;
@@ -120,8 +131,43 @@ Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
120131
userBuf[offset++] = (dwAccountType & 0x0000FF00) >> 8;
121132
userBuf[offset++] = dwAccountType & 0x000000FF;
122133

134+
if (null != roomstr) {
135+
//roomstrlength
136+
userBuf[offset++] = (roomstr.length & 0xFF00) >> 8;
137+
userBuf[offset++] = roomstr.length & 0x00FF;
138+
139+
//roomstr
140+
for (; offset < length; ++offset) {
141+
userBuf[offset] = account.charCodeAt(offset - (length - roomstr.length));
142+
}
143+
}
144+
123145
return userBuf;
124146
}
147+
Api.prototype.genSig = function (userid, expire, userBuf) {
148+
var currTime = Math.floor(Date.now() / 1000);
149+
150+
var sigDoc = {
151+
'TLS.ver': "2.0",
152+
'TLS.identifier': "" + userid,
153+
'TLS.sdkappid': Number(this.sdkappid),
154+
'TLS.time': Number(currTime),
155+
'TLS.expire': Number(expire)
156+
};
157+
158+
var sig = '';
159+
if (null != userBuf) {
160+
var base64UserBuf = base64encode(userBuf);
161+
sigDoc['TLS.userbuf'] = base64UserBuf;
162+
sig = this._hmacsha256(userid, currTime, expire, base64UserBuf);
163+
} else {
164+
sig = this._hmacsha256(userid, currTime, expire, null);
165+
}
166+
sigDoc['TLS.sig'] = sig;
167+
168+
var compressed = zlib.deflateSync(newBuffer(JSON.stringify(sigDoc))).toString('base64');
169+
return base64url.escape(compressed);
170+
}
125171
/**
126172
*【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
127173
*
@@ -130,7 +176,7 @@ Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
130176
* @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
131177
*/
132178
Api.prototype.genUserSig = function (userid, expire) {
133-
return this.genPrivateMapKey(userid, expire, null, null);
179+
return this.genSig(userid, expire, null);
134180
};
135181

136182
/**
@@ -158,33 +204,38 @@ Api.prototype.genUserSig = function (userid, expire) {
158204
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
159205
*/
160206
Api.prototype.genPrivateMapKey = function (userid, expire, roomid, privilegeMap) {
207+
var userBuf = this._genUserbuf(userid, roomid, expire, privilegeMap, 0, null);
208+
return this.genSig(userid, expire, userBuf);
161209

162-
if (null == roomid || null == privilegeMap)
163-
var userBuf = null;
164-
else
165-
var userBuf = this._genUserbuf(userid, roomid, expire, privilegeMap, 0);
166-
var currTime = Math.floor(Date.now() / 1000);
167-
168-
var sigDoc = {
169-
'TLS.ver': "2.0",
170-
'TLS.identifier': "" + userid,
171-
'TLS.sdkappid': Number(this.sdkappid),
172-
'TLS.time': Number(currTime),
173-
'TLS.expire': Number(expire)
174-
};
175-
176-
var sig = '';
177-
if (null != userBuf) {
178-
var base64UserBuf = base64encode(userBuf);
179-
sigDoc['TLS.userbuf'] = base64UserBuf;
180-
sig = this._hmacsha256(userid, currTime, expire, base64UserBuf);
181-
} else {
182-
sig = this._hmacsha256(userid, currTime, expire, null);
183-
}
184-
sigDoc['TLS.sig'] = sig;
210+
};
211+
/**
212+
*【功能说明】
213+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
214+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
215+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
216+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
217+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
218+
*
219+
*【参数说明】
220+
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
221+
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
222+
* @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
223+
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
224+
* - 第 1 位:0000 0001 = 1,创建房间的权限
225+
* - 第 2 位:0000 0010 = 2,加入房间的权限
226+
* - 第 3 位:0000 0100 = 4,发送语音的权限
227+
* - 第 4 位:0000 1000 = 8,接收语音的权限
228+
* - 第 5 位:0001 0000 = 16,发送视频的权限
229+
* - 第 6 位:0010 0000 = 32,接收视频的权限
230+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
231+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
232+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该房间内的所有功能权限。
233+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
234+
*/
235+
Api.prototype.genPrivateMapKeyWithStringRoomID = function (userid, expire, roomstr, privilegeMap) {
236+
var userBuf = this._genUserbuf(userid, 0, expire, privilegeMap, 0, roomstr);
237+
return this.genSig(userid, expire, userBuf);
185238

186-
var compressed = zlib.deflateSync(newBuffer(JSON.stringify(sigDoc))).toString('base64');
187-
return base64url.escape(compressed);
188239
};
189240

190241
exports.Api = Api;

0 commit comments

Comments
 (0)