11// Helper functions for accessing the twitter API.
22var OAuth = require ( './OAuth1Client' ) ;
33var Parse = require ( 'parse/node' ) . Parse ;
4+ var logger = require ( '../logger' ) . default ;
45
56// Returns a promise that fulfills iff this user id is valid.
67function 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+
2749module . exports = {
28- validateAppId : validateAppId ,
29- validateAuthData : validateAuthData
50+ validateAppId,
51+ validateAuthData,
52+ handleMultipleConfigurations
3053} ;
0 commit comments