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

向普通 Express 路由提供 parse authInfo 和 authorization 的支持 #52

Closed
jysperm opened this issue Jan 27, 2016 · 3 comments
Closed

Comments

@jysperm
Copy link
Contributor

jysperm commented Jan 27, 2016

有用户希望在普通 Express 路由中使用这两个中间件(

// parse authInfo
Cloud.use(route, function(req, res, next) {
var appId, appKey, masterKey, contentType, param, prod, prodHeader, prodValue, sessionToken;
contentType = req.headers['content-type'];
if (/^text\/plain.*/i.test(contentType)) {
if (req.body && req.body !== '') {
req.body = JSON.parse(req.body);
}
appId = req.body._ApplicationId;
appKey = req.body._ApplicationKey;
masterKey = req.body._MasterKey;
prodValue = req.body._ApplicationProduction;
sessionToken = req.body._SessionToken;
for (param in req.body) {
if (param.charAt(0) === '_') {
delete req.body[param];
}
}
prod = 1;
if (prodValue === 0 || prodValue === false) {
prod = 0;
}
req.AV = {
id: appId,
key: appKey,
masterKey: masterKey,
prod: prod,
sessionToken: sessionToken
};
} else {
appId = req.headers['x-lc-id'] ||
req.headers['x-avoscloud-application-id'] ||
req.headers['x-uluru-application-id'];
appKey = req.headers['x-lc-key'] ||
req.headers['x-avoscloud-application-key'] ||
req.headers['x-uluru-application-key'];
masterKey = req.headers['x-avoscloud-master-key'] || req.headers['x-uluru-master-key'];
prodHeader = req.headers['x-lc-prod'] ||
req.headers['x-avoscloud-application-production'] ||
req.headers['x-uluru-application-production'];
sessionToken = req.headers['x-lc-session'] ||
req.headers['x-uluru-session-token'] ||
req.headers['x-avoscloud-session-token'];
prod = 1;
if (prodHeader === '0' || prodHeader === 'false') {
prod = 0;
}
if (appKey && (appKey.indexOf(',master') > 0)) {
masterKey = appKey.slice(0, appKey.indexOf(','));
appKey = null;
}
req.AV = {
id: appId,
key: appKey,
masterKey: masterKey,
prod: prod,
sessionToken: sessionToken
};
}
return next();
});
// authorization
Cloud.use(route, function(req, res, next) {
var key, master, requestSign, sign, timestamp, validSign, _ref;
if (!req.AV.id) {
return unauthResp(res);
}
if (AV.applicationId === req.AV.id &&
(AV.applicationKey === req.AV.key ||
AV.masterKey === req.AV.key ||
AV.masterKey === req.AV.masterKey)) {
if (AV.masterKey === req.AV.masterKey) {
req.AV.authMasterKey = true;
}
return next();
}
requestSign = req.headers['x-lc-sign'] || req.headers['x-avoscloud-request-sign'];
if (requestSign) {
_ref = requestSign.split(',');
sign = _ref[0];
timestamp = _ref[1];
master = _ref[2];
key = master === 'master' ? AV.masterKey : AV.applicationKey;
validSign = signByKey(timestamp, key);
if (validSign === sign.toLowerCase()) {
if (master === 'master') {
req.AV.authMasterKey = true;
req.AV.masterKey = key;
} else {
req.AV.key = key;
}
return next();
}
}
return unauthResp(res);
});
),可以考虑将 leanengine.js 中主要的中间件都拆分到单独的文件,以便用户可以自行使用。

@jysperm
Copy link
Contributor Author

jysperm commented Feb 26, 2016

届时也可以支持一下 WebSocket

@aisk
Copy link
Contributor

aisk commented Feb 26, 2016

👍

@jysperm
Copy link
Contributor Author

jysperm commented Mar 22, 2017

已在 #102 中支持

@jysperm jysperm closed this as completed Mar 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants