Skip to content

Commit

Permalink
支持stst StartTime 自动修正时间
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Feb 2, 2020
1 parent 36d0920 commit c7ce625
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var getAuthorization = function (options, callback) {
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
StartTime: data.startTime, // 密钥申请时服务器时间
ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
});
};
Expand All @@ -69,6 +70,7 @@ var getAuthorization = function (options, callback) {
// TmpSecretId: credentials.tmpSecretId,
// TmpSecretKey: credentials.tmpSecretKey,
// XCosSecurityToken: credentials.sessionToken,
// StartTime: data.startTime, // 密钥申请时服务器时间
// ExpiredTime: data.expiredTime,
// ScopeLimit: true, // 细粒度控制权限需要设为 true,会限制密钥只在相同请求时重复使用
// });
Expand Down
2 changes: 1 addition & 1 deletion demo/sts-put.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>Ajax Put 上传</h1>

// 计算签名
var getAuthorization = function (options, callback) {
// var url = 'http://127.0.0.1:3000/sts-auth' +
// var url = 'http://127.0.0.1:3000/sts-auth';
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
Expand Down
18 changes: 14 additions & 4 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var getAuth = function (opt) {

var SecretId = opt.SecretId;
var SecretKey = opt.SecretKey;
var KeyTime = opt.KeyTime;
var method = (opt.method || opt.Method || 'get').toLowerCase();
var queryParams = clone(opt.Query || opt.params || {});
var headers = clone(opt.Headers || opt.headers || {});
Expand Down Expand Up @@ -154,8 +155,8 @@ var getAuth = function (opt) {
// 要用到的 Authorization 参数列表
var qSignAlgorithm = 'sha1';
var qAk = SecretId;
var qSignTime = now + ';' + exp;
var qKeyTime = now + ';' + exp;
var qSignTime = KeyTime || now + ';' + exp;
var qKeyTime = KeyTime || now + ';' + exp;
var qHeaderList = getObjectKeys(headers).join(';').toLowerCase();
var qUrlParamList = getObjectKeys(queryParams).join(';').toLowerCase();

Expand Down Expand Up @@ -276,6 +277,10 @@ function isInArray(arr, item) {
return flag;
}

function makeArray(arr) {
return isArray(arr) ? arr : [arr];
}

function each(obj, fn) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
Expand Down Expand Up @@ -561,6 +566,7 @@ var util = {
extend: extend,
isArray: isArray,
isInArray: isInArray,
makeArray: makeArray,
each: each,
map: map,
filter: filter,
Expand Down Expand Up @@ -2003,7 +2009,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.22';
COS.version = '0.5.23';

module.exports = COS;

Expand Down Expand Up @@ -4175,6 +4181,7 @@ function putBucketAcl(params, callback) {
* @return {Object} data.AccessControlPolicy 访问权限信息
*/
function getBucketAcl(params, callback) {

submitRequest.call(this, {
Action: 'name/cos:GetBucketACL',
method: 'GET',
Expand Down Expand Up @@ -4526,6 +4533,7 @@ function putBucketTagging(params, callback) {
* @return {Object} data 返回数据
*/
function getBucketTagging(params, callback) {

submitRequest.call(this, {
Action: 'name/cos:GetBucketTagging',
method: 'GET',
Expand Down Expand Up @@ -6163,6 +6171,7 @@ function getAuthorizationAsync(params, callback) {
})();

var calcAuthByTmpKey = function () {
var KeyTime = StsData.StartTime && StsData.ExpiredTime ? StsData.StartTime + ';' + StsData.ExpiredTime : '';
var Authorization = util.getAuth({
SecretId: StsData.TmpSecretId,
SecretKey: StsData.TmpSecretKey,
Expand All @@ -6172,7 +6181,8 @@ function getAuthorizationAsync(params, callback) {
Headers: headers,
Expires: params.Expires,
UseRawKey: self.options.UseRawKey,
SystemClockOffset: self.options.SystemClockOffset
SystemClockOffset: self.options.SystemClockOffset,
KeyTime: KeyTime
});
var AuthData = {
Authorization: Authorization,
Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "cos-js-sdk-v5",
"version": "0.5.22",
"version": "0.5.23",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"scripts": {
"sts.js": "node server/sts.js",
"dev": "cross-env NODE_ENV=development webpack -w",
"server": "node server/sts.js",
"build": "cross-env NODE_ENV=production webpack",
"cos-auth.min.js": "uglifyjs ./demo/common/cos-auth.js -o ./demo/common/cos-auth.min.js -c -m"
},
Expand Down
4 changes: 4 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function putBucketAcl(params, callback) {
* @return {Object} data.AccessControlPolicy 访问权限信息
*/
function getBucketAcl(params, callback) {

submitRequest.call(this, {
Action: 'name/cos:GetBucketACL',
method: 'GET',
Expand Down Expand Up @@ -530,6 +531,7 @@ function putBucketTagging(params, callback) {
* @return {Object} data 返回数据
*/
function getBucketTagging(params, callback) {

submitRequest.call(this, {
Action: 'name/cos:GetBucketTagging',
method: 'GET',
Expand Down Expand Up @@ -2191,6 +2193,7 @@ function getAuthorizationAsync(params, callback) {
})();

var calcAuthByTmpKey = function () {
var KeyTime = StsData.StartTime && StsData.ExpiredTime ? StsData.StartTime + ';' + StsData.ExpiredTime : '';
var Authorization = util.getAuth({
SecretId: StsData.TmpSecretId,
SecretKey: StsData.TmpSecretKey,
Expand All @@ -2201,6 +2204,7 @@ function getAuthorizationAsync(params, callback) {
Expires: params.Expires,
UseRawKey: self.options.UseRawKey,
SystemClockOffset: self.options.SystemClockOffset,
KeyTime: KeyTime
});
var AuthData = {
Authorization: Authorization,
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '0.5.22';
COS.version = '0.5.23';

module.exports = COS;
10 changes: 8 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var getAuth = function (opt) {

var SecretId = opt.SecretId;
var SecretKey = opt.SecretKey;
var KeyTime = opt.KeyTime;
var method = (opt.method || opt.Method || 'get').toLowerCase();
var queryParams = clone(opt.Query || opt.params || {});
var headers = clone(opt.Headers || opt.headers || {});
Expand Down Expand Up @@ -79,8 +80,8 @@ var getAuth = function (opt) {
// 要用到的 Authorization 参数列表
var qSignAlgorithm = 'sha1';
var qAk = SecretId;
var qSignTime = now + ';' + exp;
var qKeyTime = now + ';' + exp;
var qSignTime = KeyTime || now + ';' + exp;
var qKeyTime = KeyTime || now + ';' + exp;
var qHeaderList = getObjectKeys(headers).join(';').toLowerCase();
var qUrlParamList = getObjectKeys(queryParams).join(';').toLowerCase();

Expand Down Expand Up @@ -211,6 +212,10 @@ function isInArray(arr, item) {
return flag;
}

function makeArray(arr) {
return isArray(arr) ? arr : [arr];
}

function each(obj, fn) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
Expand Down Expand Up @@ -494,6 +499,7 @@ var util = {
extend: extend,
isArray: isArray,
isInArray: isInArray,
makeArray: makeArray,
each: each,
map: map,
filter: filter,
Expand Down

0 comments on commit c7ce625

Please sign in to comment.