Adds support for multiple twitter auths options (#2256)
* Adds support for multiple twitter auths options * Adds user test
This commit is contained in:
committed by
Tyler Brock
parent
fc42388305
commit
7964d0a999
50
spec/TwitterAuth.spec.js
Normal file
50
spec/TwitterAuth.spec.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
let twitter = require('../src/authDataManager/twitter');
|
||||||
|
|
||||||
|
describe('Twitter Auth', () => {
|
||||||
|
it('should use the proper configuration', () => {
|
||||||
|
// Multiple options, consumer_key found
|
||||||
|
expect(twitter.handleMultipleConfigurations({
|
||||||
|
consumer_key: 'hello',
|
||||||
|
}, [{
|
||||||
|
consumer_key: 'hello'
|
||||||
|
}, {
|
||||||
|
consumer_key: 'world'
|
||||||
|
}]).consumer_key).toEqual('hello')
|
||||||
|
|
||||||
|
// Multiple options, consumer_key not found
|
||||||
|
expect(function(){
|
||||||
|
twitter.handleMultipleConfigurations({
|
||||||
|
consumer_key: 'some',
|
||||||
|
}, [{
|
||||||
|
consumer_key: 'hello'
|
||||||
|
}, {
|
||||||
|
consumer_key: 'world'
|
||||||
|
}]);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
// Multiple options, consumer_key not found
|
||||||
|
expect(function(){
|
||||||
|
twitter.handleMultipleConfigurations({
|
||||||
|
auth_token: 'token',
|
||||||
|
}, [{
|
||||||
|
consumer_key: 'hello'
|
||||||
|
}, {
|
||||||
|
consumer_key: 'world'
|
||||||
|
}]);
|
||||||
|
}).toThrow();
|
||||||
|
|
||||||
|
// Single configuration and consumer_key set
|
||||||
|
expect(twitter.handleMultipleConfigurations({
|
||||||
|
consumer_key: 'hello',
|
||||||
|
}, {
|
||||||
|
consumer_key: 'hello'
|
||||||
|
}).consumer_key).toEqual('hello');
|
||||||
|
|
||||||
|
// General case, only 1 config, no consumer_key set
|
||||||
|
expect(twitter.handleMultipleConfigurations({
|
||||||
|
auth_token: 'token',
|
||||||
|
}, {
|
||||||
|
consumer_key: 'hello'
|
||||||
|
}).consumer_key).toEqual('hello');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
// Helper functions for accessing the twitter API.
|
// Helper functions for accessing the twitter API.
|
||||||
var OAuth = require('./OAuth1Client');
|
var OAuth = require('./OAuth1Client');
|
||||||
var Parse = require('parse/node').Parse;
|
var Parse = require('parse/node').Parse;
|
||||||
|
var logger = require('../logger').default;
|
||||||
|
|
||||||
// Returns a promise that fulfills iff this user id is valid.
|
// Returns a promise that fulfills iff this user id is valid.
|
||||||
function validateAuthData(authData, options) {
|
function validateAuthData(authData, options) {
|
||||||
|
options = handleMultipleConfigurations(authData, options);
|
||||||
var client = new OAuth(options);
|
var client = new OAuth(options);
|
||||||
client.host = "api.twitter.com";
|
client.host = "api.twitter.com";
|
||||||
client.auth_token = authData.auth_token;
|
client.auth_token = authData.auth_token;
|
||||||
@@ -24,7 +26,28 @@ function validateAppId() {
|
|||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleMultipleConfigurations(authData, options) {
|
||||||
|
if (Array.isArray(options)) {
|
||||||
|
let consumer_key = authData.consumer_key;
|
||||||
|
if (!consumer_key) {
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
options = options.filter((option) => {
|
||||||
|
return option.consumer_key == consumer_key;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.length == 0) {
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
options = options[0];
|
||||||
|
}
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
validateAppId: validateAppId,
|
validateAppId,
|
||||||
validateAuthData: validateAuthData
|
validateAuthData,
|
||||||
|
handleMultipleConfigurations
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user