Skip to content

Commit e05f071

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

File tree

3 files changed

+157
-32
lines changed

3 files changed

+157
-32
lines changed

src/tls_sig_api_v2.cpp

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -247,50 +247,39 @@ static std::string hmacsha256(uint32_t sdkappid, const std::string &identifier,
247247
TLS_API int genUserSig(uint32_t sdkappid, const std::string &userid, const std::string &key,
248248
int expire, std::string &usersig, std::string &errmsg)
249249
{
250-
uint64_t curr_time = time(NULL);
251-
std::string base64_raw_sig = hmacsha256(sdkappid, userid, curr_time, expire, key);
252-
rapidjson::Document sig_doc;
253-
sig_doc.SetObject();
254-
sig_doc.AddMember("TLS.ver", "2.0", sig_doc.GetAllocator());
255-
sig_doc.AddMember("TLS.sdkappid", sdkappid, sig_doc.GetAllocator());
256-
sig_doc.AddMember("TLS.identifier", userid, sig_doc.GetAllocator());
257-
sig_doc.AddMember("TLS.time", curr_time, sig_doc.GetAllocator());
258-
sig_doc.AddMember("TLS.expire", expire, sig_doc.GetAllocator());
259-
sig_doc.AddMember("TLS.sig", base64_raw_sig, sig_doc.GetAllocator());
260-
return json2sig(sig_doc, usersig, errmsg);
250+
return genSig(sdkappid,userid,key,"",expire,usersig,errmsg);
261251
}
262252

263253
// 生成带 userbuf 的签名
264254
TLS_API int genPrivateMapKey(uint32_t sdkappid, const std::string &userid, const std::string &key, uint32_t roomid,
265255
int expire, int privilegeMap, std::string &usersig, std::string &errmsg)
266256
{
267-
uint64_t currTime = time(NULL);
268-
std::string userbuf = gen_userbuf(userid, sdkappid, roomid, expire, privilegeMap, 0);
269-
std::string base64UserBuf;
270-
base64_encode(userbuf.data(), userbuf.length(), base64UserBuf);
271-
std::string base64RawSig = hmacsha256(
272-
sdkappid, userid, currTime, expire, key, base64UserBuf);
273-
rapidjson::Document sig_doc;
274-
sig_doc.SetObject();
275-
sig_doc.AddMember("TLS.ver", "2.0", sig_doc.GetAllocator());
276-
sig_doc.AddMember("TLS.sdkappid", sdkappid, sig_doc.GetAllocator());
277-
sig_doc.AddMember("TLS.identifier", userid, sig_doc.GetAllocator());
278-
sig_doc.AddMember("TLS.time", currTime, sig_doc.GetAllocator());
279-
sig_doc.AddMember("TLS.expire", expire, sig_doc.GetAllocator());
280-
sig_doc.AddMember("TLS.userbuf", base64UserBuf, sig_doc.GetAllocator());
281-
sig_doc.AddMember("TLS.sig", base64RawSig, sig_doc.GetAllocator());
282-
return json2sig(sig_doc, usersig, errmsg);
257+
std::string userbuf = gen_userbuf(userid, sdkappid, roomid, expire, privilegeMap, 0,"");
258+
return genSig(sdkappid,userid,key,userbuf,expire,usersig,errmsg);
259+
}
260+
// 生成带 userbuf 的签名,字符串房间号
261+
TLS_API int genPrivateMapKeyWithStringRoomID(uint32_t sdkappid, const std::string &userid, const std::string &key, const std::string &roomstr,
262+
int expire, int privilegeMap, std::string &usersig, std::string &errmsg)
263+
{
264+
std::string userbuf = gen_userbuf(userid, sdkappid, 0, expire, privilegeMap, 0,roomstr);
265+
return genSig(sdkappid,userid,key,userbuf,expire,usersig,errmsg);
283266
}
284267

285268
TLS_API std::string gen_userbuf(const std::string &account, uint32_t dwSdkappid, uint32_t dwAuthID,
286-
uint32_t dwExpTime, uint32_t dwPrivilegeMap, uint32_t dwAccountType)
269+
uint32_t dwExpTime, uint32_t dwPrivilegeMap, uint32_t dwAccountType, const std::string &roomStr)
287270
{
288271
int length = 1 + 2 + account.length() + 20;
289272
int offset = 0;
290273
char userBuf[length];
291274
memset(userBuf, 0, sizeof(userBuf));
292275

293-
userBuf[offset++] = 0;
276+
if (roomStr.length() > 0)
277+
{
278+
userBuf[offset++] = 1;
279+
length += 2 + roomStr.length();
280+
}
281+
else
282+
userBuf[offset++] = 0;
294283

295284
userBuf[offset++] = ((account.length() & 0xFF00) >> 8);
296285
userBuf[offset++] = (account.length() & 0x00FF);
@@ -330,5 +319,45 @@ TLS_API std::string gen_userbuf(const std::string &account, uint32_t dwSdkappid,
330319
userBuf[offset++] = ((dwAccountType & 0x00FF0000) >> 16);
331320
userBuf[offset++] = ((dwAccountType & 0x0000FF00) >> 8);
332321
userBuf[offset++] = (dwAccountType & 0x000000FF);
322+
323+
if (roomStr.length() > 0)
324+
{
325+
userBuf[offset++] = ((roomStr.length() & 0xFF00) >> 8);
326+
userBuf[offset++] = (roomStr.length() & 0x00FF);
327+
328+
for (; offset < length; ++offset)
329+
{
330+
userBuf[offset] = account[offset - (length - roomStr.length())];
331+
}
332+
}
333333
return std::string(userBuf, length);
334+
}
335+
TLS_API int genSig(uint32_t sdkappid, const std::string &userid, const std::string &key, const std::string &userbuf,
336+
int expire, std::string &usersig, std::string &errmsg)
337+
{
338+
uint64_t currTime = time(NULL);
339+
std::string base64UserBuf = "";
340+
std::string base64RawSig = "";
341+
if(userbuf.length() >0)
342+
{
343+
base64_encode(userbuf.data(), userbuf.length(), base64UserBuf);
344+
base64RawSig = hmacsha256(
345+
sdkappid, userid, currTime, expire, key, base64UserBuf);
346+
}
347+
else
348+
{
349+
base64RawSig = hmacsha256(sdkappid, userid, currTime, expire, key);
350+
}
351+
352+
rapidjson::Document sig_doc;
353+
sig_doc.SetObject();
354+
sig_doc.AddMember("TLS.ver", "2.0", sig_doc.GetAllocator());
355+
sig_doc.AddMember("TLS.sdkappid", sdkappid, sig_doc.GetAllocator());
356+
sig_doc.AddMember("TLS.identifier", userid, sig_doc.GetAllocator());
357+
sig_doc.AddMember("TLS.time", currTime, sig_doc.GetAllocator());
358+
sig_doc.AddMember("TLS.expire", expire, sig_doc.GetAllocator());
359+
if(base64UserBuf.length() > 0)
360+
sig_doc.AddMember("TLS.userbuf", base64UserBuf, sig_doc.GetAllocator());
361+
sig_doc.AddMember("TLS.sig", base64RawSig, sig_doc.GetAllocator());
362+
return json2sig(sig_doc, usersig, errmsg);
334363
}

src/tls_sig_api_v2.h

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,61 @@ TLS_API int genPrivateMapKey(
9191
std::string &usersig,
9292
std::string &errmsg);
9393

94+
/**
95+
*【功能说明】
96+
* 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
97+
* PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
98+
* - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
99+
* - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
100+
* 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。\
101+
*
102+
*【参数说明】
103+
* @param sdkappid - 应用id。
104+
* @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
105+
* @param key - 计算 usersig 用的加密密钥,控制台可获取。
106+
* @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
107+
* @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
108+
* @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
109+
* - 第 1 位:0000 0001 = 1,创建房间的权限
110+
* - 第 2 位:0000 0010 = 2,加入房间的权限
111+
* - 第 3 位:0000 0100 = 4,发送语音的权限
112+
* - 第 4 位:0000 1000 = 8,接收语音的权限
113+
* - 第 5 位:0001 0000 = 16,发送视频的权限
114+
* - 第 6 位:0010 0000 = 32,接收视频的权限
115+
* - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
116+
* - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
117+
* - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
118+
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
119+
* @param usersig - 生成的usersig。
120+
* @param errmsg - 错误信息。
121+
* @return 0 为成功,非 0 为失败
122+
*/
123+
TLS_API int genPrivateMapKeyWithStringRoomID(
124+
uint32_t sdkappid,
125+
const std::string &userid,
126+
const std::string &key,
127+
const std::string &roomstr,
128+
int expire,
129+
int privilegeMap,
130+
std::string &usersig,
131+
std::string &errmsg);
132+
94133
TLS_API std::string gen_userbuf(
95134
const std::string &account,
96135
uint32_t dwSdkappid,
97136
uint32_t dwAuthID,
98137
uint32_t dwExpTime,
99138
uint32_t dwPrivilegeMap,
100-
uint32_t dwAccountType);
139+
uint32_t dwAccountType,
140+
const std::string &roomStr);
141+
142+
TLS_API int genSig(uint32_t sdkappid,
143+
const std::string &userid,
144+
const std::string &key,
145+
const std::string &userbuf,
146+
int expire,
147+
std::string &usersig,
148+
std::string &errmsg);
101149
int thread_setup();
102150
void thread_cleanup();
103151

