diff --git a/lib/oauth.js b/lib/oauth.js index b3759e1..b15073b 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -34,7 +34,10 @@ manager.addJob('getTemporaryCredentials', { winston.info ('getTemporaryCredentials called for ' + bid + ', ' + key); - var oaData = GLOBAL.config.authentication[api.auth.provider]; + var oaData = api.auth.provider; + if (typeof oaData === "string") { + oaData = GLOBAL.config.authentication[oaData]; + } var oa = new oauth(oaData.requestTemporaryCredentials, oaData.requestAccessToken, oaData.oauth_consumer_key, @@ -76,8 +79,10 @@ manager.addJob('getAccessToken', { work: function(api, bid, key, oauth_token, oauth_verifier, cb) { winston.info('Running getAccessToken for ' + bid + ', ' + key); - var oaData = GLOBAL.config.authentication[api.auth.provider], - self = this; + var oaData = api.auth.provider, self = this; + if (typeof oaData === "string") { + oaData = GLOBAL.config.authentication[oaData]; + } var oa = new oauth(oaData.requestTemporaryCredentials, oaData.requestAccessToken, @@ -120,18 +125,21 @@ manager.addJob('getAccessToken', { } }); } -}) +}); exports.authorize = function( api, bid, key, cb ) { winston.info('oauth authorize called for ' + bid +', ' + key); - if (!_.has(GLOBAL.config, 'authentication') || !_.has(GLOBAL.config.authentication, api.auth.provider)) { - winston.error('Authentication provider ' + api.auth.provider + ' not defined'); + var oaData = api.auth.provider; + if (typeof oaData === "string" && (!_.has(GLOBAL.config, 'authentication') || !_.has(GLOBAL.config.authentication, oaData))) { + winston.error('Authentication provider ' + oaData + ' not defined'); return false; } - var oaData = GLOBAL.config.authentication[api.auth.provider]; + if (typeof oaData === "string") { + oaData = GLOBAL.config.authentication[oaData]; + } // See if we have an oauth record in the database client.get(bid+key+'oauth', function (err, doc) { @@ -159,7 +167,7 @@ exports.authorize = function( api, bid, key, cb ) { } }); -} +}; exports.saveOauthToken = function( api, oauth_token, oauth_verifier, bid, key, cb) { @@ -173,12 +181,12 @@ exports.saveOauthToken = function( api, oauth_token, oauth_verifier, bid, key, c api.credentials = JSON.parse(doc); if (oauth_verifier) api.credentials.oauth_verifier = oauth_verifier; - manager.enqueue('getAccessToken', api, bid, key, oauth_token, oauth_verifier, cb) + manager.enqueue('getAccessToken', api, bid, key, oauth_token, oauth_verifier, cb); } }); -} +}; -exports.OAuth = oauth \ No newline at end of file +exports.OAuth = oauth; \ No newline at end of file diff --git a/lib/oauth2.js b/lib/oauth2.js index 37e2ec7..7e20a8d 100644 --- a/lib/oauth2.js +++ b/lib/oauth2.js @@ -33,7 +33,10 @@ manager.addJob('getCode', { winston.info('Run job oauth2:getCode'); - var oaData = GLOBAL.config.authentication[api.auth.provider]; + var oaData = api.auth.provider; + if (typeof oaData === "string") { + oaData = GLOBAL.config.authentication[oaData]; + } var oa = new oauth2(oaData.client_id, oaData.client_secret, @@ -67,70 +70,74 @@ manager.addJob('getAccessToken', { work: function(api, bid, key, cb, grant_type) { winston.info('Run job oauth2:getAccessToken(' + grant_type + ')'); - var oaData = GLOBAL.config.authentication[api.auth.provider], - self = this; + + var oaData = api.auth.provider; + if (typeof oaData === "string") { + oaData = GLOBAL.config.authentication[oaData]; + } + var oa = new oauth2(oaData.client_id, oaData.client_secret, oaData.baseSite, oaData.authorizePath, oaData.accessTokenPath), - self = this; - - winston.debug('api = ' + JSON.stringify(api)); - winston.info('oaData = ' + JSON.stringify(oaData)); - //oa.setAccessTokenName(api.credentials.access_token); + self = this; - var params = { - "grant_type": grant_type - } + winston.debug('api = ' + JSON.stringify(api)); + winston.info('oaData = ' + JSON.stringify(oaData)); + //oa.setAccessTokenName(api.credentials.access_token); + + var params = { + "grant_type": grant_type + }; + + var thisCode = api.credentials.code; - var thisCode = api.credentials.code; - - if (grant_type !== 'refresh_token') { - params.redirect_uri = GLOBAL.config.url + '/oauth2'; + if (grant_type !== 'refresh_token') { + params.redirect_uri = GLOBAL.config.url + '/oauth2'; } else { thisCode = api.credentials.refresh_token; - } - - winston.info('thisCode = ' + thisCode); - winston.info('params = ' + JSON.stringify(params)); + } - oa.getOAuthAccessToken(thisCode, params, function(error, access_token, refresh_token, results) { - winston.info('Run callback for oauth2:getOAuthAccessToken'); - if(error) { - winston.error('Error oauth2:getOAuthAccessToken('+bid+key+'): '+JSON.stringify(error)); - var tout = { - expires: new Date(), - err: error, - cname: key - }; - manager.enqueue('finishAuth', tout, cb); - self.finsihed = true; - } else { - winston.debug('access_token = '+access_token); - - api.credentials.type = 'oauth2'; - api.credentials.provider = api.auth.provider; - api.credentials.access_token = access_token; - - winston.debug('typeof refresh_token = '+typeof refresh_token); - if (typeof refresh_token !== 'undefined') { - winston.debug('first refresh token = '+refresh_token); - api.credentials.refresh_token = refresh_token; - } - - api.credentials.expires = new Date().add({seconds: (results.expires_in - 300)}); - - winston.debug(JSON.stringify(api.credentials)); + winston.info('thisCode = ' + thisCode); + winston.info('params = ' + JSON.stringify(params)); + + oa.getOAuthAccessToken(thisCode || '', params, function(error, access_token, refresh_token, results) { + winston.info('Run callback for oauth2:getOAuthAccessToken'); + if(error) { + winston.error('Error oauth2:getOAuthAccessToken('+bid+key+'): '+JSON.stringify(error)); + var tout = { + expires: new Date(), + err: error, + cname: key + }; + manager.enqueue('finishAuth', tout, cb); + self.finsihed = true; + } else { + winston.debug('access_token = '+access_token); + + api.credentials.type = 'oauth2'; + api.credentials.provider = api.auth.provider; + api.credentials.access_token = access_token; + + winston.debug('typeof refresh_token = '+typeof refresh_token); + if (typeof refresh_token !== 'undefined') { + winston.debug('first refresh token = '+refresh_token); + api.credentials.refresh_token = refresh_token; + } + + api.credentials.expires = new Date().add({seconds: (results.expires_in - 300)}); + + winston.debug(JSON.stringify(api.credentials)); - client.set(bid+key+'oauth2', JSON.stringify(api.credentials)); - winston.debug(bid+key+'oauth2 saved'); - manager.enqueue('finishAuth', true, cb, { "access_token": api.credentials.access_token }); - self.finished = true; - } - }); + client.set(bid+key+'oauth2', JSON.stringify(api.credentials)); + winston.debug(bid+key+'oauth2 saved'); + manager.enqueue('finishAuth', true, cb, { "access_token": api.credentials.access_token }); + self.finished = true; + } + }); } -}) +}); exports.authorize = function( api, bid, key, cb ) { @@ -160,7 +167,7 @@ exports.authorize = function( api, bid, key, cb ) { } }); -} +}; exports.saveCode = function( res, state, code, cb) { @@ -171,8 +178,8 @@ exports.saveCode = function( res, state, code, cb) { var tout = { expires: new Date(), redirect: GLOBAL.config.url + '/bundle/' + state[0] - } + }; manager.enqueue('finishAuth', tout, cb); -} +};