Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -159,7 +167,7 @@ exports.authorize = function( api, bid, key, cb ) {
}

});
}
};

exports.saveOauthToken = function( api, oauth_token, oauth_verifier, bid, key, cb) {

Expand All @@ -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
exports.OAuth = oauth;
119 changes: 63 additions & 56 deletions lib/oauth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) {

Expand Down Expand Up @@ -160,7 +167,7 @@ exports.authorize = function( api, bid, key, cb ) {
}

});
}
};

exports.saveCode = function( res, state, code, cb) {

Expand All @@ -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);

}
};