Skip to content

Commit 8bbb2ef

Browse files
committed
Ensured proxy agent is not replaced by client certificate
1 parent c8ee305 commit 8bbb2ef

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

generator/cybersource-javascript-template/ApiClient.mustache

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@
390390
tester.response.req.headers = error.response.config.headers;
391391
tester.response.header = error.response.headers;
392392
tester.response.status = error.response.status;
393-
393+
394394
try {
395-
// 404 is caused by missing resource / resource not found
395+
// 404 is caused by missing resource / resource not found
396396
tester.response.text = JSON.stringify(error.response.data);
397397
} catch(err) {
398-
// 404 is caused by unexpected end of stream or stream not found
398+
// 404 is caused by unexpected end of stream or stream not found
399399
tester.response.text = error.message;
400400
}
401401
return tester;
@@ -433,20 +433,20 @@
433433
tester.text = JSON.stringify(response.data);
434434
return tester;
435435
}
436-
436+
437437
// Code added by Infosys dev team
438438

439439
/**
440440
* This method will set the merchantConfig object global
441-
*
441+
*
442442
* @param {Configuration} configObject merchantConfiguration properties.
443443
*/
444444
exports.prototype.setConfiguration = function (configObject) {
445445
446446
this.merchantConfig = new MerchantConfig(configObject);
447447
this.constants = Constants;
448448
if(this.merchantConfig.getIntermediateHost()) {
449-
if(this.merchantConfig.getIntermediateHost().startsWith(this.constants.HTTP_URL_PREFIX) ||
449+
if(this.merchantConfig.getIntermediateHost().startsWith(this.constants.HTTP_URL_PREFIX) ||
450450
this.merchantConfig.getIntermediateHost().startsWith('http://')) {
451451
this.basePath = this.merchantConfig.getIntermediateHost();
452452
} else {
@@ -460,17 +460,17 @@
460460

461461
/**
462462
* This method is to generate headers for http and jwt authentication.
463-
*
464-
* @param {String} httpMethod
465-
* @param {String} requestTarget
466-
* @param {String} requestBody
463+
*
464+
* @param {String} httpMethod
465+
* @param {String} requestTarget
466+
* @param {String} requestBody
467467
*/
468468
exports.prototype.callAuthenticationHeader = function (httpMethod, requestTarget, requestBody, headerParams) {
469469
470470
this.merchantConfig.setRequestTarget(requestTarget);
471471
this.merchantConfig.setRequestType(httpMethod)
472472
this.merchantConfig.setRequestJsonData(requestBody);
473-
473+
474474
this.logger.info('Authentication Type : ' + this.merchantConfig.getAuthenticationType());
475475
this.logger.info(this.constants.REQUEST_TYPE + ' : ' + httpMethod.toUpperCase());
476476
@@ -479,8 +479,8 @@
479479
// var clientId = getClientId();
480480
481481
// headerParams['v-c-client-id'] = clientId;
482-
483-
// if (this.merchantConfig.getSolutionId() != null && this.merchantConfig.getSolutionId() != '') {
482+
483+
// if (this.merchantConfig.getSolutionId() != null && this.merchantConfig.getSolutionId() != '') {
484484
// headerParams['v-c-solution-id'] = this.merchantConfig.getSolutionId();
485485
// }
486486

@@ -527,7 +527,7 @@
527527
var packageInfo = require('./../package.json');
528528
return "cybs-rest-sdk-node-" + packageInfo.version;
529529
}
530-
530+
531531
{{#emitJSDoc}}{{^usePromises}} /**
532532
* Callback function to receive the result of the operation.
533533
* @callback module:{{#invokerPackage}}{{invokerPackage}}/{{/invokerPackage}}ApiClient~callApiCallback
@@ -578,7 +578,7 @@
578578

579579
if (useProxy && (proxyAddress != null && proxyAddress != '')) {
580580
if ((proxyUser != null && proxyUser != '') && (proxyPassword!= null && proxyPassword != '')) {
581-
var proxy = process.env.http_proxy || 'http://' + proxyUser + ':' + proxyPassword + '@' + proxyAddress + ':' + proxyPort;
581+
var proxy = process.env.http_proxy || 'http://' + proxyUser + ':' + proxyPassword + '@' + proxyAddress + ':' + proxyPort;
582582
}
583583
else {
584584
var proxy = process.env.http_proxy || 'http://' + proxyAddress + ':' + proxyPort;
@@ -594,10 +594,16 @@
594594
if(enableClientCert) {
595595
var certFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getSSLClientCert()));
596596
var keyFile = pathlib.resolve(pathlib.join(this.merchantConfig.getClientCertDir(), this.merchantConfig.getPrivateKey()));
597-
axiosConfig.httpsAgent = new https.Agent({
598-
cert: fslib.readFileSync(certFile),
599-
key: fslib.readFileSync(keyFile)
600-
});
597+
598+
if (axiosConfig.httpsAgent) {
599+
axiosConfig.httpsAgent.cert = fslib.readFileSync(certFile);
600+
axiosConfig.httpsAgent.key = fslib.readFileSync(keyFile);
601+
} else {
602+
axiosConfig.httpsAgent = new https.Agent({
603+
cert: fslib.readFileSync(certFile),
604+
key: fslib.readFileSync(keyFile)
605+
});
606+
}
601607
}
602608

603609
// apply authentications
@@ -712,8 +718,8 @@
712718
if (this.enableCookies){
713719
axiosConfig.jar = this.cookieJar;
714720
}
715-
716-
axiosConfig.url = requestTarget;
721+
722+
axiosConfig.url = requestTarget;
717723

718724
{{#usePromises}} return axios.request(axiosConfig).then(function(response) {
719725
try {
@@ -722,8 +728,8 @@
722728
723729
resolve({data: data, response: response});
724730
} catch(err) {
725-
reject(err);
726-
}
731+
reject(err);
732+
}
727733
}).catch(function(error, response) {
728734
source.cancel('Stream ended.');
729735
var userError = {};
@@ -761,16 +767,16 @@
761767

762768
callback(userError, null, response);
763769
});
764-
770+
765771
process.on('uncaughtException', (reason, p) => {
766772
// console.log('Uncaught exception at Promise :' + p + ' with Reason : ' + reason);
767773
});
768774
{{/usePromises}} };
769775

770776
/**
771777
* Build request target required for the signature generation
772-
* @param {String} path
773-
* @param {Object} pathParams
778+
* @param {String} path
779+
* @param {Object} pathParams
774780
*/
775781
exports.prototype.buildRequestTarget = function (path, pathParams, queryParams) {
776782
if (!path.match(/^\//)) {
@@ -805,7 +811,7 @@
805811
}
806812
return requestTarget;
807813
};
808-
814+
809815
{{#emitJSDoc}} /**
810816
* Parses an ISO-8601 string representation of a date value.
811817
* @param {String} str The date value as a string.

0 commit comments

Comments
 (0)