Skip to content

Commit b1617c9

Browse files
author
yutingzeng
committed
增加使用字符串房间号生成privateMapKey
1 parent 4e8c102 commit b1617c9

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/TLSSigAPIv2.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,37 @@ public function genUserSig( $userid, $expire = 86400*180 ) {
5151
*/
5252

5353
public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
54-
$userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0 );
54+
$userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
55+
echo $userbuf . '\n';
56+
return $this->__genSig( $userid, $expire, $userbuf, true );
57+
}
58+
/**
59+
*【功能说明】
60+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
61+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
62+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
63+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
64+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
65+
*
66+
*【参数说明】
67+
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
68+
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
69+
* @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
70+
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
71+
* - 第 1 位:0000 0001 = 1,创建房间的权限
72+
* - 第 2 位:0000 0010 = 2,加入房间的权限
73+
* - 第 3 位:0000 0100 = 4,发送语音的权限
74+
* - 第 4 位:0000 1000 = 8,接收语音的权限
75+
* - 第 5 位:0001 0000 = 16,发送视频的权限
76+
* - 第 6 位:0010 0000 = 32,接收视频的权限
77+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
78+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
79+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
80+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
81+
*/
82+
83+
public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
84+
$userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
5585
echo $userbuf . '\n';
5686
return $this->__genSig( $userid, $expire, $userbuf, true );
5787
}
@@ -104,12 +134,18 @@ private function base64_url_decode( $base64 ) {
104134
* @param dwExpTime 过期时间:该权限加密串的过期时间. 过期时间 = now+dwExpTime
105135
* @param dwPrivilegeMap 用户权限,255表示所有权限
106136
* @param dwAccountType 用户类型, 默认为0
137+
* @param roomStr 字符串房间号
107138
* @return userbuf string 返回的userbuf
108139
*/
109140

110-
private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType ) {
111-
$userbuf = pack( 'C1', '0' );
141+
private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {
142+
112143
//cVer unsigned char/1 版本号,填0
144+
if($roomStr == '')
145+
$userbuf = pack( 'C1', '0' );
146+
else
147+
$userbuf = pack( 'C1', '1' );
148+
113149
$userbuf .= pack( 'n', strlen( $account ) );
114150
//wAccountLen unsigned short /2 第三方自己的帐号长度
115151
$userbuf .= pack( 'a'.strlen( $account ), $account );
@@ -125,6 +161,13 @@ private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap,
125161
//dwPrivilegeMap unsigned int/4 权限位
126162
$userbuf .= pack( 'N', $dwAccountType );
127163
//dwAccountType unsigned int/4
164+
if($roomStr != '')
165+
{
166+
$userbuf .= pack( 'n', strlen( $roomStr ) );
167+
//roomStrLen unsigned short /2 字符串房间号长度
168+
$userbuf .= pack( 'a'.strlen( $roomStr ), $roomStr );
169+
//roomStr roomStrLen 字符串房间号
170+
}
128171
return $userbuf;
129172
}
130173
/**

test/test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
}
2424
$sig = $api->genPrivateMapKey( 'xiaojun', 86400*180, 10000, 255 );
2525
echo $sig . '\n';
26+
$sig = $api->genPrivateMapKeyWithStringRoomID( 'xiaojun', 86400*180, "aaa", 255 );
27+
echo $sig . '\n';
2628
$init_time = 0;
2729
$expire = 0;
2830
$err_msg = '';

0 commit comments

Comments
 (0)