2016-11-22 21:30:31 +08:00
|
|
|
// Helper functions for accessing the qq Graph API.
|
2018-08-12 11:05:28 -04:00
|
|
|
const httpsRequest = require('./httpsRequest');
|
2016-11-22 21:30:31 +08:00
|
|
|
var Parse = require('parse/node').Parse;
|
|
|
|
|
|
|
|
|
|
// Returns a promise that fulfills iff this user id is valid.
|
|
|
|
|
function validateAuthData(authData) {
|
2020-12-13 11:19:04 -06:00
|
|
|
return graphRequest('me?access_token=' + authData.access_token).then(function (data) {
|
2016-11-22 21:30:31 +08:00
|
|
|
if (data && data.openid == authData.id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-12-13 11:19:04 -06:00
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.');
|
2016-11-22 21:30:31 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a promise that fulfills if this app id is valid.
|
2016-11-24 15:47:41 -05:00
|
|
|
function validateAppId() {
|
2016-11-22 21:30:31 +08:00
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A promisey wrapper for qq graph requests.
|
|
|
|
|
function graphRequest(path) {
|
2020-12-13 11:19:04 -06:00
|
|
|
return httpsRequest.get('https://graph.qq.com/oauth2.0/' + path, true).then(data => {
|
|
|
|
|
return parseResponseData(data);
|
|
|
|
|
});
|
2016-11-22 21:30:31 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-12 11:05:28 -04:00
|
|
|
function parseResponseData(data) {
|
2018-09-01 13:58:06 -04:00
|
|
|
const starPos = data.indexOf('(');
|
|
|
|
|
const endPos = data.indexOf(')');
|
|
|
|
|
if (starPos == -1 || endPos == -1) {
|
2020-12-13 11:19:04 -06:00
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'qq auth is invalid for this user.');
|
2018-08-12 11:05:28 -04:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
data = data.substring(starPos + 1, endPos - 1);
|
2018-08-12 11:05:28 -04:00
|
|
|
return JSON.parse(data);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 21:30:31 +08:00
|
|
|
module.exports = {
|
|
|
|
|
validateAppId,
|
2018-08-12 11:05:28 -04:00
|
|
|
validateAuthData,
|
|
|
|
|
parseResponseData,
|
2016-11-22 21:30:31 +08:00
|
|
|
};
|