Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Added OAuth2 authentication support
Browse files Browse the repository at this point in the history
Added OAuth2 authentication support
  • Loading branch information
manivinesh authored and sfdrogojan committed May 20, 2019
1 parent b3d31d7 commit 7d7c6d4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
56 changes: 45 additions & 11 deletions lib/fuel-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const merge = require('lodash.merge');
const version = require('../package.json').version;

module.exports = class FuelAuth {
constructor(options) {
constructor(options) {
if (options) {
if (!options.clientId || !options.clientSecret) {
throw new Error('clientId or clientSecret is missing or invalid');
Expand All @@ -20,6 +20,10 @@ module.exports = class FuelAuth {
if (typeof options.clientId !== 'string' || typeof options.clientSecret !== 'string') {
throw new Error('clientId or clientSecret must be strings');
}

if (options.authOptions && typeof options.authOptions.authVersion === 2 && !options.authUrl) {
throw new Error('Auth URL is mandatory for OAuth2 Authentication');
}
} else {
throw new Error('options are required. see readme.');
}
Expand All @@ -31,9 +35,15 @@ module.exports = class FuelAuth {
this.clientSecret = options.clientSecret;
this.expiration = null;
this.refreshToken = options.refreshToken;
this.scope = options.scope;
this.version = version;
this.globalReqOptions = options.globalReqOptions || {};
if(options.authOptions){
this.accountId = options.authOptions.accountId;
this.authVersion = options.authOptions.authVersion;
this.scope = options.authOptions.scope;
this.soapUrl = null;
this.restUrl = null;
}
}
getAccessToken(options, callback) {
let done = callback;
Expand Down Expand Up @@ -91,21 +101,38 @@ module.exports = class FuelAuth {
.then(body => callback(null, body))
.catch(err => callback(err, null));
} else {
callback(null, {
let response = {
accessToken: this.accessToken,
expiresIn: this.expiration - process.hrtime()[0]
});
};
if(this.authVersion === 2) {
response.rest_instance_url = this.restUrl;
response.soap_instance_url = this.soapUrl;
}
callback(null, response);
}
}
_requestToken(requestOptions) {
const payload = {};
if(this.authVersion === 2){
payload.client_id = this.clientId;
payload.client_secret = this.clientSecret;
payload.grant_type = 'client_credentials';
if(this.accountId){
payload.account_id = this.accountId;
}
if(this.scope){
payload.scope = this.scope;
}
} else {
payload.clientId = this.clientId;
payload.clientSecret = this.clientSecret;
}
// set auth options for request
const baseOptions = {
url: this.authUrl,
method: 'POST',
json: {
clientId: this.clientId,
clientSecret: this.clientSecret
}
json: payload
};

const options = merge({}, this.globalReqOptions, baseOptions, requestOptions);
Expand Down Expand Up @@ -137,9 +164,16 @@ module.exports = class FuelAuth {
this.refreshToken = body.refreshToken;
}

this.accessToken = body.accessToken || null;
this.expiration = body.expiresIn ? process.hrtime()[0] + body.expiresIn : null;

if(this.authVersion === 2){
this.accessToken = body.access_token || null;
body.accessToken = this.accessToken;
this.soapUrl = body.soap_instance_url;
this.restUrl = body.rest_instance_url;
this.expiration = body.expires_in ? process.hrtime()[0] + body.expires_in : null;
} else {
this.accessToken = body.accessToken || null;
this.expiration = body.expiresIn ? process.hrtime()[0] + body.expiresIn : null;
}
resolve(body);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuel-auth",
"version": "3.1.0",
"version": "3.2.0",
"description": "Node library for authenticating REST and SOAP APIs in the Salesforce Marketing Cloud (formerly ExactTarget).",
"main": "./lib/fuel-auth.js",
"scripts": {
Expand Down

0 comments on commit 7d7c6d4

Please sign in to comment.