Skip to content

Commit a67aa34

Browse files
addressed pr comments
1 parent 9c59501 commit a67aa34

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/authentication/core/MerchantConfig.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var ApiException = require('../util/ApiException');
66
var LogConfiguration = require('../logging/LogConfiguration');
77
var path = require('path');
88
var fs = require('fs');
9+
var path = require('path');
10+
var fs = require('fs');
911

1012
/**
1113
* This function has all the merchentConfig properties getters and setters methods
@@ -426,6 +428,10 @@ MerchantConfig.prototype.setMleForRequestPublicCertPath = function setMleForRequ
426428
this.mleForRequestPublicCertPath = mleForRequestPublicCertPath;
427429
}
428430

431+
MerchantConfig.prototype.getP12FilePath = function getP12FilePath() {
432+
return path.resolve(path.join(this.getKeysDirectory(), this.getKeyFileName() + '.p12'));
433+
}
434+
429435
MerchantConfig.prototype.runEnvironmentCheck = function runEnvironmentCheck(logger) {
430436

431437
/*url*/
@@ -575,6 +581,11 @@ MerchantConfig.prototype.defaultPropValues = function defaultPropValues() {
575581
this.keyFilename = this.merchantID;
576582
logger.warn(Constants.KEY_FILE_EMPTY);
577583
}
584+
try {
585+
fs.accessSync(this.getP12FilePath(), fs.constants.R_OK);
586+
} catch (err) {
587+
ApiException.ApiException("Merchant p12 certificate file not found or not readable: " + this.getP12FilePath());
588+
}
578589
}
579590
else if (this.authenticationType.toLowerCase() === Constants.OAUTH)
580591
{
@@ -642,13 +653,25 @@ MerchantConfig.prototype.defaultPropValues = function defaultPropValues() {
642653
// }
643654
// }
644655
}
656+
if (this.mleForRequestPublicCertPath) {
657+
// First check if the file exists and is readable
658+
try {
659+
fs.accessSync(this.mleForRequestPublicCertPath, fs.constants.R_OK);
660+
} catch (err) {
661+
const errorType = err.code === 'ENOENT' ? 'does not exist' : 'is not readable';
662+
ApiException.ApiException(`mleForRequestPublicCertPath file ${errorType}: ${this.mleForRequestPublicCertPath} (${err.message})`, logger);
663+
}
645664

646-
if (this.mleForRequestPublicCertPath !== null && this.mleForRequestPublicCertPath !== undefined) {
647-
var certFile = path.resolve(path.join(this.mleForRequestPublicCertPath));
665+
let stats;
648666
try {
649-
fs.accessSync(certFile, fs.constants.R_OK);
667+
stats = fs.statSync(this.mleForRequestPublicCertPath);
650668
} catch (err) {
651-
ApiException.ApiException("mleForRequestPublicCertPath file is not readable or does not exist" + ": " + certFile, logger);
669+
ApiException.ApiException(`Error checking file stats for mleForRequestPublicCertPath: ${this.mleForRequestPublicCertPath} (${err.message})`, logger);
670+
}
671+
672+
// Check if it's a file
673+
if (stats.isFile() === false) {
674+
ApiException.ApiException(`mleForRequestPublicCertPath is not a file: ${this.mleForRequestPublicCertPath}`, logger);
652675
}
653676
}
654677

src/authentication/util/Cache.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ var ApiException = require('./ApiException');
99
var Logger = require('../logging/Logger');
1010
var Utility = require('./Utility');
1111

12+
function loadP12FileToAsn1(filePath) {
13+
var p12Buffer = fs.readFileSync(filePath);
14+
var p12Der = forge.util.binary.raw.encode(new Uint8Array(p12Buffer));
15+
var p12Asn1 = forge.asn1.fromDer(p12Der);
16+
return p12Asn1;
17+
}
18+
1219

1320
/**
1421
* This module is doing Caching.
@@ -19,7 +26,7 @@ exports.fetchCachedCertificate = function (merchantConfig, logger) {
1926
var cachedCertificateFromP12File = cache.get("certificateFromP12File");
2027
var cachedLastModifiedTimeStamp = cache.get("certificateLastModifideTimeStamp");
2128

22-
var filePath = path.resolve(path.join(merchantConfig.getKeysDirectory(), merchantConfig.getKeyFileName() + '.p12'));
29+
var filePath = merchantConfig.getP12FilePath();
2330
if (fs.existsSync(filePath)) {
2431
const stats = fs.statSync(filePath);
2532
const currentFileLastModifiedTime = stats.mtime;
@@ -48,9 +55,7 @@ exports.fetchCachedCertificate = function (merchantConfig, logger) {
4855
//Function to read the file and put values to new cache
4956
function getCertificate(keyPass, filePath, fileLastModifiedTime, logger) {
5057
try {
51-
var p12Buffer = fs.readFileSync(filePath);
52-
var p12Der = forge.util.binary.raw.encode(new Uint8Array(p12Buffer));
53-
var p12Asn1 = forge.asn1.fromDer(p12Der);
58+
var p12Asn1 = loadP12FileToAsn1(filePath);
5459
var certificate = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, keyPass);
5560
cache.put("certificateFromP12File", certificate);
5661
cache.put("certificateLastModifideTimeStamp", fileLastModifiedTime);
@@ -90,13 +95,7 @@ exports.getRequestMLECertFromCache = function(merchantConfig) {
9095
cacheKey = merchantId + Constants.MLE_CACHE_IDENTIFIER_FOR_CONFIG_CERT;
9196
mleCertPath = merchantConfig.getMleForRequestPublicCertPath();
9297
} else if (Constants.JWT === merchantConfig.getAuthenticationType().toLowerCase()) {
93-
mleCertPath = path.resolve(path.join(merchantConfig.getKeysDirectory(), merchantConfig.getKeyFileName() + '.p12'));
94-
try {
95-
fs.accessSync(mleCertPath, fs.constants.R_OK);
96-
} catch (err) {
97-
logger.warn("MLE certificate file not found or not readable: " + mleCertPath);
98-
return null;
99-
}
98+
mleCertPath = merchantConfig.getP12FilePath();
10099
cacheKey = merchantId + Constants.MLE_CACHE_IDENTIFIER_FOR_P12_CERT;
101100
} else {
102101
logger.debug("The certificate to use for MLE for requests is not provided in the merchant configuration. Please ensure that the certificate path is provided.");
@@ -138,10 +137,8 @@ function setupMLECache(merchantConfig, cacheKey, mleCertPath) {
138137
function loadCertificateFromP12(merchantConfig, mleCertPath) {
139138
const logger = Logger.getLogger(merchantConfig, 'Cache');
140139
try {
141-
// Read the P12 file as before
142-
var p12Buffer = fs.readFileSync(mleCertPath);
143-
var p12Der = forge.util.binary.raw.encode(new Uint8Array(p12Buffer));
144-
var p12Asn1 = forge.asn1.fromDer(p12Der);
140+
// Read the P12 file and convert to ASN1
141+
var p12Asn1 = loadP12FileToAsn1(mleCertPath);
145142
var p12Cert = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, merchantConfig.getKeyPass());
146143

147144
// Extract the certificate from the P12 container

0 commit comments

Comments
 (0)