@@ -20,50 +20,70 @@ base64url.escape = function escape(str) {
2020 . replace ( / = / g, '_' ) ;
2121} ;
2222
23- base64url . encode = function encode ( str ) {
23+ base64url . encode = function encode ( str ) {
2424 return this . escape ( newBuffer ( str ) . toString ( 'base64' ) ) ;
2525} ;
2626
27- base64url . decode = function decode ( str ) {
27+ base64url . decode = function decode ( str ) {
2828 return newBuffer ( this . unescape ( str ) , 'base64' ) . toString ( ) ;
2929} ;
3030
3131function base64encode ( str ) {
3232 return newBuffer ( str ) . toString ( 'base64' )
3333}
34+
3435function base64decode ( str ) {
3536 return newBuffer ( str , 'base64' ) . toString ( )
3637}
3738
38- var Api = function ( sdkappid , key ) {
39- this . sdkappid = sdkappid ;
39+ var Api = function ( sdkappid , key ) {
40+ this . sdkappid = sdkappid ;
4041 this . key = key ;
4142} ;
42- /**用于生成实时音视频(TRTC)业务进房权限加密串,具体用途用法参考TRTC文档:https://cloud.tencent.com/document/product/647/32240
43- * TRTC业务进房权限加密串需使用用户定义的userbuf
44- * @brief 生成 userbuf
45- * @param account 用户名
46- * @param dwSdkappid sdkappid
47- * @param dwAuthID 数字房间号
48- * @param dwExpTime 过期时间:该权限加密串的过期时间,建议300秒,实际过期时间:now+dwExpTime
49- * @param dwPrivilegeMap 用户权限,255表示所有权限
50- * @param dwAccountType 用户类型,默认为0
51- * @return userbuf {string} 返回的userbuf
52- */
43+ /**
44+ * 通过传入参数生成 base64 的 hmac 值
45+ * @param identifier
46+ * @param currTime
47+ * @param expire
48+ * @returns {string }
49+ * @private
50+ */
51+ Api . prototype . _hmacsha256 = function ( identifier , currTime , expire , base64UserBuf ) {
52+ var contentToBeSigned = "TLS.identifier:" + identifier + "\n" ;
53+ contentToBeSigned += "TLS.sdkappid:" + this . sdkappid + "\n" ;
54+ contentToBeSigned += "TLS.time:" + currTime + "\n" ;
55+ contentToBeSigned += "TLS.expire:" + expire + "\n" ;
56+ if ( null != base64UserBuf ) {
57+ contentToBeSigned += "TLS.userbuf:" + base64UserBuf + "\n" ;
58+ }
59+ const hmac = crypto . createHmac ( "sha256" , this . key ) ;
60+ return hmac . update ( contentToBeSigned ) . digest ( 'base64' ) ;
61+ } ;
62+ /**
63+ * TRTC业务进房权限加密串需使用用户定义的userbuf
64+ * @brief 生成 userbuf
65+ * @param account 用户名
66+ * @param dwSdkappid sdkappid
67+ * @param dwAuthID 数字房间号
68+ * @param dwExpTime 过期时间:该权限加密串的过期时间,建议300秒,实际过期时间:now+dwExpTime
69+ * @param dwPrivilegeMap 用户权限,255表示所有权限
70+ * @param dwAccountType 用户类型,默认为0
71+ * @return userbuf {string} 返回的userbuf
72+ */
5373Api . prototype . _genUserbuf = function ( account , dwAuthID , dwExpTime ,
54- dwPrivilegeMap , dwAccountType ) {
74+ dwPrivilegeMap , dwAccountType ) {
5575
5676 let accountLength = account . length ;
5777 let offset = 0 ;
58- let userBuf = new Buffer . alloc ( 1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4 ) ;
78+ let userBuf = new Buffer . alloc ( 1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4 ) ;
5979
6080 //cVer
6181 userBuf [ offset ++ ] = 0 ;
6282
6383 //wAccountLen
6484 userBuf [ offset ++ ] = ( accountLength & 0xFF00 ) >> 8 ;
6585 userBuf [ offset ++ ] = accountLength & 0x00FF ;
66-
86+
6787 //buffAccount
6888 for ( ; offset < 3 + accountLength ; ++ offset ) {
6989 userBuf [ offset ] = account . charCodeAt ( offset - 3 ) ;
@@ -74,26 +94,26 @@ Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
7494 userBuf [ offset ++ ] = ( this . sdkappid & 0x00FF0000 ) >> 16 ;
7595 userBuf [ offset ++ ] = ( this . sdkappid & 0x0000FF00 ) >> 8 ;
7696 userBuf [ offset ++ ] = this . sdkappid & 0x000000FF ;
77-
97+
7898 //dwAuthId
7999 userBuf [ offset ++ ] = ( dwAuthID & 0xFF000000 ) >> 24 ;
80100 userBuf [ offset ++ ] = ( dwAuthID & 0x00FF0000 ) >> 16 ;
81101 userBuf [ offset ++ ] = ( dwAuthID & 0x0000FF00 ) >> 8 ;
82102 userBuf [ offset ++ ] = dwAuthID & 0x000000FF ;
83-
103+
84104 //过期时间:dwExpTime+now
85- let expire = Date . now ( ) / 1000 + dwExpTime ;
105+ let expire = Date . now ( ) / 1000 + dwExpTime ;
86106 userBuf [ offset ++ ] = ( expire & 0xFF000000 ) >> 24 ;
87107 userBuf [ offset ++ ] = ( expire & 0x00FF0000 ) >> 16 ;
88108 userBuf [ offset ++ ] = ( expire & 0x0000FF00 ) >> 8 ;
89109 userBuf [ offset ++ ] = expire & 0x000000FF ;
90-
110+
91111 //dwPrivilegeMap
92112 userBuf [ offset ++ ] = ( dwPrivilegeMap & 0xFF000000 ) >> 24 ;
93113 userBuf [ offset ++ ] = ( dwPrivilegeMap & 0x00FF0000 ) >> 16 ;
94114 userBuf [ offset ++ ] = ( dwPrivilegeMap & 0x0000FF00 ) >> 8 ;
95115 userBuf [ offset ++ ] = dwPrivilegeMap & 0x000000FF ;
96-
116+
97117 //dwAccountType
98118 userBuf [ offset ++ ] = ( dwAccountType & 0xFF000000 ) >> 24 ;
99119 userBuf [ offset ++ ] = ( dwAccountType & 0x00FF0000 ) >> 16 ;
@@ -102,39 +122,17 @@ Api.prototype._genUserbuf = function (account, dwAuthID, dwExpTime,
102122
103123 return userBuf ;
104124}
105- /**
106- * 通过传入参数生成 base64 的 hmac 值
107- * @param identifier
108- * @param currTime
109- * @param expire
110- * @returns {string }
111- * @private
112- */
113- Api . prototype . _hmacsha256 = function ( identifier , currTime , expire , base64UserBuf ) {
114- var contentToBeSigned = "TLS.identifier:" + identifier + "\n" ;
115- contentToBeSigned += "TLS.sdkappid:" + this . sdkappid + "\n" ;
116- contentToBeSigned += "TLS.time:" + currTime + "\n" ;
117- contentToBeSigned += "TLS.expire:" + expire + "\n" ;
118- if ( null != base64UserBuf ) {
119- contentToBeSigned += "TLS.userbuf:" + base64UserBuf + "\n" ;
120- }
121- const hmac = crypto . createHmac ( "sha256" , this . key ) ;
122- return hmac . update ( contentToBeSigned ) . digest ( 'base64' ) ;
123- } ;
124-
125125/**
126126 *【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
127127 *
128128 *【参数说明】
129- * userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
130- * expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
129+ * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
130+ * @param expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
131131 */
132- Api . prototype . genUserSig = function ( userid , expire ) {
133- return this . genPrivateMapKey ( userid , expire , null , null ) ;
132+ Api . prototype . genUserSig = function ( userid , expire ) {
133+ return this . genPrivateMapKey ( userid , expire , null , null ) ;
134134} ;
135135
136-
137-
138136/**
139137 *【功能说明】
140138 * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
@@ -144,10 +142,10 @@ Api.prototype.genUserSig = function(userid, expire){
144142 * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
145143 *
146144 *【参数说明】
147- * userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
148- * expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
149- * roomid - 房间号,用于指定该 userid 可以进入的房间号
150- * privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
145+ * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
146+ * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
147+ * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
148+ * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
151149 * - 第 1 位:0000 0001 = 1,创建房间的权限
152150 * - 第 2 位:0000 0010 = 2,加入房间的权限
153151 * - 第 3 位:0000 0100 = 4,发送语音的权限
@@ -159,17 +157,17 @@ Api.prototype.genUserSig = function(userid, expire){
159157 * - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
160158 * - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
161159 */
162- Api . prototype . genPrivateMapKey = function ( userid , expire , roomid , privilegeMap ) {
160+ Api . prototype . genPrivateMapKey = function ( userid , expire , roomid , privilegeMap ) {
163161
164- if ( null == roomid || null == privilegeMap )
165- var userBuf = null ;
162+ if ( null == roomid || null == privilegeMap )
163+ var userBuf = null ;
166164 else
167- var userBuf = this . _genUserbuf ( userid , roomid , expire , privilegeMap , 0 ) ;
168- var currTime = Math . floor ( Date . now ( ) / 1000 ) ;
165+ var userBuf = this . _genUserbuf ( userid , roomid , expire , privilegeMap , 0 ) ;
166+ var currTime = Math . floor ( Date . now ( ) / 1000 ) ;
169167
170168 var sigDoc = {
171169 'TLS.ver' : "2.0" ,
172- 'TLS.identifier' : "" + userid ,
170+ 'TLS.identifier' : "" + userid ,
173171 'TLS.sdkappid' : Number ( this . sdkappid ) ,
174172 'TLS.time' : Number ( currTime ) ,
175173 'TLS.expire' : Number ( expire )
@@ -189,4 +187,4 @@ Api.prototype.genPrivateMapKey = function(userid, expire, roomid,privilegeMap){
189187 return base64url . escape ( compressed ) ;
190188} ;
191189
192- exports . Api = Api ;
190+ exports . Api = Api ;
0 commit comments