From 7c159cb1a88f50fdb1ccdad564bd7da685be8226 Mon Sep 17 00:00:00 2001 From: John Wynn Date: Tue, 7 Mar 2023 08:41:50 -0600 Subject: [PATCH 1/5] Initial try --- src/ApiClient.js | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 8d140469d..4b7d4e073 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -8,13 +8,13 @@ * NOTE: This class is auto generated. Do not edit the class manually and submit a new issue instead. * */ -(function(root, factory) { +(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. - define(['superagent'], factory); + define(['superagent', 'superagent-proxy'], factory); } else if (typeof module === 'object' && module.exports) { // CommonJS-like environments that support module.exports, like Node. - module.exports = factory(require('superagent')); + module.exports = factory(require('superagent'), require('superagent-proxy')); } else { // Browser globals (root is window) if (!root.Docusign) { @@ -22,9 +22,10 @@ } root.Docusign.ApiClient = factory(root.superagent, opts); } -}(this, function(superagent, opts) { +}(this, function(superagent, proxy, opts) { 'use strict'; - + const proxyUrl = process.env.HTTP_PROXY; + var SCOPE_SIGNATURE = "signature"; var SCOPE_EXTENDED = "extended"; var SCOPE_IMPERSONATION = "impersonation"; @@ -64,9 +65,19 @@ } return jwt.sign(jwtPayload, privateKey, { algorithm: JWT_SIGNING_ALGO }); }; + + const generateRequest = function (saRequest, proxyUrl) { + if (proxyUrl) { + proxy(saRequest, proxyUrl) + } + + return saRequest; + } var sendJWTTokenRequest = function (assertion, oAuthBasePath, callback) { + var request = superagent.post("https://" + oAuthBasePath + "/oauth/token") + generateRequest(request, proxyUrl) .timeout(exports.prototype.timeout) .set('Content-Type', 'application/x-www-form-urlencoded') .set('Cache-Control', 'no-store') @@ -489,13 +500,23 @@ * @param {module:ApiClient~callApiCallback} callback The callback function. If this is left undefined, this method will return a promise instead. * @returns {Object} The SuperAgent request object if a callback is specified, else {Promise} A {@link https://www.promisejs.org/|Promise} object. */ + const fs = require('fs'); + exports.prototype.callApi = function callApi(path, httpMethod, pathParams, - queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, + + queryParams, headerParams, formParams, bodyParam, authNames, contentTypes, accepts, returnType, callback) { var _this = this; var url = this.buildUrl(path, pathParams); - var request = superagent(httpMethod, url); + + debugger; + var saRequest = superagent(httpMethod, url); + + let ca = fs.readFileSync('./sftrust.crt'); // should be the + + saRequest.ca(ca); + var request = generateRequest(saRequest, proxyUrl); var _formParams = this.normalizeParams(formParams); var body = httpMethod.toUpperCase() === 'GET' && !bodyParam ? undefined : bodyParam || {}; @@ -759,6 +780,7 @@ }, OAuthToken = require('./OAuth').OAuthToken, request = superagent.post("https://" + this.getOAuthBasePath() + "/oauth/token") + generateRequest(request, proxyUrl) .send(postData) .set(headers) .type("application/x-www-form-urlencoded"); @@ -799,7 +821,8 @@ "Pragma": "no-cache" } - var request = superagent.get("https://" + this.getOAuthBasePath() + "/oauth/userinfo").set(headers); + var request = superagent.get("https://" + this.getOAuthBasePath() + "/oauth/userinfo") + generateRequest(request, proxyUrl).set(headers); var UserInfo = require('./OAuth').UserInfo; From 4c7e897a5518cff114ee421893059cc2d5c71903 Mon Sep 17 00:00:00 2001 From: John Wynn Date: Tue, 7 Mar 2023 09:07:06 -0600 Subject: [PATCH 2/5] updated function to dynamically include CA cert --- src/ApiClient.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 4b7d4e073..8ea46ac36 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -24,7 +24,6 @@ } }(this, function(superagent, proxy, opts) { 'use strict'; - const proxyUrl = process.env.HTTP_PROXY; var SCOPE_SIGNATURE = "signature"; var SCOPE_EXTENDED = "extended"; @@ -66,9 +65,16 @@ return jwt.sign(jwtPayload, privateKey, { algorithm: JWT_SIGNING_ALGO }); }; - const generateRequest = function (saRequest, proxyUrl) { - if (proxyUrl) { - proxy(saRequest, proxyUrl) + const generateRequest = function (saRequest, shouldProxy = false, caCert = false) { + const PROXY_URL = process.env.HTTP_PROXY; + const CA_CERT = process.env.CA_CERT; + if (caCert && CA_CERT) { + let ca = fs.readFileSync(CA_CERT); // should be the + saRequest.ca(ca); + } + + if (shouldProxy && PROXY_URL) { + proxy(saRequest, PROXY_URL) } return saRequest; @@ -77,7 +83,7 @@ var sendJWTTokenRequest = function (assertion, oAuthBasePath, callback) { var request = superagent.post("https://" + oAuthBasePath + "/oauth/token") - generateRequest(request, proxyUrl) + generateRequest(request, true) .timeout(exports.prototype.timeout) .set('Content-Type', 'application/x-www-form-urlencoded') .set('Cache-Control', 'no-store') @@ -513,10 +519,7 @@ debugger; var saRequest = superagent(httpMethod, url); - let ca = fs.readFileSync('./sftrust.crt'); // should be the - - saRequest.ca(ca); - var request = generateRequest(saRequest, proxyUrl); + var request = generateRequest(saRequest, true, true); var _formParams = this.normalizeParams(formParams); var body = httpMethod.toUpperCase() === 'GET' && !bodyParam ? undefined : bodyParam || {}; @@ -780,7 +783,7 @@ }, OAuthToken = require('./OAuth').OAuthToken, request = superagent.post("https://" + this.getOAuthBasePath() + "/oauth/token") - generateRequest(request, proxyUrl) + generateRequest(request, true) .send(postData) .set(headers) .type("application/x-www-form-urlencoded"); @@ -822,7 +825,7 @@ } var request = superagent.get("https://" + this.getOAuthBasePath() + "/oauth/userinfo") - generateRequest(request, proxyUrl).set(headers); + generateRequest(request, true).set(headers); var UserInfo = require('./OAuth').UserInfo; From d55e151fe0b706e380d7232ae66b8b31443c2fba Mon Sep 17 00:00:00 2001 From: John Wynn Date: Tue, 7 Mar 2023 09:17:27 -0600 Subject: [PATCH 3/5] added superagent-proxy --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c9beeff05..a9ae9d029 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,8 @@ "jsonwebtoken": "^9.0.0", "passport-oauth2": "^1.6.1", "safe-buffer": "^5.1.2", - "superagent": "3.8.2" + "superagent": "3.8.2", + "superagent-proxy": "^3.0.0" }, "devDependencies": { "docdash": "0.4.0", From ca77299b530a1323100f41a63dd903ca2e6bcea5 Mon Sep 17 00:00:00 2001 From: John Wynn Date: Tue, 7 Mar 2023 09:56:28 -0600 Subject: [PATCH 4/5] updating to use NODE_EXTRA_CA_CERTS --- src/ApiClient.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 8ea46ac36..93c78c179 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -65,13 +65,13 @@ return jwt.sign(jwtPayload, privateKey, { algorithm: JWT_SIGNING_ALGO }); }; - const generateRequest = function (saRequest, shouldProxy = false, caCert = false) { + const generateRequest = function (saRequest, shouldProxy = false) { const PROXY_URL = process.env.HTTP_PROXY; - const CA_CERT = process.env.CA_CERT; - if (caCert && CA_CERT) { - let ca = fs.readFileSync(CA_CERT); // should be the - saRequest.ca(ca); - } + // const CA_CERT = process.env.CA_CERT; + // if (caCert && CA_CERT) { + // let ca = fs.readFileSync(CA_CERT); // should be the + // saRequest.ca(ca); + // } if (shouldProxy && PROXY_URL) { proxy(saRequest, PROXY_URL) From 2050c2318df636c85075e022f370f8f50a468b38 Mon Sep 17 00:00:00 2001 From: John Wynn Date: Tue, 7 Mar 2023 10:18:21 -0600 Subject: [PATCH 5/5] update to clean code --- src/ApiClient.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 93c78c179..6317c629c 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -65,15 +65,10 @@ return jwt.sign(jwtPayload, privateKey, { algorithm: JWT_SIGNING_ALGO }); }; - const generateRequest = function (saRequest, shouldProxy = false) { + const configureProxy = function (saRequest) { const PROXY_URL = process.env.HTTP_PROXY; - // const CA_CERT = process.env.CA_CERT; - // if (caCert && CA_CERT) { - // let ca = fs.readFileSync(CA_CERT); // should be the - // saRequest.ca(ca); - // } - if (shouldProxy && PROXY_URL) { + if (PROXY_URL) { proxy(saRequest, PROXY_URL) } @@ -83,7 +78,7 @@ var sendJWTTokenRequest = function (assertion, oAuthBasePath, callback) { var request = superagent.post("https://" + oAuthBasePath + "/oauth/token") - generateRequest(request, true) + configureProxy(request) .timeout(exports.prototype.timeout) .set('Content-Type', 'application/x-www-form-urlencoded') .set('Cache-Control', 'no-store') @@ -516,10 +511,9 @@ var _this = this; var url = this.buildUrl(path, pathParams); - debugger; var saRequest = superagent(httpMethod, url); - var request = generateRequest(saRequest, true, true); + var request = configureProxy(saRequest); var _formParams = this.normalizeParams(formParams); var body = httpMethod.toUpperCase() === 'GET' && !bodyParam ? undefined : bodyParam || {}; @@ -783,7 +777,7 @@ }, OAuthToken = require('./OAuth').OAuthToken, request = superagent.post("https://" + this.getOAuthBasePath() + "/oauth/token") - generateRequest(request, true) + configureProxy(request) .send(postData) .set(headers) .type("application/x-www-form-urlencoded"); @@ -825,7 +819,7 @@ } var request = superagent.get("https://" + this.getOAuthBasePath() + "/oauth/userinfo") - generateRequest(request, true).set(headers); + configureProxy(request).set(headers); var UserInfo = require('./OAuth').UserInfo; @@ -906,6 +900,7 @@ var assertion = jwt.sign(jwt_payload, private_key, {algorithm: 'RS256'}); superagent('post', 'https://' + this.getOAuthBasePath() + '/oauth/token') + configureProxy(request) .timeout(this.timeout) .set('Content-Type', 'application/x-www-form-urlencoded') .set('Cache-Control', 'no-store')