Skip to content

Commit 7964d0a

Browse files
flovilmartTylerBrock
authored andcommitted
Adds support for multiple twitter auths options (#2256)
* Adds support for multiple twitter auths options * Adds user test
1 parent fc42388 commit 7964d0a

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

spec/TwitterAuth.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
let twitter = require('../src/authDataManager/twitter');
2+
3+
describe('Twitter Auth', () => {
4+
it('should use the proper configuration', () => {
5+
// Multiple options, consumer_key found
6+
expect(twitter.handleMultipleConfigurations({
7+
consumer_key: 'hello',
8+
}, [{
9+
consumer_key: 'hello'
10+
}, {
11+
consumer_key: 'world'
12+
}]).consumer_key).toEqual('hello')
13+
14+
// Multiple options, consumer_key not found
15+
expect(function(){
16+
twitter.handleMultipleConfigurations({
17+
consumer_key: 'some',
18+
}, [{
19+
consumer_key: 'hello'
20+
}, {
21+
consumer_key: 'world'
22+
}]);
23+
}).toThrow();
24+
25+
// Multiple options, consumer_key not found
26+
expect(function(){
27+
twitter.handleMultipleConfigurations({
28+
auth_token: 'token',
29+
}, [{
30+
consumer_key: 'hello'
31+
}, {
32+
consumer_key: 'world'
33+
}]);
34+
}).toThrow();
35+
36+
// Single configuration and consumer_key set
37+
expect(twitter.handleMultipleConfigurations({
38+
consumer_key: 'hello',
39+
}, {
40+
consumer_key: 'hello'
41+
}).consumer_key).toEqual('hello');
42+
43+
// General case, only 1 config, no consumer_key set
44+
expect(twitter.handleMultipleConfigurations({
45+
auth_token: 'token',
46+
}, {
47+
consumer_key: 'hello'
48+
}).consumer_key).toEqual('hello');
49+
});
50+
});

src/authDataManager/twitter.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Helper functions for accessing the twitter API.
22
var OAuth = require('./OAuth1Client');
33
var Parse = require('parse/node').Parse;
4+
var logger = require('../logger').default;
45

56
// Returns a promise that fulfills iff this user id is valid.
67
function validateAuthData(authData, options) {
8+
options = handleMultipleConfigurations(authData, options);
79
var client = new OAuth(options);
810
client.host = "api.twitter.com";
911
client.auth_token = authData.auth_token;
@@ -24,7 +26,28 @@ function validateAppId() {
2426
return Promise.resolve();
2527
}
2628

29+
function handleMultipleConfigurations(authData, options) {
30+
if (Array.isArray(options)) {
31+
let consumer_key = authData.consumer_key;
32+
if (!consumer_key) {
33+
logger.error('Twitter Auth', 'Multiple twitter configurations are available, by no consumer_key was sent by the client.');
34+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth is invalid for this user.');
35+
}
36+
options = options.filter((option) => {
37+
return option.consumer_key == consumer_key;
38+
});
39+
40+
if (options.length == 0) {
41+
logger.error('Twitter Auth','Cannot find a configuration for the provided consumer_key');
42+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth is invalid for this user.');
43+
}
44+
options = options[0];
45+
}
46+
return options;
47+
}
48+
2749
module.exports = {
28-
validateAppId: validateAppId,
29-
validateAuthData: validateAuthData
50+
validateAppId,
51+
validateAuthData,
52+
handleMultipleConfigurations
3053
};

0 commit comments

Comments
 (0)