qcloud-sts-sdk.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * 代码出处:
  4. * https://github.com/tencentyun/qcloud-cos-sts-sdk
  5. */
  6. class STS{
  7. // 临时密钥计算样例
  8. function _hex2bin($data) {
  9. $len = strlen($data);
  10. return pack("H" . $len, $data);
  11. }
  12. // obj 转 query string
  13. function json2str($obj, $notEncode = false) {
  14. ksort($obj);
  15. $arr = array();
  16. if(!is_array($obj)){
  17. throw new Exception($obj + " must be a array");
  18. }
  19. foreach ($obj as $key => $val) {
  20. array_push($arr, $key . '=' . ($notEncode ? $val : rawurlencode($val)));
  21. }
  22. return join('&', $arr);
  23. }
  24. // 计算临时密钥用的签名
  25. function getSignature($opt, $key, $method, $config) {
  26. $formatString = $method . $config['domain'] . '/?' . $this->json2str($opt, 1);
  27. $sign = hash_hmac('sha1', $formatString, $key);
  28. $sign = base64_encode($this->_hex2bin($sign));
  29. return $sign;
  30. }
  31. // v2接口的key首字母小写,v3改成大写,此处做了向下兼容
  32. function backwardCompat($result) {
  33. if(!is_array($result)){
  34. throw new Exception($result + " must be a array");
  35. }
  36. $compat = array();
  37. foreach ($result as $key => $value) {
  38. if(is_array($value)) {
  39. $compat[lcfirst($key)] = $this->backwardCompat($value);
  40. } elseif ($key == 'Token') {
  41. $compat['sessionToken'] = $value;
  42. } else {
  43. $compat[lcfirst($key)] = $value;
  44. }
  45. }
  46. return $compat;
  47. }
  48. // 获取临时密钥
  49. function getTempKeys($config) {
  50. if(array_key_exists('bucket', $config)){
  51. $ShortBucketName = substr($config['bucket'],0, strripos($config['bucket'], '-'));
  52. $AppId = substr($config['bucket'], 1 + strripos($config['bucket'], '-'));
  53. }
  54. if(array_key_exists('policy', $config)){
  55. $policy = $config['policy'];
  56. }else{
  57. $policy = array(
  58. 'version'=> '2.0',
  59. 'statement'=> array(
  60. array(
  61. 'action'=> $config['allowActions'],
  62. 'effect'=> 'allow',
  63. 'principal'=> array('qcs'=> array('*')),
  64. 'resource'=> array(
  65. 'qcs::cos:' . $config['region'] . ':uid/' . $AppId . ':prefix//' . $AppId . '/' . $ShortBucketName . '/' . $config['allowPrefix']
  66. )
  67. )
  68. )
  69. );
  70. }
  71. $policyStr = str_replace('\\/', '/', json_encode($policy));
  72. $Action = 'GetFederationToken';
  73. $Nonce = rand(10000, 20000);
  74. $Timestamp = time();
  75. $Method = 'POST';
  76. $params = array(
  77. 'SecretId'=> $config['secretId'],
  78. 'Timestamp'=> $Timestamp,
  79. 'Nonce'=> $Nonce,
  80. 'Action'=> $Action,
  81. 'DurationSeconds'=> $config['durationSeconds'],
  82. 'Version'=>'2018-08-13',
  83. 'Name'=> 'cos',
  84. 'Region'=> 'ap-guangzhou',
  85. 'Policy'=> urlencode($policyStr)
  86. );
  87. $params['Signature'] = $this->getSignature($params, $config['secretKey'], $Method, $config);
  88. $url = $config['url'];
  89. $ch = curl_init($url);
  90. if(array_key_exists('proxy', $config)){
  91. $config['proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['proxy']);
  92. }
  93. curl_setopt($ch, CURLOPT_HEADER, 0);
  94. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
  95. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  97. curl_setopt($ch, CURLOPT_POST, 1);
  98. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json2str($params));
  99. $result = curl_exec($ch);
  100. if(curl_errno($ch)) $result = curl_error($ch);
  101. curl_close($ch);
  102. $result = json_decode($result, 1);
  103. if (isset($result['Response'])) {
  104. $result = $result['Response'];
  105. $result['startTime'] = $result['ExpiredTime'] - $config['durationSeconds'];
  106. }
  107. $result = $this->backwardCompat($result);
  108. return $result;
  109. }
  110. // get policy
  111. function getPolicy($scopes){
  112. if (!is_array($scopes)){
  113. return null;
  114. }
  115. $statements = array();
  116. for($i=0, $counts=count($scopes); $i < $counts; $i++){
  117. $actions=array();
  118. $resources = array();
  119. array_push($actions, $scopes[$i]->get_action());
  120. array_push($resources, $scopes[$i]->get_resource());
  121. $principal = array(
  122. 'qcs' => array('*')
  123. );
  124. $statement = array(
  125. 'actions' => $actions,
  126. 'effect' => 'allow',
  127. 'principal' => $principal,
  128. 'resource' => $resources
  129. );
  130. array_push($statements, $statement);
  131. }
  132. $policy = array(
  133. 'version' => '2.0',
  134. 'statement' => $statements
  135. );
  136. return $policy;
  137. }
  138. }
  139. class Scope{
  140. var $action;
  141. var $bucket;
  142. var $region;
  143. var $resourcePrefix;
  144. function __construct($action, $bucket, $region, $resourcePrefix){
  145. $this->action = $action;
  146. $this->bucket = $bucket;
  147. $this->region = $region;
  148. $this->resourcePrefix = $resourcePrefix;
  149. }
  150. function get_action(){
  151. return $this->action;
  152. }
  153. function get_resource(){
  154. $index = strripos($this->bucket, '-');
  155. $bucketName = substr($this->bucket, 0, $index);
  156. $appid = substr($this->bucket, $index + 1);
  157. if(!(strpos($this->resourcePrefix, '/') === 0)){
  158. $this->resourcePrefix = '/' . $this->resourcePrefix;
  159. }
  160. return 'qcs::cos:' . $this->region . ':uid/' . $appid . ':prefix//' . $appid . '/' . $bucketName . $this->resourcePrefix;
  161. }
  162. }
  163. ?>