Skip to content

Commit b69c2fb

Browse files
accepting string/jwk object as mle repsone private key
1 parent 4414e13 commit b69c2fb

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"promise": "^8.3.0",
2626
"winston": "^3.11.0",
2727
"winston-daily-rotate-file": "^4.7.1",
28-
"node-jose": "^2.2.0"
28+
"node-jose": "^2.2.0",
29+
"jwk-to-pem": "^2.0.7"
2930
},
3031
"keywords": [
3132
"nodeJS"

src/authentication/core/MerchantConfig.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function MerchantConfig(result) {
147147
* PrivateKey instance used for Response MLE decryption by the SDK.
148148
* Optional — either provide this object directly or specify the private key file path via configuration.
149149
*/
150-
this.responseMlePrivateKey = result.responseMlePrivateKey;
150+
this.setResponseMlePrivateKey(result.responseMlePrivateKey);
151151

152152

153153
this.mapToControlMLEonAPI = result.mapToControlMLEonAPI;
@@ -563,7 +563,23 @@ MerchantConfig.prototype.getResponseMlePrivateKey = function getResponseMlePriva
563563
}
564564

565565
MerchantConfig.prototype.setResponseMlePrivateKey = function setResponseMlePrivateKey(responseMlePrivateKey) {
566-
this.responseMlePrivateKey = responseMlePrivateKey;
566+
var logger = Logger.getLogger(this, 'MerchantConfig');
567+
568+
if (responseMlePrivateKey) {
569+
logger.debug('Processing response MLE private key');
570+
571+
try {
572+
// Use synchronous version of parseAndReturnPem
573+
const pemKey = Utility.parseAndReturnPem(responseMlePrivateKey, logger);
574+
logger.debug('Successfully parsed response MLE private key');
575+
this.responseMlePrivateKey = pemKey;
576+
} catch (error) {
577+
logger.error(`Error parsing response MLE private key: ${error.message}`);
578+
throw new ApiException.ApiException(`Error parsing response MLE private key: ${error.message}`, logger);
579+
}
580+
} else {
581+
this.responseMlePrivateKey = responseMlePrivateKey;
582+
}
567583
}
568584

569585
MerchantConfig.prototype.getInternalMapToControlResponseMLEonAPI = function getInternalMapToControlResponseMLEonAPI() {

src/authentication/util/Utility.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var ApiException = require('./ApiException');
44
var Constants = require('./Constants');
55
var fs = require('fs');
66
var forge = require('node-forge');
7+
var jwkToPem = require('jwk-to-pem');
78

89
exports.getResponseCodeMessage = function (responseCode) {
910

@@ -277,3 +278,41 @@ exports.readPrivateKeyFromPemFile = function(filePath, password, logger) {
277278
ApiException.AuthException(`Error loading private key from PEM file: ${filePath}: ${error.message}`);
278279
}
279280
};
281+
282+
exports.parseAndReturnPem = function(key, logger) {
283+
logger.debug(`Parsing private key to PEM format synchronously, key type: ${typeof key}`);
284+
285+
if (typeof key === 'string') {
286+
logger.debug('Processing string key as potential PEM private key');
287+
try {
288+
// Validate it's a valid private key PEM
289+
forge.pki.privateKeyFromPem(key);
290+
logger.debug('Successfully validated private key PEM format');
291+
return key;
292+
} catch (error) {
293+
logger.error(`Invalid private key PEM format: ${error.message}`);
294+
throw new Error('Invalid private key PEM format');
295+
}
296+
} else if (typeof key === 'object' && key !== null) {
297+
logger.debug('Processing object key as potential JWK private key');
298+
try {
299+
// Check if it has the 'd' property which indicates a private key
300+
if (!key.d) {
301+
logger.error('JWK object is not a private key (missing d parameter)');
302+
throw new Error('JWK object is not a private key');
303+
}
304+
305+
// Convert JWK to PEM (private key)
306+
logger.debug('Converting JWK to private key PEM');
307+
const pem = jwkToPem(key, { private: true });
308+
logger.debug('Successfully converted JWK to private key PEM format');
309+
return pem;
310+
} catch (error) {
311+
logger.error(`Invalid JWK private key object: ${error.message}`);
312+
throw new Error('Invalid JWK private key object');
313+
}
314+
} else {
315+
logger.error(`Unsupported key format: ${typeof key}`);
316+
throw new Error('Unsupported key format');
317+
}
318+
}

0 commit comments

Comments
 (0)