Files
kami-parse-server/src/Adapters/Auth/twitter.js

52 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-06-15 16:09:20 -04:00
// Helper functions for accessing the twitter API.
var OAuth = require('./OAuth1Client');
var Parse = require('parse/node').Parse;
// Returns a promise that fulfills iff this user id is valid.
function validateAuthData(authData, options) {
if (!options) {
2020-12-13 11:19:04 -06:00
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Twitter auth configuration missing');
}
options = handleMultipleConfigurations(authData, options);
var client = new OAuth(options);
client.host = 'api.twitter.com';
client.auth_token = authData.auth_token;
client.auth_token_secret = authData.auth_token_secret;
return client.get('/1.1/account/verify_credentials.json').then(data => {
if (data && data.id_str == '' + authData.id) {
return;
}
2020-12-13 11:19:04 -06:00
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth is invalid for this user.');
});
}
// Returns a promise that fulfills iff this app id is valid.
function validateAppId() {
return Promise.resolve();
}
function handleMultipleConfigurations(authData, options) {
if (Array.isArray(options)) {
2016-12-07 15:17:05 -08:00
const consumer_key = authData.consumer_key;
if (!consumer_key) {
2020-12-13 11:19:04 -06:00
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth is invalid for this user.');
}
options = options.filter(option => {
return option.consumer_key == consumer_key;
});
if (options.length == 0) {
2020-12-13 11:19:04 -06:00
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth is invalid for this user.');
}
options = options[0];
}
return options;
}
module.exports = {
validateAppId,
validateAuthData,
handleMultipleConfigurations,
};