GetUserSig.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\Tencent;
  4. class GetUserSig {
  5. private $key = false;
  6. private $sdkappid = 0;
  7. /**
  8. *【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
  9. *
  10. *【参数说明】
  11. * @param string userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
  12. * @param string expire - UserSig 票据的过期时间,单位是秒,比如 86400 代表生成的 UserSig 票据在一天后就无法再使用了。
  13. * @return string 签名字符串
  14. * @throws \Exception
  15. */
  16. public function genUserSig( $userid, $expire = 864000 ) {
  17. return $this->__genSig( $userid, $expire, '', false );
  18. }
  19. /**
  20. *【功能说明】
  21. * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
  22. * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
  23. * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
  24. * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
  25. * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
  26. *
  27. *【参数说明】
  28. * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
  29. * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
  30. * @param roomid - 房间号,用于指定该 userid 可以进入的房间号
  31. * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
  32. * - 第 1 位:0000 0001 = 1,创建房间的权限
  33. * - 第 2 位:0000 0010 = 2,加入房间的权限
  34. * - 第 3 位:0000 0100 = 4,发送语音的权限
  35. * - 第 4 位:0000 1000 = 8,接收语音的权限
  36. * - 第 5 位:0001 0000 = 16,发送视频的权限
  37. * - 第 6 位:0010 0000 = 32,接收视频的权限
  38. * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
  39. * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
  40. * - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
  41. * - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
  42. */
  43. public function genPrivateMapKey( $userid, $expire, $roomid, $privilegeMap ) {
  44. $userbuf = $this->__genUserBuf( $userid, $roomid, $expire, $privilegeMap, 0, '' );
  45. return $this->__genSig( $userid, $expire, $userbuf, true );
  46. }
  47. /**
  48. *【功能说明】
  49. * 用于签发 TRTC 进房参数中可选的 PrivateMapKey 权限票据。
  50. * PrivateMapKey 需要跟 UserSig 一起使用,但 PrivateMapKey 比 UserSig 有更强的权限控制能力:
  51. * - UserSig 只能控制某个 UserID 有无使用 TRTC 服务的权限,只要 UserSig 正确,其对应的 UserID 可以进出任意房间。
  52. * - PrivateMapKey 则是将 UserID 的权限控制的更加严格,包括能不能进入某个房间,能不能在该房间里上行音视频等等。
  53. * 如果要开启 PrivateMapKey 严格权限位校验,需要在【实时音视频控制台】=>【应用管理】=>【应用信息】中打开“启动权限密钥”开关。
  54. *
  55. *【参数说明】
  56. * @param userid - 用户id,限制长度为32字节,只允许包含大小写英文字母(a-zA-Z)、数字(0-9)及下划线和连词符。
  57. * @param expire - PrivateMapKey 票据的过期时间,单位是秒,比如 86400 生成的 PrivateMapKey 票据在一天后就无法再使用了。
  58. * @param roomstr - 房间号,用于指定该 userid 可以进入的房间号
  59. * @param privilegeMap - 权限位,使用了一个字节中的 8 个比特位,分别代表八个具体的功能权限开关:
  60. * - 第 1 位:0000 0001 = 1,创建房间的权限
  61. * - 第 2 位:0000 0010 = 2,加入房间的权限
  62. * - 第 3 位:0000 0100 = 4,发送语音的权限
  63. * - 第 4 位:0000 1000 = 8,接收语音的权限
  64. * - 第 5 位:0001 0000 = 16,发送视频的权限
  65. * - 第 6 位:0010 0000 = 32,接收视频的权限
  66. * - 第 7 位:0100 0000 = 64,发送辅路(也就是屏幕分享)视频的权限
  67. * - 第 8 位:1000 0000 = 200,接收辅路(也就是屏幕分享)视频的权限
  68. * - privilegeMap == 1111 1111 == 255 代表该 userid 在该 roomid 房间内的所有功能权限。
  69. * - privilegeMap == 0010 1010 == 42 代表该 userid 拥有加入房间和接收音视频数据的权限,但不具备其他权限。
  70. */
  71. public function genPrivateMapKeyWithStringRoomID( $userid, $expire, $roomstr, $privilegeMap ) {
  72. $userbuf = $this->__genUserBuf( $userid, 0, $expire, $privilegeMap, 0, $roomstr );
  73. return $this->__genSig( $userid, $expire, $userbuf, true );
  74. }
  75. public function __construct( $sdkappid, $key ) {
  76. $this->sdkappid = $sdkappid;
  77. $this->key = $key;
  78. }
  79. /**
  80. * 用于 url 的 base64 encode
  81. * '+' => '*', '/' => '-', '=' => '_'
  82. * @param string $string 需要编码的数据
  83. * @return string 编码后的base64串,失败返回false
  84. * @throws \Exception
  85. */
  86. private function base64_url_encode( $string ) {
  87. static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
  88. $base64 = base64_encode( $string );
  89. if ( $base64 === false ) {
  90. throw new \Exception( 'base64_encode error' );
  91. }
  92. return str_replace( array_keys( $replace ), array_values( $replace ), $base64 );
  93. }
  94. /**
  95. * 用于 url 的 base64 decode
  96. * '+' => '*', '/' => '-', '=' => '_'
  97. * @param string $base64 需要解码的base64串
  98. * @return string 解码后的数据,失败返回false
  99. * @throws \Exception
  100. */
  101. private function base64_url_decode( $base64 ) {
  102. static $replace = Array( '+' => '*', '/' => '-', '=' => '_' );
  103. $string = str_replace( array_values( $replace ), array_keys( $replace ), $base64 );
  104. $result = base64_decode( $string );
  105. if ( $result == false ) {
  106. throw new \Exception( 'base64_url_decode error' );
  107. }
  108. return $result;
  109. }
  110. /**
  111. * TRTC业务进房权限加密串使用用户定义的userbuf
  112. * @brief 生成 userbuf
  113. * @param account 用户名
  114. * @param dwSdkappid sdkappid
  115. * @param dwAuthID 数字房间号
  116. * @param dwExpTime 过期时间:该权限加密串的过期时间. 过期时间 = now+dwExpTime
  117. * @param dwPrivilegeMap 用户权限,255表示所有权限
  118. * @param dwAccountType 用户类型, 默认为0
  119. * @param roomStr 字符串房间号
  120. * @return userbuf string 返回的userbuf
  121. */
  122. private function __genUserBuf( $account, $dwAuthID, $dwExpTime, $dwPrivilegeMap, $dwAccountType,$roomStr ) {
  123. //cVer unsigned char/1 版本号,填0
  124. if($roomStr == '')
  125. $userbuf = pack( 'C1', '0' );
  126. else
  127. $userbuf = pack( 'C1', '1' );
  128. $userbuf .= pack( 'n', strlen( $account ) );
  129. //wAccountLen unsigned short /2 第三方自己的帐号长度
  130. $userbuf .= pack( 'a'.strlen( $account ), $account );
  131. //buffAccount wAccountLen 第三方自己的帐号字符
  132. $userbuf .= pack( 'N', $this->sdkappid );
  133. //dwSdkAppid unsigned int/4 sdkappid
  134. $userbuf .= pack( 'N', $dwAuthID );
  135. //dwAuthId unsigned int/4 群组号码/音视频房间号
  136. $expire = $dwExpTime + time();
  137. $userbuf .= pack( 'N', $expire );
  138. //dwExpTime unsigned int/4 过期时间 (当前时间 + 有效期(单位:秒,建议300秒))
  139. $userbuf .= pack( 'N', $dwPrivilegeMap );
  140. //dwPrivilegeMap unsigned int/4 权限位
  141. $userbuf .= pack( 'N', $dwAccountType );
  142. //dwAccountType unsigned int/4
  143. if($roomStr != '')
  144. {
  145. $userbuf .= pack( 'n', strlen( $roomStr ) );
  146. //roomStrLen unsigned short /2 字符串房间号长度
  147. $userbuf .= pack( 'a'.strlen( $roomStr ), $roomStr );
  148. //roomStr roomStrLen 字符串房间号
  149. }
  150. return $userbuf;
  151. }
  152. /**
  153. * 使用 hmac sha256 生成 sig 字段内容,经过 base64 编码
  154. * @param $identifier 用户名,utf-8 编码
  155. * @param $curr_time 当前生成 sig 的 unix 时间戳
  156. * @param $expire 有效期,单位秒
  157. * @param $base64_userbuf base64 编码后的 userbuf
  158. * @param $userbuf_enabled 是否开启 userbuf
  159. * @return string base64 后的 sig
  160. */
  161. private function hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled ) {
  162. $content_to_be_signed = 'TLS.identifier:' . $identifier . "\n"
  163. . 'TLS.sdkappid:' . $this->sdkappid . "\n"
  164. . 'TLS.time:' . $curr_time . "\n"
  165. . 'TLS.expire:' . $expire . "\n";
  166. if ( true == $userbuf_enabled ) {
  167. $content_to_be_signed .= 'TLS.userbuf:' . $base64_userbuf . "\n";
  168. }
  169. return base64_encode( hash_hmac( 'sha256', $content_to_be_signed, $this->key, true ) );
  170. }
  171. /**
  172. * 生成签名。
  173. *
  174. * @param $identifier 用户账号
  175. * @param int $expire 过期时间,单位秒,默认 180 天
  176. * @param $userbuf base64 编码后的 userbuf
  177. * @param $userbuf_enabled 是否开启 userbuf
  178. * @return string 签名字符串
  179. * @throws \Exception
  180. */
  181. private function __genSig( $identifier, $expire, $userbuf, $userbuf_enabled ) {
  182. $curr_time = time();
  183. $sig_array = Array(
  184. 'TLS.ver' => '2.0',
  185. 'TLS.identifier' => strval( $identifier ),
  186. 'TLS.sdkappid' => intval( $this->sdkappid ),
  187. 'TLS.expire' => intval( $expire ),
  188. 'TLS.time' => intval( $curr_time )
  189. );
  190. $base64_userbuf = '';
  191. if ( true == $userbuf_enabled ) {
  192. $base64_userbuf = base64_encode( $userbuf );
  193. $sig_array['TLS.userbuf'] = strval( $base64_userbuf );
  194. }
  195. $sig_array['TLS.sig'] = $this->hmacsha256( $identifier, $curr_time, $expire, $base64_userbuf, $userbuf_enabled );
  196. if ( $sig_array['TLS.sig'] === false ) {
  197. throw new \Exception( 'base64_encode error' );
  198. }
  199. $json_str_sig = json_encode( $sig_array );
  200. if ( $json_str_sig === false ) {
  201. throw new \Exception( 'json_encode error' );
  202. }
  203. $compressed = gzcompress( $json_str_sig );
  204. if ( $compressed === false ) {
  205. throw new \Exception( 'gzcompress error' );
  206. }
  207. return $this->base64_url_encode( $compressed );
  208. }
  209. /**
  210. * 验证签名。
  211. *
  212. * @param string $sig 签名内容
  213. * @param string $identifier 需要验证用户名,utf-8 编码
  214. * @param int $init_time 返回的生成时间,unix 时间戳
  215. * @param int $expire_time 返回的有效期,单位秒
  216. * @param string $userbuf 返回的用户数据
  217. * @param string $error_msg 失败时的错误信息
  218. * @return boolean 验证是否成功
  219. * @throws \Exception
  220. */
  221. private function __verifySig( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
  222. try {
  223. $error_msg = '';
  224. $compressed_sig = $this->base64_url_decode( $sig );
  225. $pre_level = error_reporting( E_ERROR );
  226. $uncompressed_sig = gzuncompress( $compressed_sig );
  227. error_reporting( $pre_level );
  228. if ( $uncompressed_sig === false ) {
  229. throw new \Exception( 'gzuncompress error' );
  230. }
  231. $sig_doc = json_decode( $uncompressed_sig );
  232. if ( $sig_doc == false ) {
  233. throw new \Exception( 'json_decode error' );
  234. }
  235. $sig_doc = ( array )$sig_doc;
  236. if ( $sig_doc['TLS.identifier'] !== $identifier ) {
  237. throw new \Exception( "identifier dosen't match" );
  238. }
  239. if ( $sig_doc['TLS.sdkappid'] != $this->sdkappid ) {
  240. throw new \Exception( "sdkappid dosen't match" );
  241. }
  242. $sig = $sig_doc['TLS.sig'];
  243. if ( $sig == false ) {
  244. throw new \Exception( 'sig field is missing' );
  245. }
  246. $init_time = $sig_doc['TLS.time'];
  247. $expire_time = $sig_doc['TLS.expire'];
  248. $curr_time = time();
  249. if ( $curr_time > $init_time+$expire_time ) {
  250. throw new \Exception( 'sig expired' );
  251. }
  252. $userbuf_enabled = false;
  253. $base64_userbuf = '';
  254. if ( isset( $sig_doc['TLS.userbuf'] ) ) {
  255. $base64_userbuf = $sig_doc['TLS.userbuf'];
  256. $userbuf = base64_decode( $base64_userbuf );
  257. $userbuf_enabled = true;
  258. }
  259. $sigCalculated = $this->hmacsha256( $identifier, $init_time, $expire_time, $base64_userbuf, $userbuf_enabled );
  260. if ( $sig != $sigCalculated ) {
  261. throw new \Exception( 'verify failed' );
  262. }
  263. return true;
  264. } catch ( \Exception $ex ) {
  265. $error_msg = $ex->getMessage();
  266. return false;
  267. }
  268. }
  269. /**
  270. * 带 userbuf 验证签名。
  271. *
  272. * @param string $sig 签名内容
  273. * @param string $identifier 需要验证用户名,utf-8 编码
  274. * @param int $init_time 返回的生成时间,unix 时间戳
  275. * @param int $expire_time 返回的有效期,单位秒
  276. * @param string $error_msg 失败时的错误信息
  277. * @return boolean 验证是否成功
  278. * @throws \Exception
  279. */
  280. public function verifySig( $sig, $identifier, &$init_time, &$expire_time, &$error_msg ) {
  281. $userbuf = '';
  282. return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
  283. }
  284. /**
  285. * 验证签名
  286. * @param string $sig 签名内容
  287. * @param string $identifier 需要验证用户名,utf-8 编码
  288. * @param int $init_time 返回的生成时间,unix 时间戳
  289. * @param int $expire_time 返回的有效期,单位秒
  290. * @param string $userbuf 返回的用户数据
  291. * @param string $error_msg 失败时的错误信息
  292. * @return boolean 验证是否成功
  293. * @throws \Exception
  294. */
  295. public function verifySigWithUserBuf( $sig, $identifier, &$init_time, &$expire_time, &$userbuf, &$error_msg ) {
  296. return $this->__verifySig( $sig, $identifier, $init_time, $expire_time, $userbuf, $error_msg );
  297. }
  298. }