Skip to content

Commit 51091b4

Browse files
author
yutingzeng
committed
修改函数名和注释
1 parent 5370ba8 commit 51091b4

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

TLSSigAPITest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var TLSSigAPIv2 = require('./TLSSigAPIv2');
22

33
var api = new TLSSigAPIv2.Api(1400000000, "5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e");
4-
var sig = api.genSig("xiaojun", 86400*180);
4+
var sig = api.genUserSig("xiaojun", 86400*180);
55
console.log("sig " + sig);
6-
var sig = api.genSigWithUserbuf("xiaojun", 86400*180, 10000,255);
6+
var sig = api.genPrivateMapKey("xiaojun", 86400*180, 10000,255);
77
console.log("sig with userbuf " + sig);

TLSSigAPIv2.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
5555

5656
let accountLength = account.length;
5757
let offset = 0;
58-
let userBuf = new Buffer(1+2+accountLength+4+4+4+4+4);
58+
let userBuf = new Buffer.alloc(1+2+accountLength+4+4+4+4+4);
5959

6060
//cVer
6161
userBuf[offset++] = 0;
@@ -123,35 +123,53 @@ Api.prototype._hmacsha256 = function(identifier, currTime, expire, base64UserBuf
123123
};
124124

125125
/**
126-
* 生成 usersig
127-
* @param string $identifier 用户名
128-
* @return string 生成的失败时为false
126+
*【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
127+
*
128+
*【参数说明】
129+
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
130+
* expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
129131
*/
130-
/**
131-
* 生成 usersig
132-
* @param identifier 用户账号
133-
* @param expire 有效期,单位秒
134-
* @returns {string} 返回的 sig 值
135-
*/
136-
Api.prototype.genSig = function(identifier, expire){
137-
return this.genSigWithUserbuf(identifier, expire, null);
132+
Api.prototype.genUserSig = function(userid, expire){
133+
return this.genPrivateMapKey(userid, expire, null,null);
138134
};
139135

136+
137+
140138
/**
141-
* 生成带 userbuf 的 usersig
142-
* @param identifier 用户账号
143-
* @param expire 有效期,单位秒
144-
* @param userBuf 用户数据
145-
* @returns {string} 返回的 sig 值
139+
*【功能说明】
140+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
141+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
142+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
143+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
144+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
145+
*
146+
*【参数说明】
147+
* userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
148+
* expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
149+
* roomid - 房间号,用于指定该 userid 可以进入的房间号
150+
* privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
151+
* - 第 1 位:0000 0001 = 1,创建房间的权限
152+
* - 第 2 位:0000 0010 = 2,加入房间的权限
153+
* - 第 3 位:0000 0100 = 4,发送语音的权限
154+
* - 第 4 位:0000 1000 = 8,接收语音的权限
155+
* - 第 5 位:0001 0000 = 16,发送视频的权限
156+
* - 第 6 位:0010 0000 = 32,接收视频的权限
157+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
158+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
159+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
160+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
146161
*/
147-
Api.prototype.genSigWithUserbuf = function(identifier, expire, roomnum,privilege,){
162+
Api.prototype.genPrivateMapKey = function(userid, expire, roomid,privilegeMap){
148163

149-
var userBuf = this._genUserbuf(identifier,roomnum,expire,privilege,0);
164+
if(null == roomid || null == privilegeMap )
165+
var userBuf = null ;
166+
else
167+
var userBuf = this._genUserbuf(userid,roomid,expire,privilegeMap,0);
150168
var currTime = Math.floor(Date.now()/1000);
151169

152170
var sigDoc = {
153171
'TLS.ver': "2.0",
154-
'TLS.identifier': ""+identifier,
172+
'TLS.identifier': ""+userid,
155173
'TLS.sdkappid': Number(this.sdkappid),
156174
'TLS.time': Number(currTime),
157175
'TLS.expire': Number(expire)
@@ -161,9 +179,9 @@ Api.prototype.genSigWithUserbuf = function(identifier, expire, roomnum,privilege
161179
if (null != userBuf) {
162180
var base64UserBuf = base64encode(userBuf);
163181
sigDoc['TLS.userbuf'] = base64UserBuf;
164-
sig = this._hmacsha256(identifier, currTime, expire, base64UserBuf);
182+
sig = this._hmacsha256(userid, currTime, expire, base64UserBuf);
165183
} else {
166-
sig = this._hmacsha256(identifier, currTime, expire, null);
184+
sig = this._hmacsha256(userid, currTime, expire, null);
167185
}
168186
sigDoc['TLS.sig'] = sig;
169187

0 commit comments

Comments
 (0)