Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/1.2.0 #69

Merged
merged 11 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 89 additions & 40 deletions demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,32 @@ var isCIHost = function isCIHost(url) {
);
};

var error = function error(err, opt) {
var sourceErr = err;
err.message = err.message || null;

if (typeof opt === 'string') {
err.error = opt;
err.message = opt;
} else if ((typeof opt === 'undefined' ? 'undefined' : _typeof(opt)) === 'object' && opt !== null) {
extend(err, opt);
if (opt.code || opt.name) err.code = opt.code || opt.name;
if (opt.message) err.message = opt.message;
if (opt.stack) err.stack = opt.stack;
}

if (typeof Object.defineProperty === 'function') {
Object.defineProperty(err, 'name', { writable: true, enumerable: false });
Object.defineProperty(err, 'message', { enumerable: true });
}

err.name = opt && opt.name || err.name || err.code || 'Error';
if (!err.code) err.code = err.name;
if (!err.error) err.error = clone(sourceErr); // 兼容老的错误格式

return err;
};

var util = {
noop: noop,
formatParams: formatParams,
Expand Down Expand Up @@ -676,7 +702,8 @@ var util = {
getAuth: getAuth,
compareVersion: compareVersion,
canFileSlice: canFileSlice,
isCIHost: isCIHost
isCIHost: isCIHost,
error: error
};

module.exports = util;
Expand Down Expand Up @@ -2722,7 +2749,7 @@ var advance = __webpack_require__(27);
var defaultOptions = {
SecretId: '',
SecretKey: '',
XCosSecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
ChunkRetryTimes: 2,
FileParallelLimit: 3,
ChunkParallelLimit: 3,
Expand Down Expand Up @@ -2784,7 +2811,7 @@ COS.util = {
json2xml: util.json2xml
};
COS.getAuthorization = util.getAuth;
COS.version = '1.1.8';
COS.version = '1.2.0';

module.exports = COS;

Expand Down Expand Up @@ -8152,6 +8179,7 @@ function postObject(params, callback) {
headers: headers,
qs: params.Query,
filePath: filePath,
TaskId: params.TaskId,
onProgress: onProgress
}, function (err, data) {
onProgress(null, true);
Expand Down Expand Up @@ -9164,7 +9192,7 @@ function getObjectUrl(params, callback) {

var signUrl = url;
signUrl += '?' + (AuthData.Authorization.indexOf('q-signature') > -1 ? replaceUrlParamList(AuthData.Authorization) : 'sign=' + encodeURIComponent(AuthData.Authorization));
AuthData.XCosSecurityToken && (signUrl += '&x-cos-security-token=' + AuthData.XCosSecurityToken);
AuthData.SecurityToken && (signUrl += '&x-cos-security-token=' + AuthData.SecurityToken);
AuthData.ClientIP && (signUrl += '&clientIP=' + AuthData.ClientIP);
AuthData.ClientUA && (signUrl += '&clientUA=' + AuthData.ClientUA);
AuthData.Token && (signUrl += '&token=' + AuthData.Token);
Expand All @@ -9175,7 +9203,7 @@ function getObjectUrl(params, callback) {
});

if (AuthData) {
syncUrl += '?' + AuthData.Authorization + (AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '');
syncUrl += '?' + AuthData.Authorization + (AuthData.SecurityToken ? '&x-cos-security-token=' + AuthData.SecurityToken : '');
queryParamsStr && (syncUrl += '&' + queryParamsStr);
} else {
queryParamsStr && (syncUrl += '?' + queryParamsStr);
Expand Down Expand Up @@ -9309,7 +9337,6 @@ var getSignHost = function getSignHost(opt) {

// 异步获取签名
function getAuthorizationAsync(params, callback) {

var headers = util.clone(params.Headers);
var headerHost = '';
util.each(headers, function (v, k) {
Expand All @@ -9322,30 +9349,17 @@ function getAuthorizationAsync(params, callback) {
// Host 加入签名计算
if (!headerHost && params.SignHost && forceSignHost) headers.Host = params.SignHost;

var cb = function cb(AuthData) {

// 检查签名格式
var formatAllow = false;
var auth = AuthData.Authorization;
if (auth) {
if (auth.indexOf(' ') > -1) {
formatAllow = false;
} else if (auth.indexOf('q-sign-algorithm=') > -1 && auth.indexOf('q-ak=') > -1 && auth.indexOf('q-sign-time=') > -1 && auth.indexOf('q-key-time=') > -1 && auth.indexOf('q-url-param-list=') > -1) {
formatAllow = true;
} else {
try {
auth = base64.atob(auth);
if (auth.indexOf('a=') > -1 && auth.indexOf('k=') > -1 && auth.indexOf('t=') > -1 && auth.indexOf('r=') > -1 && auth.indexOf('b=') > -1) {
formatAllow = true;
}
} catch (e) {}
}
}
if (formatAllow) {
callback && callback(null, AuthData);
} else {
callback && callback('authorization error');
}
// 获取凭证的回调,避免用户 callback 多次
var cbDone = false;
var cb = function cb(err, AuthData) {
if (cbDone) return;
cbDone = true;
if (AuthData && AuthData.XCosSecurityToken && !AuthData.SecurityToken) {
AuthData = util.clone(AuthData);
AuthData.SecurityToken = AuthData.XCosSecurityToken;
delete AuthData.XCosSecurityToken;
}
callback && callback(err, AuthData);
};

var self = this;
Expand Down Expand Up @@ -9413,12 +9427,43 @@ function getAuthorizationAsync(params, callback) {
});
var AuthData = {
Authorization: Authorization,
XCosSecurityToken: StsData.XCosSecurityToken || '',
SecurityToken: StsData.SecurityToken || StsData.XCosSecurityToken || '',
Token: StsData.Token || '',
ClientIP: StsData.ClientIP || '',
ClientUA: StsData.ClientUA || ''
};
cb(AuthData);
cb(null, AuthData);
};

var checkAuthError = function checkAuthError(AuthData) {
if (AuthData.Authorization) {
// 检查签名格式
var formatAllow = false;
var auth = AuthData.Authorization;
if (auth) {
if (auth.indexOf(' ') > -1) {
formatAllow = false;
} else if (auth.indexOf('q-sign-algorithm=') > -1 && auth.indexOf('q-ak=') > -1 && auth.indexOf('q-sign-time=') > -1 && auth.indexOf('q-key-time=') > -1 && auth.indexOf('q-url-param-list=') > -1) {
formatAllow = true;
} else {
try {
auth = atob(auth);
if (auth.indexOf('a=') > -1 && auth.indexOf('k=') > -1 && auth.indexOf('t=') > -1 && auth.indexOf('r=') > -1 && auth.indexOf('b=') > -1) {
formatAllow = true;
}
} catch (e) {}
}
}
if (!formatAllow) return util.error(new Error('getAuthorization callback params format error'));
} else {
if (!AuthData.TmpSecretId) return util.error(new Error('getAuthorization callback params missing "TmpSecretId"'));
if (!AuthData.TmpSecretKey) return util.error(new Error('getAuthorization callback params missing "TmpSecretKey"'));
if (!AuthData.SecurityToken && !AuthData.XCosSecurityToken) return util.error(new Error('getAuthorization callback params missing "SecurityToken"'));
if (!AuthData.ExpiredTime) return util.error(new Error('getAuthorization callback params missing "ExpiredTime"'));
if (AuthData.ExpiredTime && AuthData.ExpiredTime.toString().length !== 10) return util.error(new Error('getAuthorization callback params "ExpiredTime" should be 10 digits'));
if (AuthData.StartTime && AuthData.StartTime.toString().length !== 10) return util.error(new Error('getAuthorization callback params "StartTime" should be 10 StartTime'));
}
return false;
};

// 先判断是否有临时密钥
Expand All @@ -9442,14 +9487,16 @@ function getAuthorizationAsync(params, callback) {
if (typeof AuthData === 'string') {
AuthData = { Authorization: AuthData };
}
if (AuthData.TmpSecretId && AuthData.TmpSecretKey && AuthData.XCosSecurityToken && AuthData.ExpiredTime) {
var AuthError = checkAuthError(AuthData);
if (AuthError) return cb(AuthError);
if (AuthData.Authorization) {
cb(null, AuthData);
} else {
StsData = AuthData || {};
StsData.Scope = Scope;
StsData.ScopeKey = ScopeKey;
self._StsCache.push(StsData);
calcAuthByTmpKey();
} else {
cb(AuthData);
}
});
} else if (self.options.getSTS) {
Expand All @@ -9461,8 +9508,10 @@ function getAuthorizationAsync(params, callback) {
StsData = data || {};
StsData.Scope = Scope;
StsData.ScopeKey = ScopeKey;
StsData.TmpSecretId = StsData.SecretId;
StsData.TmpSecretKey = StsData.SecretKey;
if (!StsData.TmpSecretId) StsData.TmpSecretId = StsData.SecretId;
if (!StsData.TmpSecretKey) StsData.TmpSecretKey = StsData.SecretKey;
var AuthError = checkAuthError(StsData);
if (AuthError) return cb(AuthError);
self._StsCache.push(StsData);
calcAuthByTmpKey();
});
Expand All @@ -9482,9 +9531,9 @@ function getAuthorizationAsync(params, callback) {
});
var AuthData = {
Authorization: Authorization,
XCosSecurityToken: self.options.XCosSecurityToken
SecurityToken: self.options.SecurityToken || self.options.XCosSecurityToken
};
cb(AuthData);
cb(null, AuthData);
return AuthData;
}();
}
Expand Down Expand Up @@ -9637,7 +9686,7 @@ function _submitRequest(params, callback) {
params.AuthData.Token && (opt.headers['token'] = params.AuthData.Token);
params.AuthData.ClientIP && (opt.headers['clientIP'] = params.AuthData.ClientIP);
params.AuthData.ClientUA && (opt.headers['clientUA'] = params.AuthData.ClientUA);
params.AuthData.XCosSecurityToken && (opt.headers[token] = params.AuthData.XCosSecurityToken);
params.AuthData.SecurityToken && (opt.headers[token] = params.AuthData.SecurityToken);

// 清理 undefined 和 null 字段
opt.headers && (opt.headers = util.clearKey(opt.headers));
Expand Down
2 changes: 1 addition & 1 deletion demo-album/lib/cos-wx-sdk-v5.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion demo/demo-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var getAuthorization = function(options, callback) {
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
});
Expand Down Expand Up @@ -159,7 +159,11 @@ var toolsDao = {
Region: config.Region,
Key: file.name,
FilePath: file.path,
FileSize: file.size,
SliceSize: 1024 * 1024 * 5, // 文件大于5mb自动使用分块上传
onTaskReady: function(taskId) {
TaskId = taskId
},
onProgress: function (info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
Expand Down
Loading