From 7c8bab4ab6635f6bc7711e9c7e31433de3897404 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 12 Jul 2016 08:22:59 -0400 Subject: [PATCH 1/2] Adds support for multiple twitter auths options --- src/authDataManager/twitter.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/authDataManager/twitter.js b/src/authDataManager/twitter.js index 8de2fca527..dbd16ba2ac 100644 --- a/src/authDataManager/twitter.js +++ b/src/authDataManager/twitter.js @@ -1,9 +1,26 @@ // Helper functions for accessing the twitter API. var OAuth = require('./OAuth1Client'); var Parse = require('parse/node').Parse; +var logger = require('../logger'); // Returns a promise that fulfills iff this user id is valid. function validateAuthData(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]; + } var client = new OAuth(options); client.host = "api.twitter.com"; client.auth_token = authData.auth_token; From 6e35d33a76d423b516d3b4b4c8c1a762412fb822 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Wed, 13 Jul 2016 09:39:09 -0400 Subject: [PATCH 2/2] Adds user test --- spec/TwitterAuth.spec.js | 50 ++++++++++++++++++++++++++++++++++ src/authDataManager/twitter.js | 44 +++++++++++++++++------------- 2 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 spec/TwitterAuth.spec.js diff --git a/spec/TwitterAuth.spec.js b/spec/TwitterAuth.spec.js new file mode 100644 index 0000000000..0c0de56ec9 --- /dev/null +++ b/spec/TwitterAuth.spec.js @@ -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'); + }); +}); \ No newline at end of file diff --git a/src/authDataManager/twitter.js b/src/authDataManager/twitter.js index dbd16ba2ac..20d73a3968 100644 --- a/src/authDataManager/twitter.js +++ b/src/authDataManager/twitter.js @@ -1,26 +1,11 @@ // Helper functions for accessing the twitter API. var OAuth = require('./OAuth1Client'); var Parse = require('parse/node').Parse; -var logger = require('../logger'); +var logger = require('../logger').default; // Returns a promise that fulfills iff this user id is valid. function validateAuthData(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]; - } + options = handleMultipleConfigurations(authData, options); var client = new OAuth(options); client.host = "api.twitter.com"; client.auth_token = authData.auth_token; @@ -41,7 +26,28 @@ function validateAppId() { 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 = { - validateAppId: validateAppId, - validateAuthData: validateAuthData + validateAppId, + validateAuthData, + handleMultipleConfigurations };