src/tlssigapi_v2_tool.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ static void usage(const std::string &prog)
1919
std::cout << "Usage:" << std::endl;
2020
std::cout << "\tgen sig: " << prog << " gen key sig_file sdkappid identifier" << std::endl;
2121
std::cout << "\tgen sig e.g.: " << prog << " gen 5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e sig 1400000000 xiaojun" << std::endl;
22-
std::cout << "\tgenuser sig: " << prog << " genuser key sig_file sdkappid identifier userbuf" << std::endl;
23-
std::cout << "\tgenuser sig: " << prog << " genuser 5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e sig 1400000000 xiaojun abc" << std::endl;
22+
std::cout << "\tgenuser sig: " << prog << " genuser key sig_file sdkappid identifier" << std::endl;
23+
std::cout << "\tgenuser sig: " << prog << " genuser 5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e sig 1400000000 xiaojun" << std::endl;
24+
std::cout << "\tgenuserbystringroom sig: " << prog << " genuserbystringroom 5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e sig 1400000000 xiaojun" << std::endl;
2425
}
2526

2627
static int genUserSig(const std::string &key, const std::string &sig_file,
@@ -74,6 +75,44 @@ static int genPrivateMapKey(const std::string &key, const std::string &sig_file,
7475
return -3;
7576
}
7677

78+
#if defined(WIN32) || defined(WIN64)
79+
FILE *sig_fp = NULL;
80+
fopen_s(&sig_fp, sig_file.c_str(), "w+");
81+
#else
82+
FILE *sig_fp = fopen(sig_file.c_str(), "w+");
83+
#endif
84+
if (!sig_fp)
85+
{
86+
std::cout << "open file " << sig_file << "failed" << std::endl;
87+
return -4;
88+
}
89+
90+
// 将签名写入文件
91+
int written_cnt = (int)fwrite(sig.c_str(), sizeof(char), sig.size(), sig_fp);
92+
if (sig.size() > (unsigned int)written_cnt && 0 != ferror(sig_fp))
93+
{
94+
std::cout << "write sig content failed" << std::endl;
95+
return -5;
96+
}
97+
98+
std::cout << sig << std::endl;
99+
std::cout << "generate sig ok" << std::endl;
100+
101+
return 0;
102+
}
103+
static int genPrivateMapKeyWithStringRoomID(const std::string &key, const std::string &sig_file,
104+
uint32_t sdkappid, const std::string &userid)
105+
{
106+
std::string sig;
107+
std::string err_msg;
108+
int ret = genPrivateMapKeyWithStringRoomID(sdkappid, userid, key,
109+
"10000657", 180 * 86400, 255, sig, err_msg);
110+
if (0 != ret)
111+
{
112+
std::cout << "error msg: " << err_msg << " return " << ret << std::endl;
113+
return -3;
114+
}
115+
77116
#if defined(WIN32) || defined(WIN64)
78117
FILE *sig_fp = NULL;
79118
fopen_s(&sig_fp, sig_file.c_str(), "w+");
@@ -131,6 +170,15 @@ int main(int argc, char *argv[])
131170
ret = genPrivateMapKey(key, sig_file,
132171
strtol(sdkappid_str.c_str(), NULL, 10), userid);
133172
}
173+
else if (0 == strcmp(cmd, "genuserbystringroom") && 6 == argc)
174+
{
175+
std::string key = argv[2];
176+
std::string sig_file = argv[3];
177+
std::string sdkappid_str = argv[4];
178+
std::string userid = argv[5];
179+
ret = genPrivateMapKeyWithStringRoomID(key, sig_file,
180+
strtol(sdkappid_str.c_str(), NULL, 10), userid);
181+
}
134182
else
135183
{
136184
usage(argv[0]);

0 commit comments

Comments
 (0)