1
1
// Helper functions for accessing the twitter API.
2
2
var OAuth = require ( './OAuth1Client' ) ;
3
3
var Parse = require ( 'parse/node' ) . Parse ;
4
+ var logger = require ( '../logger' ) . default ;
4
5
5
6
// Returns a promise that fulfills iff this user id is valid.
6
7
function validateAuthData ( authData , options ) {
8
+ options = handleMultipleConfigurations ( authData , options ) ;
7
9
var client = new OAuth ( options ) ;
8
10
client . host = "api.twitter.com" ;
9
11
client . auth_token = authData . auth_token ;
@@ -24,7 +26,28 @@ function validateAppId() {
24
26
return Promise . resolve ( ) ;
25
27
}
26
28
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
+
27
49
module . exports = {
28
- validateAppId : validateAppId ,
29
- validateAuthData : validateAuthData
50
+ validateAppId,
51
+ validateAuthData,
52
+ handleMultipleConfigurations
30
53
} ;
0 commit comments