Skip to content

Commit b042465

Browse files
authored
Merge pull request #12 from Tomxiaobai/master
usersig update
2 parents 77e574d + c0a21e3 commit b042465

File tree

2 files changed

+171
-1
lines changed

2 files changed

+171
-1
lines changed

README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Note
2+
This project is the php implementation of tls-sig-api-v2. Previous asymmetric keys cannot use APIs of this version. To enable them to use APIs of this version,[see here](https://github.com/tencentyun/tls-sig-api-php)
3+
4+
## integration
5+
You can use composer or source code integration.
6+
7+
### composer integration
8+
``` json
9+
{
10+
"require": {
11+
"tencent/tls-sig-api-v2": "1.0"
12+
}
13+
}
14+
```
15+
16+
### source code integration
17+
Download `TLSSigAPIv2.php` to the project.
18+
19+
## use
20+
``` php
21+
<?php
22+
23+
require 'vendor/autoload.php'
24+
// require_once "../src/TLSSigAPIv2.php"; // 源码集成使用相对路径
25+
26+
$api = new \Tencent\TLSSigAPIv2(1400000000, '5bd2850fff3ecb11d7c805251c51ee463a25727bddc2385f3fa8bfee1bb93b5e');
27+
$sig = $api->genUserSig('xiaojun');
28+
echo $sig . "\n";
29+
```

src/TLSSigAPIv2.php

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class TLSSigAPIv2 {
2121
* @throws \Exception
2222
*/
2323

24+
/**
25+
* Function: Used to issue UserSig that is required by the TRTC and IM services.
26+
*
27+
* Parameter description:
28+
* @param userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
29+
* @param expire - UserSig expiration time, in seconds. For example, 86400 indicates that the generated UserSig will expire one day after being generated.
30+
* @return string signature string
31+
* @throws \Exception
32+
*/
33+
2434
public function genUserSig( $userid, $expire = 86400*180 ) {
2535
return $this->__genSig( $userid, $expire, '', false );
2636
}
@@ -50,6 +60,31 @@ public function genUserSig( $userid, $expire = 86400*180 ) {
5060
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
5161
*/
5262

63+
/**
64+
* Function:
65+
* Used to issue PrivateMapKey that is optional for room entry.
66+
* PrivateMapKey must be used together with UserSig but with more powerful permission control capabilities.
67+
* - UserSig can only control whether a UserID has permission to use the TRTC service. As long as the UserSig is correct, the user with the corresponding UserID can enter or leave any room.
68+
* - PrivateMapKey specifies more stringent permissions for a UserID, including whether the UserID can be used to enter a specific room and perform audio/video upstreaming in the room.
69+
* To enable stringent PrivateMapKey permission bit verification, you need to enable permission key in TRTC console > Application Management > Application Info.
70+
*
71+
* Parameter description:
72+
* userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
73+
* roomid - ID of the room to which the specified UserID can enter.
74+
* expire - PrivateMapKey expiration time, in seconds. For example, 86400 indicates that the generated PrivateMapKey will expire one day after being generated.
75+
* privilegeMap - Permission bits. Eight bits in the same byte are used as the permission switches of eight specific features:
76+
* - Bit 1: 0000 0001 = 1, permission for room creation
77+
* - Bit 2: 0000 0010 = 2, permission for room entry
78+
* - Bit 3: 0000 0100 = 4, permission for audio sending
79+
* - Bit 4: 0000 1000 = 8, permission for audio receiving
80+
* - Bit 5: 0001 0000 = 16, permission for video sending
81+
* - Bit 6: 0010 0000 = 32, permission for video receiving
82+
* - Bit 7: 0100 0000 = 64, permission for substream video sending (screen sharing)
83+
* - Bit 8: 1000 0000 = 200, permission for substream video receiving (screen sharing)
84+
* - privilegeMap == 1111 1111 == 255: Indicates that the UserID has all feature permissions of the room specified by roomid.
85+
* - privilegeMap == 0010 1010 == 42: Indicates that the UserID has only the permissions to enter the room and receive audio/video data.
86+
*/
87+
5388
public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
5489
$userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
5590
return $this->__genSig( $userid, $expire, $userbuf, true );
@@ -79,6 +114,31 @@ public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
79114
* - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
80115
*/
81116

117+
/**
118+
* Function:
119+
* Used to issue PrivateMapKey that is optional for room entry.
120+
* PrivateMapKey must be used together with UserSig but with more powerful permission control capabilities.
121+
* - UserSig can only control whether a UserID has permission to use the TRTC service. As long as the UserSig is correct, the user with the corresponding UserID can enter or leave any room.
122+
* - PrivateMapKey specifies more stringent permissions for a UserID, including whether the UserID can be used to enter a specific room and perform audio/video upstreaming in the room.
123+
* To enable stringent PrivateMapKey permission bit verification, you need to enable permission key in TRTC console > Application Management > Application Info.
124+
*
125+
* Parameter description:
126+
* @param userid - User ID. The value can be up to 32 bytes in length and contain letters (a-z and A-Z), digits (0-9), underscores (_), and hyphens (-).
127+
* @param roomstr - ID of the room to which the specified UserID can enter.
128+
* @param expire - PrivateMapKey expiration time, in seconds. For example, 86400 indicates that the generated PrivateMapKey will expire one day after being generated.
129+
* @param privilegeMap - Permission bits. Eight bits in the same byte are used as the permission switches of eight specific features:
130+
* - Bit 1: 0000 0001 = 1, permission for room creation
131+
* - Bit 2: 0000 0010 = 2, permission for room entry
132+
* - Bit 3: 0000 0100 = 4, permission for audio sending
133+
* - Bit 4: 0000 1000 = 8, permission for audio receiving
134+
* - Bit 5: 0001 0000 = 16, permission for video sending
135+
* - Bit 6: 0010 0000 = 32, permission for video receiving
136+
* - Bit 7: 0100 0000 = 64, permission for substream video sending (screen sharing)
137+
* - Bit 8: 1000 0000 = 200, permission for substream video receiving (screen sharing)
138+
* - privilegeMap == 1111 1111 == 255: Indicates that the UserID has all feature permissions of the room specified by roomid.
139+
* - privilegeMap == 0010 1010 == 42: Indicates that the UserID has only the permissions to enter the room and receive audio/video data.
140+
*/
141+
82142
public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
83143
$userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
84144
return $this->__genSig( $userid, $expire, $userbuf, true );
@@ -97,6 +157,13 @@ public function __construct( $sdkappid, $key ) {
97157
* @throws \Exception
98158
*/
99159

160+
/**
161+
* base64 encode for url
162+
* '+' => '*', '/' => '-', '=' => '_'
163+
* @param string $string data to be encoded
164+
* @return string The encoded base64 string, returns false on failure
165+
* @throws \Exception
166+
*/
100167
private function base64_url_encode( $string ) {
101168
static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
102169
$base64 = base64_encode( $string );
@@ -114,6 +181,13 @@ private function base64_url_encode( $string ) {
114181
* @throws \Exception
115182
*/
116183

184+
/**
185+
* base64 decode for url
186+
* '+' => '*', '/' => '-', '=' => '_'
187+
* @param string $base64 base64 string to be decoded
188+
* @return string Decoded data, return false on failure
189+
* @throws \Exception
190+
*/
117191
private function base64_url_decode( $base64 ) {
118192
static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
119193
$string = str_replace( array_values( $replace ), array_keys( $replace ), $base64 );
@@ -136,6 +210,19 @@ private function base64_url_decode( $base64 ) {
136210
* @return userbuf string 返回的userbuf
137211
*/
138212

213+
/**
214+
* User-defined userbuf is used for the encrypted string of TRTC service entry permission
215+
* @brief generate userbuf
216+
* @param account username
217+
* @param dwSdkappid sdkappid
218+
* @param dwAuthID digital room number
219+
* @param dwExpTime Expiration time: The expiration time of the encrypted string of this permission. Expiration time = now+dwExpTime
220+
* @param dwPrivilegeMap User permissions, 255 means all permissions
221+
* @param dwAccountType User type, default is 0
222+
* @param roomStr String room number
223+
* @return userbuf string returned userbuf
224+
*/
225+
139226
private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {
140227

141228
//cVer unsigned char/1 版本号,填0
@@ -178,6 +265,15 @@ private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap,
178265
* @return string base64 后的 sig
179266
*/
180267

268+
/**
269+
* Use hmac sha256 to generate sig field content, base64 encoded
270+
* @param $identifier Username, utf-8 encoded
271+
* @param $curr_time The unix timestamp of the current generated sig
272+
* @param $expire Validity period, in seconds
273+
* @param $base64_userbuf base64 encoded userbuf
274+
* @param $userbuf_enabled 是No enable userbuf
275+
* @return string sig after base64
276+
*/
181277
private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled ) {
182278
$content_to_be_signed = 'TLS.identifier:' . $identifier . "\n"
183279
. 'TLS.sdkappid:' . $this->sdkappid . "\n"
@@ -199,7 +295,17 @@ private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf,
199295
* @return string 签名字符串
200296
* @throws \Exception
201297
*/
202-
298+
299+
/**
300+
* Generate signature.
301+
*
302+
* @param $identifier user account
303+
* @param int $expire Expiration time, in seconds, default 180 days
304+
* @param $userbuf base64 encoded userbuf
305+
* @param $userbuf_enabled Whether to enable userbuf
306+
* @return string signature string
307+
* @throws \Exception
308+
*/
203309
private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
204310
$curr_time = time();
205311
$sig_array = Array(
@@ -242,6 +348,19 @@ private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
242348
* @param string $error_msg 失败时的错误信息
243349
* @return boolean 验证是否成功
244350
* @throws \Exception
351+
*/
352+
353+
/**
354+
* Verify signature.
355+
*
356+
* @param string $sig Signature content
357+
* @param string $identifier Need to authenticate user name, utf-8 encoding
358+
* @param int $init_time Returned generation time, unix timestamp
359+
* @param int $expire_time Return the validity period, in seconds
360+
* @param string $userbuf returned user data
361+
* @param string $error_msg error message on failure
362+
* @return boolean Verify success
363+
* @throws \Exception
245364
*/
246365

247366
private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
@@ -310,6 +429,17 @@ private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$u
310429
* @throws \Exception
311430
*/
312431

432+
/**
433+
* Verify signature with userbuf.
434+
*
435+
* @param string $sig Signature content
436+
* @param string $identifier Need to authenticate user name, utf-8 encoding
437+
* @param int $init_time Returned generation time, unix timestamp
438+
* @param int $expire_time Return the validity period, in seconds
439+
* @param string $error_msg error message on failure
440+
* @return boolean Verify success
441+
* @throws \Exception
442+
*/
313443
public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$error_msg ) {
314444
$userbuf = '';
315445
return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
@@ -327,6 +457,17 @@ public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$erro
327457
* @throws \Exception
328458
*/
329459

460+
/**
461+
* Verify signature
462+
* @param string $sig Signature content
463+
* @param string $identifier Need to authenticate user name, utf-8 encoding
464+
* @param int $init_time Returned generation time, unix timestamp
465+
* @param int $expire_time Return the validity period, in seconds
466+
* @param string $userbuf returned user data
467+
* @param string $error_msg error message on failure
468+
* @return boolean Verify success
469+
* @throws \Exception
470+
*/
330471
public function verifySigWithUserBuf( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
331472
return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
332473
}

0 commit comments

Comments
 (0)