Skip to content

Commit

Permalink
实例化支持 XCosSecurityToken 参数
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Nov 11, 2018
1 parent 5fd0e3c commit cb6870f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 108 deletions.
118 changes: 27 additions & 91 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,94 +17,24 @@ var util = {
}
};

var cos = new COS({
getAuthorization: function (options,callback) {
// 方法一、后端通过获取临时密钥给到前端,前端计算签名
// var url = 'http://127.0.0.1:3000/sts';
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
try {
var data = JSON.parse(e.target.responseText);
} catch (e) {
}
callback({
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
ExpiredTime: data.expiredTime,
});
};
xhr.send();


// // 方法二、后端通过获取临时密钥,并计算好签名给到前端
// var method = (options.Method || 'get').toLowerCase();
// var key = options.Key || '';
// var query = options.Query || {};
// var headers = options.Headers || {};
// var pathname = key.indexOf('/') === 0 ? key : '/' + key;
// // var url = 'http://127.0.0.1:3000/sts-auth';
// var url = '../server/sts-auth.php';
// var xhr = new XMLHttpRequest();
// var data = {
// method: method,
// pathname: pathname,
// query: query,
// headers: headers,
// };
// xhr.open('POST', url, true);
// xhr.setRequestHeader('content-type', 'application/json');
// xhr.onload = function (e) {
// try {
// var AuthData = JSON.parse(e.target.responseText);
// } catch (e) {
// }
// callback({
// Authorization: AuthData.Authorization,
// XCosSecurityToken: AuthData.XCosSecurityToken,
// });
// };
// xhr.send(JSON.stringify(data));


// // 方法三、后端使用固定密钥计算签名,返回给前端,auth.php,注意:后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
// var method = (options.Method || 'get').toLowerCase();
// var key = options.Key || '';
// var query = options.Query || {};
// var headers = options.Headers || {};
// var pathname = key.indexOf('/') === 0 ? key : '/' + key;
// // var url = 'http://127.0.0.1:3000/auth';
// var url = '../server/auth.php';
// var xhr = new XMLHttpRequest();
// var data = {
// method: method,
// pathname: pathname,
// query: query,
// headers: headers,
// };
// xhr.open('POST', url, true);
// xhr.setRequestHeader('content-type', 'application/json');
// xhr.onload = function (e) {
// callback({ Authorization: e.target.responseText, });
// };
// xhr.send(JSON.stringify(data));


// // 方法四、前端使用固定密钥计算签名(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// Method: options.Method,
// Key: options.Key,
// Query: options.Query,
// Headers: options.Headers,
// Expires: 60,
// });
// callback(authorization);
}
});

var cos = new COS();

(function () {
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
try {
var data = JSON.parse(e.target.responseText);
} catch (e) {
}
cos.options.SecretId = data.credentials && data.credentials.tmpSecretId;
cos.options.SecretKey = data.credentials && data.credentials.tmpSecretKey;
cos.options.XCosSecurityToken = data.credentials && data.credentials.sessionToken;
};
xhr.send();
})();

var TaskId;

Expand Down Expand Up @@ -157,9 +87,15 @@ function getAuth() {
cos.options.getAuthorization({
Method: 'get',
Key: key
}, function (auth) {
// 注意:这里的 Bucket 格式是 test-1250000000
logger.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + encodeURIComponent(key).replace(/%2F/g, '/') + '?sign=' + encodeURIComponent(auth));
}, function (AuthData) {
if (typeof AuthData === 'string') {
AuthData = {Authorization: AuthData.Authorization};
}
var url = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
encodeURIComponent(key).replace(/%2F/g, '/') +
'?' + AuthData +
(AuthData.XCosSecurityToken ? '&' + AuthData.XCosSecurityToken : '');
logger.log(url);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "0.4.21",
"version": "0.4.22",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down
37 changes: 22 additions & 15 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ function getObjectUrl(params, callback) {
callback(null, {Url: url});
return url;
}
var authorization = getAuthorizationAsync.call(this, {
var AuthData = getAuthorizationAsync.call(this, {
Bucket: params.Bucket || '',
Region: params.Region || '',
Method: params.Method || 'get',
Expand All @@ -1691,7 +1691,7 @@ function getObjectUrl(params, callback) {
}, function (AuthData) {
if (!callback) return;
var signUrl = url;
signUrl += '?sign=' + encodeURIComponent(AuthData.Authorization);
signUrl += '?' + AuthData.Authorization;
AuthData.XCosSecurityToken && (signUrl += '&x-cos-security-token=' + AuthData.XCosSecurityToken);
AuthData.ClientIP && (signUrl += '&clientIP=' + AuthData.ClientIP);
AuthData.ClientUA && (signUrl += '&clientUA=' + AuthData.ClientUA);
Expand All @@ -1700,8 +1700,9 @@ function getObjectUrl(params, callback) {
callback(null, {Url: signUrl});
});
});
if (authorization) {
return url + '?sign=' + encodeURIComponent(authorization);
if (AuthData) {
return url + '?' + AuthData.Authorization +
(AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '');
} else {
return url;
}
Expand Down Expand Up @@ -1890,17 +1891,23 @@ function getAuthorizationAsync(params, callback) {
calcAuthByTmpKey();
});
} else { // 内部计算获取签名
var Authorization = util.getAuth({
SecretId: params.SecretId || self.options.SecretId,
SecretKey: params.SecretKey || self.options.SecretKey,
Method: params.Method,
Key: PathName,
Query: params.Query,
Headers: params.Headers,
Expires: params.Expires,
});
callback && callback({Authorization: Authorization});
return Authorization;
return (function () {
var Authorization = util.getAuth({
SecretId: params.SecretId || self.options.SecretId,
SecretKey: params.SecretKey || self.options.SecretKey,
Method: params.Method,
Key: PathName,
Query: params.Query,
Headers: params.Headers,
Expires: params.Expires,
});
var AuthData = {
Authorization: Authorization,
XCosSecurityToken: self.options.XCosSecurityToken,
};
callback && callback(AuthData);
return AuthData;
})();
}
return '';
}
Expand Down
3 changes: 2 additions & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var defaultOptions = {
Protocol: '',
CompatibilityMode: false,
ForcePathStyle: false,
XCosSecurityToken: '',
UploadIdCacheLimit: 50,
};

Expand All @@ -49,6 +50,6 @@ util.extend(COS.prototype, base);
util.extend(COS.prototype, advance);

COS.getAuthorization = util.getAuth;
COS.version = '0.4.21';
COS.version = '0.4.22';

module.exports = COS;

0 comments on commit cb6870f

Please sign in to comment.