diff --git a/lib/fuel-auth.js b/lib/fuel-auth.js index 9b58476..053ee1a 100644 --- a/lib/fuel-auth.js +++ b/lib/fuel-auth.js @@ -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'); @@ -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.'); } @@ -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; @@ -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); @@ -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); }); }); diff --git a/package.json b/package.json index 8fe7e4b..1c582ba 100644 --- a/package.json +++ b/package.json @@ -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": {