Skip to content

Commit

Permalink
exposed algorithm and signatureMethod on NetSuiteOAuth constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
husseinmkwizu committed Jun 21, 2021
1 parent d637390 commit 6d784f0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 98 deletions.
208 changes: 114 additions & 94 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
"use strict";

// Dependencies
const request = require('request');
const OAuth = require('oauth-1.0a');
const crypto = require('crypto');
const request = require("request");
const OAuth = require("oauth-1.0a");
const crypto = require("crypto");

module.exports = NetSuiteOAuth;

Expand All @@ -17,108 +17,128 @@ module.exports = NetSuiteOAuth;
* @param tokenId
* @param tokenSecret
* @param account
* @param algorithm
* @param signatureMethod
* @returns {PromiseLike<ArrayBuffer>}
* @constructor
*/
function NetSuiteOAuth(url, method, consumerKey, consumerSecret, tokenId, tokenSecret, account) {
this.oauth = OAuth({
consumer: {
key: consumerKey,
secret: consumerSecret
},
realm: account,
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
});
function NetSuiteOAuth(
url,
method,
consumerKey,
consumerSecret,
tokenId,
tokenSecret,
account,
algorithm = "sha1",
signatureMethod = "HMAC-SHA1"
) {
this.oauth = OAuth({
consumer: {
key: consumerKey,
secret: consumerSecret,
},
realm: account,
signature_method: signatureMethod,
hash_function(base_string, key) {
return crypto
.createHmac(algorithm, key)
.update(base_string)
.digest("base64");
},
});

this.request_data = {
url: url,
method: method
};
this.request_data = {
url: url,
method: method,
};

this.token = {
key: tokenId,
secret: tokenSecret
};
this.token = {
key: tokenId,
secret: tokenSecret,
};

this.headers = this.oauth.toHeader(this.oauth.authorize(this.request_data, this.token));
this.headers['Content-Type'] = 'application/json';
this.headers = this.oauth.toHeader(
this.oauth.authorize(this.request_data, this.token)
);
this.headers["Content-Type"] = "application/json";
}

NetSuiteOAuth.prototype.get = function() {
return new Promise((resolve, reject) => {
request({
url: this.request_data.url,
method: this.request_data.method,
headers: this.headers
}, function(error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || error);
}
else {
if (typeof body == 'string') body = JSON.parse(body);
resolve(body || error);
}
});

});
NetSuiteOAuth.prototype.get = function () {
return new Promise((resolve, reject) => {
request(
{
url: this.request_data.url,
method: this.request_data.method,
headers: this.headers,
},
function (error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log("Body data:", body);
reject(body || error);
} else {
if (typeof body == "string") body = JSON.parse(body);
resolve(body || error);
}
}
);
});
};

NetSuiteOAuth.prototype.post = function(data) {
return new Promise((resolve, reject) => {
request({
url: this.request_data.url,
method: this.request_data.method,
json: data,
headers: this.headers
}, function(error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || error);
}
else {
if (typeof body == 'string') {
try {
body = JSON.parse(body);
} catch (error){
console.log('unable to parse response body');
reject(error);
}
}
resolve(body || error);
NetSuiteOAuth.prototype.post = function (data) {
return new Promise((resolve, reject) => {
request(
{
url: this.request_data.url,
method: this.request_data.method,
json: data,
headers: this.headers,
},
function (error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log("Body data:", body);
reject(body || error);
} else {
if (typeof body == "string") {
try {
body = JSON.parse(body);
} catch (error) {
console.log("unable to parse response body");
reject(error);
}
});

});
}
resolve(body || error);
}
}
);
});
};

NetSuiteOAuth.prototype.put = function(data) {
return new Promise((resolve, reject) => {
request({
url: this.request_data.url,
method: this.request_data.method,
json: data,
headers: this.headers
}, function(error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log('Body data:', body);
reject(body || error);
}
else {
if (typeof body == 'string') {
try {
body = JSON.parse(body);
} catch (error){
console.log('unable to parse response body');
reject(error);
}
}
resolve(body || error);
NetSuiteOAuth.prototype.put = function (data) {
return new Promise((resolve, reject) => {
request(
{
url: this.request_data.url,
method: this.request_data.method,
json: data,
headers: this.headers,
},
function (error, response, body) {
if (error || response.statusCode.toString()[0] != 2) {
console.log("Body data:", body);
reject(body || error);
} else {
if (typeof body == "string") {
try {
body = JSON.parse(body);
} catch (error) {
console.log("unable to parse response body");
reject(error);
}
});

});
}
resolve(body || error);
}
}
);
});
};
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "netsuite-tba-oauth",
"version": "0.8.0",
"version": "0.8.1",
"description": "NetSuite Token Based Authentication Module",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/RezolveTecnologia/netsuite-tba-oauth.git"
"url": "git+https://github.com/husseinmkwizu/netsuite-tba-oauth.git"
},
"keywords": [
"tba",
Expand All @@ -17,11 +17,14 @@
"restlet"
],
"author": "Jaime Ramos",
"contributors": [
"Hussein Mkwizu"
],
"license": "ISC",
"bugs": {
"url": "https://github.com/RezolveTecnologia/netsuite-tba-oauth/issues"
"url": "https://github.com/husseinmkwizu/netsuite-tba-oauth/issues"
},
"homepage": "https://github.com/RezolveTecnologia/netsuite-tba-oauth#readme",
"homepage": "https://github.com/husseinmkwizu/netsuite-tba-oauth#readme",
"dependencies": {
"oauth-1.0a": "^2.2.4",
"request": "^2.88.0"
Expand Down

0 comments on commit 6d784f0

Please sign in to comment.