Sts.php 6.3 KB

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