Skip to content

Commit

Permalink
修改 json2str 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed May 3, 2018
1 parent c71a380 commit d95cc06
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
7 changes: 3 additions & 4 deletions server/sts-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ var util = {
getRandom: function (min, max) {
return Math.round(Math.random() * (max - min) + min);
},
// json 转 query string
json2str: function (obj, notEncode) {
// obj 转 query string
json2str: function (obj) {
var arr = [];
Object.keys(obj).sort().forEach(function (item) {
var val = obj[item] || '';
!notEncode && (val = val);
arr.push(item + '=' + val);
});
return arr.join('&');
},
// 计算签名
getSignature: function (opt, key, method) {
var formatString = method + config.Domain + '/v2/index.php?' + util.json2str(opt, 1);
var formatString = method + config.Domain + '/v2/index.php?' + util.json2str(opt);
var hmac = crypto.createHmac('sha1', key);
var sign = hmac.update(new Buffer(formatString, 'utf8')).digest('base64');
return sign;
Expand Down
21 changes: 14 additions & 7 deletions server/sts-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
);
}

// json 转 query string
function json2str($obj, $notEncode = false) {
// obj 转 query string
function json2str($obj) {
ksort($obj);
$arr = array();
foreach ($obj as $key => $val) {
!$notEncode && ($val = urlencode($val));
array_push($arr, $key . '=' . $val);
}
return join('&', $arr);
Expand All @@ -36,7 +35,7 @@ function json2str($obj, $notEncode = false) {
// 计算临时密钥用的签名
function getSignature($opt, $key, $method) {
global $config;
$formatString = $method . $config['Domain'] . '/v2/index.php?' . json2str($opt, 1);
$formatString = $method . $config['Domain'] . '/v2/index.php?' . json2str($opt);
$sign = hash_hmac('sha1', $formatString, $key);
$sign = base64_encode(hex2bin($sign));
return $sign;
Expand Down Expand Up @@ -120,6 +119,13 @@ function getTempKeys() {
);

$policyStr = str_replace('\\/', '/', json_encode($policy));

// 有效时间小于 30 秒就重新获取临时密钥,否则使用缓存的临时密钥
if (isset($_SESSION['tempKeysCache']) && isset($_SESSION['tempKeysCache']['expiredTime']) && isset($_SESSION['tempKeysCache']['policyStr']) &&
$_SESSION['tempKeysCache']['expiredTime'] - time() > 30 && $_SESSION['tempKeysCache']['policyStr'] === $policyStr) {
return $_SESSION['tempKeysCache'];
}

$Action = 'GetFederationToken';
$Nonce = rand(10000, 20000);
$Timestamp = time() - 1;
Expand All @@ -137,7 +143,7 @@ function getTempKeys() {
);
$params['Signature'] = urlencode(getSignature($params, $config['SecretKey'], $Method));

$url = $config['Url'] . '?' . json2str($params, 1);
$url = $config['Url'] . '?' . json2str($params);
$ch = curl_init($url);
$config['Proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['Proxy']);
curl_setopt($ch, CURLOPT_HEADER, 0);
Expand All @@ -148,10 +154,11 @@ function getTempKeys() {
curl_close($ch);

$result = json_decode($result, 1);
$_SESSION['tempKeysCache'] = $result['data'];
if (isset($result['data'])) $result = $result['data'];
$_SESSION['tempKeysCache'] = $result;
$_SESSION['tempKeysCache']['policyStr'] = $policyStr;

return $result['data'];
return $result;
};

// 计算 COS API 请求用的签名
Expand Down
11 changes: 5 additions & 6 deletions server/sts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ var util = {
getRandom: function (min, max) {
return Math.round(Math.random() * (max - min) + min);
},
// json 转 query string
json2str: function (obj, notEncode) {
// obj 转 query string
json2str: function (obj) {
var arr = [];
Object.keys(obj).sort().forEach(function (item) {
var val = obj[item] || '';
!notEncode && (val = val);
arr.push(item + '=' + val);
});
return arr.join('&');
},
// 计算签名
getSignature: function (opt, key, method) {
var formatString = method + config.Domain + '/v2/index.php?' + util.json2str(opt, 1);
var formatString = method + config.Domain + '/v2/index.php?' + util.json2str(opt);
var hmac = crypto.createHmac('sha1', key);
var sign = hmac.update(new Buffer(formatString, 'utf8')).digest('base64');
return sign;
Expand Down Expand Up @@ -146,8 +145,8 @@ var getTempKeys = function (callback) {
};
request(opt, function (err, response, body) {
body = body && JSON.parse(body);
var data = body.data;
callback(err, data);
if (body.data) body = body.data;
callback(err, body);
});
};

Expand Down
14 changes: 7 additions & 7 deletions server/sts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
'AllowPrefix' => '_ALLOW_DIR_/*', // 必填,这里改成允许的路径前缀,这里可以根据自己网站的用户登录态判断允许上传的目录,例子:* 或者 a/* 或者 a.jpg
);

// json 转 query string
function json2str($obj, $notEncode = false) {
// obj 转 query string
function json2str($obj) {
ksort($obj);
$arr = array();
foreach ($obj as $key => $val) {
!$notEncode && ($val = urlencode($val));
array_push($arr, $key . '=' . $val);
}
return join('&', $arr);
Expand All @@ -27,7 +26,7 @@ function json2str($obj, $notEncode = false) {
// 计算临时密钥用的签名
function getSignature($opt, $key, $method) {
global $config;
$formatString = $method . $config['Domain'] . '/v2/index.php?' . json2str($opt, 1);
$formatString = $method . $config['Domain'] . '/v2/index.php?' . json2str($opt);
$sign = hash_hmac('sha1', $formatString, $key);
$sign = base64_encode(hex2bin($sign));
return $sign;
Expand All @@ -51,7 +50,7 @@ function getTempKeys() {
array(
'action'=> array(
// // 这里可以从临时密钥的权限上控制前端允许的操作
'name/cos:*', // 这样写可以包含下面所有权限
// 'name/cos:*', // 这样写可以包含下面所有权限

// // 列出所有允许的操作
// // ACL 读写
Expand Down Expand Up @@ -128,7 +127,7 @@ function getTempKeys() {
);
$params['Signature'] = urlencode(getSignature($params, $config['SecretKey'], $Method));

$url = $config['Url'] . '?' . json2str($params, 1);
$url = $config['Url'] . '?' . json2str($params);
$ch = curl_init($url);
$config['Proxy'] && curl_setopt($ch, CURLOPT_PROXY, $config['Proxy']);
curl_setopt($ch, CURLOPT_HEADER, 0);
Expand All @@ -139,8 +138,9 @@ function getTempKeys() {
curl_close($ch);

$result = json_decode($result, 1);
if (isset($result['data'])) $result = $result['data'];

return $result['data'];
return $result;
};

// 获取临时密钥,计算签名
Expand Down

0 comments on commit d95cc06

Please sign in to comment.