Skip to content

Commit e527581

Browse files
committed
支持 STS Scope
1 parent e84677f commit e527581

File tree

9 files changed

+594
-294
lines changed

9 files changed

+594
-294
lines changed

demo/demo.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var util = {
2020
var cos = new COS({
2121
getAuthorization: function (options,callback) {
2222

23+
2324
// 方法一、后端通过获取临时密钥给到前端,前端计算签名
2425
// var url = 'http://127.0.0.1:3000/sts';
2526
var url = '../server/sts.php';
@@ -28,19 +29,42 @@ var cos = new COS({
2829
xhr.onload = function (e) {
2930
try {
3031
var data = JSON.parse(e.target.responseText);
32+
var credentials = data.credentials;
3133
} catch (e) {
3234
}
3335
callback({
34-
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
35-
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
36-
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
36+
TmpSecretId: credentials.tmpSecretId,
37+
TmpSecretKey: credentials.tmpSecretKey,
38+
XCosSecurityToken: credentials.sessionToken,
3739
ExpiredTime: data.expiredTime,
3840
});
3941
};
4042
xhr.send();
4143

4244

43-
// // 方法二、后端使用固定密钥计算签名,返回给前端,auth.php,注意:后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
45+
// // 方法二、【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
46+
// var url = 'http://127.0.0.1:3000/sts-scope';
47+
// var xhr = new XMLHttpRequest();
48+
// xhr.open('POST', url, true);
49+
// xhr.setRequestHeader('Content-Type', 'application/json');
50+
// xhr.onload = function (e) {
51+
// try {
52+
// var data = JSON.parse(e.target.responseText);
53+
// var credentials = data.credentials;
54+
// } catch (e) {
55+
// }
56+
// callback({
57+
// TmpSecretId: credentials.tmpSecretId,
58+
// TmpSecretKey: credentials.tmpSecretKey,
59+
// XCosSecurityToken: credentials.sessionToken,
60+
// ExpiredTime: data.expiredTime,
61+
// ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
62+
// });
63+
// };
64+
// xhr.send(JSON.stringify(options.Scope));
65+
66+
67+
// // 方法三、后端使用固定密钥计算签名,返回给前端,auth.php,注意:后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
4468
// var method = (options.Method || 'get').toLowerCase();
4569
// var key = options.Key || '';
4670
// var query = options.Query || {};
@@ -58,12 +82,15 @@ var cos = new COS({
5882
// xhr.open('POST', url, true);
5983
// xhr.setRequestHeader('content-type', 'application/json');
6084
// xhr.onload = function (e) {
61-
// callback({ Authorization: e.target.responseText, });
85+
// callback({
86+
// Authorization: e.target.responseText,
87+
// // XCosSecurityToken: sessionToken, // 如果使用临时密钥,需要传 sessionToken
88+
// });
6289
// };
6390
// xhr.send(JSON.stringify(data));
6491

6592

66-
// // 方法三、前端使用固定密钥计算签名(适用于前端调试)
93+
// // 方法四、前端使用固定密钥计算签名(适用于前端调试)
6794
// var authorization = COS.getAuthorization({
6895
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
6996
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
@@ -73,7 +100,11 @@ var cos = new COS({
73100
// Headers: options.Headers,
74101
// Expires: 60,
75102
// });
76-
// callback(authorization);
103+
// callback({
104+
// Authorization: e.target.responseText,
105+
// // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
106+
// });
107+
77108
}
78109
});
79110

0 commit comments

Comments
 (0)