2016-06-15 16:09:20 -04:00
|
|
|
// Helper functions for accessing the twitter API.
|
2016-02-04 14:03:39 -05:00
|
|
|
var OAuth = require('./OAuth1Client');
|
|
|
|
|
var Parse = require('parse/node').Parse;
|
2016-12-06 17:09:43 -05:00
|
|
|
var logger = require('../../logger').default;
|
2016-02-04 14:03:39 -05:00
|
|
|
|
|
|
|
|
// Returns a promise that fulfills iff this user id is valid.
|
|
|
|
|
function validateAuthData(authData, options) {
|
2018-09-01 13:58:06 -04:00
|
|
|
if (!options) {
|
|
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.INTERNAL_SERVER_ERROR,
|
|
|
|
|
'Twitter auth configuration missing'
|
|
|
|
|
);
|
2017-03-28 15:16:47 -07:00
|
|
|
}
|
2016-07-23 21:02:45 +02:00
|
|
|
options = handleMultipleConfigurations(authData, options);
|
2016-02-04 14:03:39 -05:00
|
|
|
var client = new OAuth(options);
|
2018-09-01 13:58:06 -04:00
|
|
|
client.host = 'api.twitter.com';
|
2016-02-04 14:03:39 -05:00
|
|
|
client.auth_token = authData.auth_token;
|
|
|
|
|
client.auth_token_secret = authData.auth_token_secret;
|
2016-12-01 10:24:46 -08:00
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
return client.get('/1.1/account/verify_credentials.json').then(data => {
|
2017-01-11 12:31:40 -08:00
|
|
|
if (data && data.id_str == '' + authData.id) {
|
2016-02-04 14:03:39 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OBJECT_NOT_FOUND,
|
2018-09-01 13:58:06 -04:00
|
|
|
'Twitter auth is invalid for this user.'
|
|
|
|
|
);
|
2016-02-04 14:03:39 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns a promise that fulfills iff this app id is valid.
|
|
|
|
|
function validateAppId() {
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 21:02:45 +02:00
|
|
|
function handleMultipleConfigurations(authData, options) {
|
|
|
|
|
if (Array.isArray(options)) {
|
2016-12-07 15:17:05 -08:00
|
|
|
const consumer_key = authData.consumer_key;
|
2016-07-23 21:02:45 +02:00
|
|
|
if (!consumer_key) {
|
2018-09-01 13:58:06 -04:00
|
|
|
logger.error(
|
|
|
|
|
'Twitter Auth',
|
|
|
|
|
'Multiple twitter configurations are available, by no consumer_key was sent by the client.'
|
|
|
|
|
);
|
|
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OBJECT_NOT_FOUND,
|
|
|
|
|
'Twitter auth is invalid for this user.'
|
|
|
|
|
);
|
2016-07-23 21:02:45 +02:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
options = options.filter(option => {
|
2016-07-23 21:02:45 +02:00
|
|
|
return option.consumer_key == consumer_key;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (options.length == 0) {
|
2018-09-01 13:58:06 -04:00
|
|
|
logger.error(
|
|
|
|
|
'Twitter Auth',
|
|
|
|
|
'Cannot find a configuration for the provided consumer_key'
|
|
|
|
|
);
|
|
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OBJECT_NOT_FOUND,
|
|
|
|
|
'Twitter auth is invalid for this user.'
|
|
|
|
|
);
|
2016-07-23 21:02:45 +02:00
|
|
|
}
|
|
|
|
|
options = options[0];
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 14:03:39 -05:00
|
|
|
module.exports = {
|
2016-07-23 21:02:45 +02:00
|
|
|
validateAppId,
|
|
|
|
|
validateAuthData,
|
2018-09-01 13:58:06 -04:00
|
|
|
handleMultipleConfigurations,
|
2016-02-04 14:03:39 -05:00
|
|
|
};
|