Skip to content

Commit

Permalink
支持 STS Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Dec 6, 2018
1 parent e84677f commit e527581
Show file tree
Hide file tree
Showing 9 changed files with 594 additions and 294 deletions.
45 changes: 38 additions & 7 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var util = {
var cos = new COS({
getAuthorization: function (options,callback) {


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


// // 方法二、后端使用固定密钥计算签名,返回给前端,auth.php,注意:后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
// // 方法二、【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
// var url = 'http://127.0.0.1:3000/sts-scope';
// var xhr = new XMLHttpRequest();
// xhr.open('POST', url, true);
// xhr.setRequestHeader('Content-Type', 'application/json');
// xhr.onload = function (e) {
// try {
// var data = JSON.parse(e.target.responseText);
// var credentials = data.credentials;
// } catch (e) {
// }
// callback({
// TmpSecretId: credentials.tmpSecretId,
// TmpSecretKey: credentials.tmpSecretKey,
// XCosSecurityToken: credentials.sessionToken,
// ExpiredTime: data.expiredTime,
// ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
// });
// };
// xhr.send(JSON.stringify(options.Scope));


// // 方法三、后端使用固定密钥计算签名,返回给前端,auth.php,注意:后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
// var method = (options.Method || 'get').toLowerCase();
// var key = options.Key || '';
// var query = options.Query || {};
Expand All @@ -58,12 +82,15 @@ var cos = new COS({
// xhr.open('POST', url, true);
// xhr.setRequestHeader('content-type', 'application/json');
// xhr.onload = function (e) {
// callback({ Authorization: e.target.responseText, });
// callback({
// Authorization: e.target.responseText,
// // XCosSecurityToken: sessionToken, // 如果使用临时密钥,需要传 sessionToken
// });
// };
// xhr.send(JSON.stringify(data));


// // 方法三、前端使用固定密钥计算签名(适用于前端调试)
// // 方法四、前端使用固定密钥计算签名(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
Expand All @@ -73,7 +100,11 @@ var cos = new COS({
// Headers: options.Headers,
// Expires: 60,
// });
// callback(authorization);
// callback({
// Authorization: e.target.responseText,
// // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
// });

}
});

Expand Down
Loading

0 comments on commit e527581

Please sign in to comment.