Changed implementation of __parsePEM; Now it does not return anymore an object but the value;

Moved __parsePEM outside of Pass class as parsePEM function;
This commit is contained in:
alexandercerutti
2018-08-23 22:34:16 +02:00
parent 7d9cd5e1af
commit 137b9ab841

View File

@@ -583,8 +583,9 @@ class Pass {
Object.assign(this.props, this._filterOptions(options.overrides));
let certPaths = Object.keys(options.certificates)
.map((val) => {
let optCertsNames = Object.keys(options.certificates);
let certPaths = optCertsNames.map((val) => {
const cert = options.certificates[val];
const filePath = !(cert instanceof Object) ? cert : cert["keyFile"];
const resolvedPath = path.resolve(filePath);
@@ -593,41 +594,38 @@ class Pass {
});
return Promise.all(certPaths).then(contents => {
contents.forEach(file => {
let pem = this.__parsePEM(file, options.certificates.signerKey.passphrase);
contents.forEach((file, index) => {
let certName = optCertsNames[index];
let pem = parsePEM(file, options.certificates[certName].passphrase);
if (!pem) {
return reject(errors.INVALID_CERTS)
}
this.Certificates[pem.key] = pem.value;
this.Certificates[certName] = pem;
});
});
}
}
/**
/**
* Parses the PEM-formatted passed text (certificates)
*
* @method __parsePEM
* @function parsePEM
* @params {String} element - Text content of .pem files
* @params {String} passphrase - passphrase for the key
* @returns {Object} - Object containing name of the certificate and its parsed content
* @params {String=} passphrase - passphrase for the key
* @returns {Object} The parsed certificate or key in node forge format
*/
__parsePEM(element, passphrase) {
if (element.includes("PRIVATE KEY") && !!passphrase) {
return {
key: "signerKey",
value: forge.pki.decryptRsaPrivateKey(element, String(passphrase))
};
function parsePEM(element, passphrase) {
if (element.includes("PRIVATE KEY") && passphrase) {
return forge.pki.decryptRsaPrivateKey(element, String(passphrase));
} else if (element.includes("CERTIFICATE")) {
// PEM-exported certificates with keys are in PKCS#12 format, hence they are composed of bags.
return {
key: element.includes("Bag Attributes") ? "signerCert" : "wwdr",
value: forge.pki.certificateFromPem(element)
};
return forge.pki.certificateFromPem(element);
} else {
return {};
}
return null;
}
}