Skip to content

Commit fa47f51

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

File tree

7 files changed

+121
-14
lines changed

7 files changed

+121
-14
lines changed

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>tls-sig-api-v2-java</name>
4+
<comment>Project tls-sig-api-v2-java created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=
2+
eclipse.preferences.version=1

src/main/java/com/tencentyun/TLSSigAPIv2.java

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,51 @@ public String genUserSig(String userid, long expire) {
5252
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
5353
* @param roomid - 房间号,用于指定该 userid 可以进入的房间号
5454
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
55-
* - 第 1 位:0000 0001 = 1,创建房间的权限
56-
* - 第 2 位:0000 0010 = 2,加入房间的权限
57-
* - 第 3 位:0000 0100 = 4,发送语音的权限
58-
* - 第 4 位:0000 1000 = 8,接收语音的权限
59-
* - 第 5 位:0001 0000 = 16,发送视频的权限
60-
* - 第 6 位:0010 0000 = 32,接收视频的权限
61-
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
62-
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
63-
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
64-
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
55+
* - 第 1 位:0000 0001 = 1,创建房间的权限
56+
* - 第 2 位:0000 0010 = 2,加入房间的权限
57+
* - 第 3 位:0000 0100 = 4,发送语音的权限
58+
* - 第 4 位:0000 1000 = 8,接收语音的权限
59+
* - 第 5 位:0001 0000 = 16,发送视频的权限
60+
* - 第 6 位:0010 0000 = 32,接收视频的权限
61+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
62+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
63+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
64+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
6565
* @return usersig - 生成带userbuf的签名
6666
*/
6767
public String genPrivateMapKey(String userid, long expire, long roomid, long privilegeMap) {
68-
byte[] userbuf = genUserBuf(userid, roomid, expire, privilegeMap, 0); //生成userbuf
68+
byte[] userbuf = genUserBuf(userid, roomid, expire, privilegeMap, 0, null); //生成userbuf
69+
return genUserSig(userid, expire, userbuf);
70+
}
71+
72+
/**
73+
* 【功能说明】
74+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
75+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
76+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
77+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
78+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】/【应用管理】/【应用信息】中打开“启动权限密钥”开关。
79+
* <p>
80+
* 【参数说明】
81+
*
82+
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
83+
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
84+
* @param roomstr - 字符串房间号,用于指定该 userid 可以进入的房间号
85+
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
86+
* - 第 1 位:0000 0001 = 1,创建房间的权限
87+
* - 第 2 位:0000 0010 = 2,加入房间的权限
88+
* - 第 3 位:0000 0100 = 4,发送语音的权限
89+
* - 第 4 位:0000 1000 = 8,接收语音的权限
90+
* - 第 5 位:0001 0000 = 16,发送视频的权限
91+
* - 第 6 位:0010 0000 = 32,接收视频的权限
92+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
93+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
94+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
95+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
96+
* @return usersig - 生成带userbuf的签名
97+
*/
98+
public String genPrivateMapKeyWithStringRoomID(String userid, long expire, String roomstr, long privilegeMap) {
99+
byte[] userbuf = genUserBuf(userid, 0, expire, privilegeMap, 0, roomstr); //生成userbuf
69100
return genUserSig(userid, expire, userbuf);
70101
}
71102

@@ -125,7 +156,7 @@ private String genUserSig(String userid, long expire, byte[] userbuf) {
125156
}
126157

127158
public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
128-
long dwPrivilegeMap, long dwAccountType) {
159+
long dwPrivilegeMap, long dwAccountType, String RoomStr) {
129160
//视频校验位需要用到的字段,按照网络字节序放入buf中
130161
/*
131162
cVer unsigned char/1 版本号,填0
@@ -138,11 +169,18 @@ public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
138169
dwAccountType unsigned int/4 第三方帐号类型
139170
*/
140171
int accountLength = account.length();
172+
int roomStrLength = RoomStr.length();
141173
int offset = 0;
142-
byte[] userbuf = new byte[1 + 2 + accountLength + 4 + 4 + 4 + 4 + 4];
174+
int bufLength = 1 + 2 + accountLength + 20 ;
175+
byte[] userbuf = new byte[bufLength];
143176

144177
//cVer
145-
userbuf[offset++] = 0;
178+
if (roomStrLength > 0) {
179+
bufLength = bufLength + 2 + roomStrLength;
180+
userbuf[offset++] = 1;
181+
} else {
182+
userbuf[offset++] = 0;
183+
}
146184

147185
//wAccountLen
148186
userbuf[offset++] = (byte) ((accountLength & 0xFF00) >> 8);
@@ -185,6 +223,17 @@ public byte[] genUserBuf(String account, long dwAuthID, long dwExpTime,
185223
userbuf[offset++] = (byte) ((dwAccountType & 0x0000FF00) >> 8);
186224
userbuf[offset++] = (byte) (dwAccountType & 0x000000FF);
187225

226+
227+
if (roomStrLength > 0) {
228+
//roomStrLen
229+
userbuf[offset++] = (byte) ((roomStrLength & 0xFF00) >> 8);
230+
userbuf[offset++] = (byte) (roomStrLength & 0x00FF);
231+
232+
//roomStr
233+
for (; offset < bufLength; ++offset) {
234+
userbuf[offset] = (byte) RoomStr.charAt(offset - (bufLength - roomStrLength));
235+
}
236+
}
188237
return userbuf;
189238
}
190239
}

src/main/main.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

src/test/com/com.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/tencentyun" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Untitled {
2+
public static void main(String []args) {
3+
System.out.println("Hello World");
4+
}
5+
}

src/test/test.iml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/com/tencentyun" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="junit" level="project" />
11+
</component>
12+
</module>

0 commit comments

Comments
 (0)