Skip to content

Commit

Permalink
Merge 579c61f into ed22fbd
Browse files Browse the repository at this point in the history
  • Loading branch information
harshkothari410 committed Oct 25, 2018
2 parents ed22fbd + 579c61f commit eac1cd4
Show file tree
Hide file tree
Showing 4 changed files with 343 additions and 16 deletions.
233 changes: 228 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"just-safe-get": "^1.2.1",
"just-safe-set": "^2.0.1",
"just-template": "^1.1.22",
"nforce": "^1.10.0",
"path": "^0.12.7",
"repeat": "0.0.6",
"simple-oauth2": "^1.5.2",
Expand Down
44 changes: 33 additions & 11 deletions src/remoteCollection/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const debug = require('debug')('refocus-collector:remoteCollection');
const request = require('superagent');
const get = require('just-safe-get');
const set = require('just-safe-set');
const nforce = require('nforce');
require('superagent-proxy')(request);
const constants = require('../constants');
const attachSubjectsToGenerator = require('../utils/httpUtils').attachSubjectsToGenerator;
Expand Down Expand Up @@ -40,7 +41,8 @@ function sendRemoteRequest(generator) {

// If token is present, add to request header.
if (generator.token) {
const accessToken = generator.token.accessToken;
// Expecting accessToken or access_token from remote source.
const accessToken = generator.token.accessToken || generator.token.access_token;
if (get(simpleOauth, 'tokenFormat')) {
set(conn, AUTH_HEADER,
simpleOauth.tokenFormat.replace('{accessToken}', accessToken));
Expand Down Expand Up @@ -73,14 +75,17 @@ function sendRemoteRequest(generator) {
debug('sendRemoteRequest token expired, requesting a new one');
generator.token = null;
return prepareRemoteRequest(generator)
.then((resp) => {
if (resp) {
debug('sendRemoteRequest returned OK');
generator.res = resp.res;
}

return resolve(generator);
});
.then((resp) => {
if (resp) {
debug('sendRemoteRequest returned OK');
generator.res = resp.res;
}

return resolve(generator);
});
} else {
debug('sendRemoteRequest returned error %O', err);
generator.res = err;
}

debug('sendRemoteRequest returned error %O', err);
Expand Down Expand Up @@ -110,13 +115,30 @@ function prepareRemoteRequest(generator) {
if (!generator.token && get(generator, 'connection.simple_oauth')) {
const method = generator.connection.simple_oauth.method;
const simpleOauth = generator.connection.simple_oauth;
const oauth2 = require('simple-oauth2').create(simpleOauth.credentials);
return oauth2[method]

if (generator.connection.simple_oauth.salesforce) {
const credentials = {
clientId: simpleOauth.credentials.client.id,
clientSecret: simpleOauth.credentials.client.secret,
redirectUri: simpleOauth.credentials.client.redirectUri,
};

const org = nforce.createConnection(credentials);
return org.authenticate(simpleOauth.tokenConfig)
.then((token) => {
generator.token = token;
return sendRemoteRequest(generator);
});

} else {
const oauth2 = require('simple-oauth2').create(simpleOauth.credentials);
return oauth2[method]
.getToken(simpleOauth.tokenConfig)
.then((token) => {
generator.token = token;
return sendRemoteRequest(generator);
});
}
}

return sendRemoteRequest(generator);
Expand Down

0 comments on commit eac1cd4

Please sign in to comment.