Skip to content

Commit 41bcd86

Browse files
committed
Merge pull request #232 from wangxiao/new-ajax
[feat] 支持短 header 及签名认证。
2 parents c2c27d1 + 1743e0a commit 41bcd86

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"dependencies": {
1414
"localstorage-memory": "^1.0.1",
15+
"md5": "^2.0.0",
1516
"qiniu": "6.1.3",
1617
"underscore": "^1.8.3"
1718
},
@@ -42,16 +43,15 @@
4243
},
4344
"license": "MIT",
4445
"author": {
45-
"name": "dennis zhuang",
46-
"email": "[email protected]"
46+
"name": "LeanCloud",
47+
"email": "[email protected]"
4748
},
4849
"browser": {
4950
"react-native": false,
5051
"./src/browserify-wrapper/ajax.js": "./src/browserify-wrapper/ajax-browser.js",
5152
"./src/browserify-wrapper/upload.js": "./src/browserify-wrapper/upload-browser.js",
5253
"./src/browserify-wrapper/localStorage.js": "./src/browserify-wrapper/localstorage-browser.js",
5354
"./src/browserify-wrapper/parse-base64.js": "./src/browserify-wrapper/parse-base64-browser.js",
54-
5555
"./dist/browserify-wrapper/ajax.js": "./dist/node/browserify-wrapper/ajax-browser.js",
5656
"./dist/browserify-wrapper/upload.js": "./dist/node/browserify-wrapper/upload-browser.js",
5757
"./dist/browserify-wrapper/localStorage.js": "./dist/node/browserify-wrapper/localstorage-browser.js",

src/av.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ AV._ = require('underscore');
1919
AV.version = require('./version');
2020
AV.Promise = require('./promise');
2121
AV.localStorage = require('./localstorage');
22+
2223
// 挂载所有内部配置项
2324
AV._config = AV._config || {};
2425

2526
// 以下模块为了兼容原有代码,使用这种加载方式。
26-
require('./utils')(AV);
27+
require('./utils').init(AV);
2728
require('./error')(AV);
2829
require('./event')(AV);
2930
require('./geopoint')(AV);

src/browserify-wrapper/ajax-browser.js

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,35 @@
33
* Each engineer has a duty to keep the code elegant
44
**/
55

6-
var Promise = require('../promise');
6+
'use strict';
77

8-
module.exports = function _ajax(method, url, data, success, error) {
9-
var options = {
8+
const AVPromise = require('../promise');
9+
const AVUtils = require('../utils');
10+
11+
const ajax = (method, url, data, success, error) => {
12+
const AV = global.AV;
13+
14+
const promise = new AVPromise();
15+
const options = {
1016
success: success,
1117
error: error
1218
};
1319

14-
if (useXDomainRequest()) {
15-
return ajaxIE8(method, url, data)._thenRunCallbacks(options);
16-
}
17-
18-
var promise = new Promise();
19-
var handled = false;
20+
const appId = AV.applicationId;
21+
const appKey = AV.applicationKey;
22+
const masterKey = AV.masterKey;
2023

21-
var xhr = new XMLHttpRequest();
22-
xhr.onreadystatechange = function() {
24+
let handled = false;
25+
const xhr = new global.XMLHttpRequest();
26+
xhr.onreadystatechange = () => {
2327
if (xhr.readyState === 4) {
2428
if (handled) {
2529
return;
2630
}
2731
handled = true;
2832

2933
if (xhr.status >= 200 && xhr.status < 300) {
30-
var response;
34+
let response;
3135
try {
3236
response = JSON.parse(xhr.responseText);
3337
} catch (e) {
@@ -44,49 +48,19 @@ module.exports = function _ajax(method, url, data, success, error) {
4448
}
4549
};
4650
xhr.open(method, url, true);
47-
xhr.setRequestHeader("Content-Type", "text/plain"); // avoid pre-flight.
51+
xhr.setRequestHeader('X-LC-Id', appId);
52+
53+
let signature;
54+
if (masterKey) {
55+
signature = AVUtils.sign(masterKey, true);
56+
} else {
57+
signature = AVUtils.sign(appKey);
58+
}
59+
60+
xhr.setRequestHeader('X-LC-Sign', signature);
61+
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
4862
xhr.send(data);
4963
return promise._thenRunCallbacks(options);
5064
};
5165

52-
function useXDomainRequest() {
53-
if (typeof(XDomainRequest) !== "undefined") {
54-
// We're in IE 8+.
55-
if ('withCredentials' in new XMLHttpRequest()) {
56-
// We're in IE 10+.
57-
return false;
58-
}
59-
return true;
60-
}
61-
return false;
62-
}
63-
64-
function ajaxIE8(method, url, data) {
65-
var promise = new Promise();
66-
var xdr = new XDomainRequest();
67-
xdr.onload = function() {
68-
var response;
69-
try {
70-
response = JSON.parse(xdr.responseText);
71-
} catch (e) {
72-
promise.reject(e);
73-
}
74-
if (response) {
75-
promise.resolve(response);
76-
}
77-
};
78-
xdr.onerror = xdr.ontimeout = function() {
79-
// Let's fake a real error message.
80-
var fakeResponse = {
81-
responseText: JSON.stringify({
82-
code: AV.Error.X_DOMAIN_REQUEST,
83-
error: "IE's XDomainRequest does not supply error info."
84-
})
85-
};
86-
promise.reject(xdr);
87-
};
88-
xdr.onprogress = function() {};
89-
xdr.open(method, url);
90-
xdr.send(data);
91-
return promise;
92-
}
66+
module.exports = ajax;

src/utils.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
'use strict';
77

88
const _ = require('underscore');
9+
const md5 = require('md5');
910

10-
module.exports = function(AV) {
11+
const init = (AV) => {
1112

1213
// 挂载一些配置
1314
let AVConfig = AV._config;
@@ -138,12 +139,19 @@ module.exports = function(AV) {
138139
*/
139140

140141
AV.init = (...args) => {
142+
143+
const masterKeyWarn = () => {
144+
console.warn('MasterKey should not be used in the browser. ' +
145+
'The permissions of MasterKey can be across all the server permissions,' +
146+
' including the setting of ACL .');
147+
};
148+
141149
switch (args.length) {
142150
case 1:
143151
const options = args[0];
144152
if (typeof options === 'object') {
145153
if (!AVConfig.isNode && options.masterKey) {
146-
throw new Error('AV.init(): Master Key is only used in Node.js.');
154+
masterKeyWarn();
147155
}
148156
initialize(options.appId, options.appKey, options.masterKey);
149157
setRegionServer(options.region);
@@ -156,7 +164,7 @@ module.exports = function(AV) {
156164
case 3:
157165
console.warn('Please use AV.init() to replace AV.initialize() .');
158166
if (!AVConfig.isNode && args.length === 3) {
159-
throw new Error('AV.init(): Master Key is only used in Node.js.');
167+
masterKeyWarn();
160168
}
161169
initialize(...args);
162170
setRegionServer('cn');
@@ -380,11 +388,12 @@ module.exports = function(AV) {
380388

381389
dataObject._ApplicationId = AV.applicationId;
382390
dataObject._ApplicationKey = AV.applicationKey;
383-
if(!AV._isNullOrUndefined(AV.applicationProduction)) {
391+
if (!AV._isNullOrUndefined(AV.applicationProduction)) {
384392
dataObject._ApplicationProduction = AV.applicationProduction;
385393
}
386-
if(AV._useMasterKey)
387-
dataObject._MasterKey = AV.masterKey;
394+
if (AV._useMasterKey) {
395+
dataObject._MasterKey = AV.masterKey;
396+
}
388397
dataObject._ClientVersion = AV.VERSION;
389398
// Pass the session token on every request.
390399
return AV.User.currentAsync().then(function(currentUser) {
@@ -663,4 +672,21 @@ module.exports = function(AV) {
663672
AV._isNullOrUndefined = function(x) {
664673
return _.isNull(x) || _.isUndefined(x);
665674
};
675+
676+
};
677+
678+
module.exports = {
679+
680+
init: init,
681+
682+
// 计算 X-LC-Sign 的签名方法
683+
sign: (key, isMasterKey) => {
684+
const now = new Date().getTime();
685+
const signature = md5(now + key);
686+
if (isMasterKey) {
687+
return signature + ',' + now + ',master';
688+
} else {
689+
return signature + ',' + now;
690+
}
691+
}
666692
};

0 commit comments

Comments
 (0)