2016-08-24 22:39:46 +06:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-08-25 23:03:48 +06:00
|
|
|
// Helper functions for accessing the vkontakte API.
|
2016-10-19 17:38:01 +03:00
|
|
|
|
2018-08-12 11:05:28 -04:00
|
|
|
const httpsRequest = require('./httpsRequest');
|
2016-08-24 22:39:46 +06:00
|
|
|
var Parse = require('parse/node').Parse;
|
2016-12-06 17:09:43 -05:00
|
|
|
var logger = require('../../logger').default;
|
2016-08-24 22:39:46 +06:00
|
|
|
|
|
|
|
|
// Returns a promise that fulfills iff this user id is valid.
|
2016-12-01 10:24:46 -08:00
|
|
|
function validateAuthData(authData, params) {
|
2016-10-19 17:38:01 +03:00
|
|
|
return vkOAuth2Request(params).then(function (response) {
|
2017-07-08 20:29:02 +08:00
|
|
|
if (response && response.access_token) {
|
2018-08-17 22:38:07 +03:00
|
|
|
return request("api.vk.com", "method/users.get?access_token=" + authData.access_token + "&v=5.8").then(function (response) {
|
|
|
|
|
if (response && response.response && response.response.length && response.response[0].id == authData.id) {
|
2016-10-19 17:38:01 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Vk auth is invalid for this user.');
|
|
|
|
|
});
|
2016-08-24 22:39:46 +06:00
|
|
|
}
|
2016-10-19 17:38:01 +03:00
|
|
|
logger.error('Vk Auth', 'Vk appIds or appSecret is incorrect.');
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Vk appIds or appSecret is incorrect.');
|
2016-08-24 22:39:46 +06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 17:38:01 +03:00
|
|
|
function vkOAuth2Request(params) {
|
2017-06-26 19:33:08 +07:00
|
|
|
return new Promise(function (resolve) {
|
2016-12-02 16:11:54 -08:00
|
|
|
if (!params || !params.appIds || !params.appIds.length || !params.appSecret || !params.appSecret.length) {
|
2016-10-19 17:38:01 +03:00
|
|
|
logger.error('Vk Auth', 'Vk auth is not configured. Missing appIds or appSecret.');
|
|
|
|
|
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Vk auth is not configured. Missing appIds or appSecret.');
|
|
|
|
|
}
|
2017-06-26 19:33:08 +07:00
|
|
|
resolve();
|
|
|
|
|
}).then(function () {
|
|
|
|
|
return request("oauth.vk.com", "access_token?client_id=" + params.appIds + "&client_secret=" + params.appSecret + "&v=5.59&grant_type=client_credentials");
|
|
|
|
|
});
|
2016-10-19 17:38:01 +03:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 22:39:46 +06:00
|
|
|
// Returns a promise that fulfills iff this app id is valid.
|
|
|
|
|
function validateAppId() {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A promisey wrapper for api requests
|
2016-10-19 17:38:01 +03:00
|
|
|
function request(host, path) {
|
2018-08-12 11:05:28 -04:00
|
|
|
return httpsRequest.get("https://" + host + "/" + path);
|
2016-08-24 22:39:46 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
validateAppId: validateAppId,
|
|
|
|
|
validateAuthData: validateAuthData
|
2016-12-01 10:24:46 -08:00
|
|
|
};
|