提交 8b466088 编写于 作者: F Florent Vilmart

Refactors auth with common request

上级 dcd8e562
// Helper functions for accessing the Facebook Graph API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -36,19 +35,9 @@ function validateAppId(appIds, authData) {
// A promisey wrapper for FB graph requests.
function graphRequest(path) {
return new Promise(function(resolve, reject) {
https.get('https://graph.facebook.com/v2.5/' + path, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Facebook.');
});
return require('./request')({
host: 'graph.facebook.com',
path: 'v2.5/' + path
});
}
......
// Helper functions for accessing the github API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -22,26 +21,13 @@ function validateAppId() {
// A promisey wrapper for api requests
function request(path, access_token) {
return new Promise(function(resolve, reject) {
https.get({
host: 'api.github.com',
path: '/' + path,
headers: {
'Authorization': 'bearer ' + access_token,
'User-Agent': 'parse-server'
}
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Github.');
});
return require('./request')({
host: 'api.github.com',
path: '/' + path,
headers: {
'Authorization': 'bearer ' + access_token,
'User-Agent': 'parse-server'
}
});
}
......
// Helper functions for accessing the google API.
var https = require('https');
var Parse = require('parse/node').Parse;
function validateIdToken(id, token) {
......@@ -48,19 +47,9 @@ function validateAppId() {
// A promisey wrapper for api requests
function request(path) {
return new Promise(function(resolve, reject) {
https.get("https://www.googleapis.com/oauth2/v3/" + path, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Google.');
});
return require('./request')({
host: 'www.googleapis.com',
path: 'oauth2/v3/' + path
});
}
......
// Helper functions for accessing the instagram API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -22,19 +21,9 @@ function validateAppId() {
// A promisey wrapper for api requests
function request(path) {
return new Promise(function(resolve, reject) {
https.get("https://api.instagram.com/v1/" + path, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Instagram.');
});
return require('./request')({
host: 'api.instagram.com',
path: 'v1/' + path
});
}
......
// Helper functions for accessing the Janrain Capture API.
var https = require('https');
var Parse = require('parse/node').Parse;
var querystring = require('querystring');
......@@ -29,22 +28,9 @@ function request(host, access_token) {
'access_token': access_token,
'attribute_name': 'uuid' // we only need to pull the uuid for this access token to make sure it matches
});
return new Promise(function(resolve, reject) {
https.get({
host: host,
path: '/entity?' + query_string_data
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function () {
resolve(JSON.parse(data));
});
}).on('error', function() {
reject('Failed to validate this access token with Janrain capture.');
});
return require('./request')({
host,
path: '/entity?' + query_string_data
});
}
......
// Helper functions for accessing the linkedin API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -31,23 +30,10 @@ function request(path, access_token, is_mobile_sdk) {
headers['x-li-src'] = 'msdk';
}
return new Promise(function(resolve, reject) {
https.get({
host: 'api.linkedin.com',
path: '/v1/' + path,
headers: headers
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Linkedin.');
});
return require('./request')({
host: 'api.linkedin.com',
path: '/v1/' + path,
headers: headers
});
}
......
// Helper functions for accessing the meetup API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -22,25 +21,12 @@ function validateAppId() {
// A promisey wrapper for api requests
function request(path, access_token) {
return new Promise(function(resolve, reject) {
https.get({
host: 'api.meetup.com',
path: '/2/' + path,
headers: {
'Authorization': 'bearer ' + access_token
}
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Meetup.');
});
return require('./request')({
host: 'api.meetup.com',
path: '/2/' + path,
headers: {
'Authorization': 'bearer ' + access_token
}
});
}
......
const https = require('https');
function request({
host,
path,
headers
}) {
return new Promise(function(resolve, reject) {
https.get({
host,
path,
headers
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
try {
data = JSON.parse(data);
} catch(e) {
return reject(e);
}
resolve(data);
});
}).on('error', function(e) {
reject(e);
});
});
}
module.exports = request;
// Helper functions for accessing the Spotify API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -36,25 +35,12 @@ function validateAppId(appIds, authData) {
// A promisey wrapper for Spotify API requests.
function request(path, access_token) {
return new Promise(function(resolve, reject) {
https.get({
host: 'api.spotify.com',
path: '/v1/' + path,
headers: {
'Authorization': 'Bearer ' + access_token
}
}, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function() {
reject('Failed to validate this access token with Spotify.');
});
return require('./request')({
host: 'api.spotify.com',
path: '/v1/' + path,
headers: {
'Authorization': 'Bearer ' + access_token
}
});
}
......
......@@ -2,7 +2,6 @@
// Helper functions for accessing the vkontakte API.
var https = require('https');
var Parse = require('parse/node').Parse;
var logger = require('../../logger').default;
......@@ -40,20 +39,10 @@ function validateAppId() {
// A promisey wrapper for api requests
function request(host, path) {
return new Promise(function (resolve, reject) {
https.get("https://" + host + "/" + path, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function () {
reject('Failed to validate this access token with Vk.');
});
});
return require('./request')({
host,
path
})
}
module.exports = {
......
// Helper functions for accessing the WeChat Graph API.
var https = require('https');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
......@@ -19,19 +18,9 @@ function validateAppId() {
// A promisey wrapper for WeChat graph requests.
function graphRequest(path) {
return new Promise(function (resolve, reject) {
https.get('https://api.weixin.qq.com/sns/' + path, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data);
resolve(data);
});
}).on('error', function () {
reject('Failed to validate this access token with weixin.');
});
return require('./request')({
host: 'api.weixin.qq.com',
path: '/sns/' + path
});
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册