Skip to content

Commit

Permalink
Adjust code to pass eslint curly (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbarth authored Jun 15, 2023
1 parent 329e427 commit b425626
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"one-var": ["error", "never"],
"no-duplicate-imports": "error",
"no-use-before-define": "error",
"curly": "warn",
"curly": "error",
"eqeqeq": ["warn", "smart"],
"no-var": "warn",
"prefer-const": "warn"
Expand Down
11 changes: 8 additions & 3 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ function validateXml(xml, key) {
sig.keyInfoProvider = new FileKeyInfo(key);
sig.loadSignature(signature.toString());
var res = sig.checkSignature(xml);
if (!res) console.log(sig.validationErrors);
if (!res) {
console.log(sig.validationErrors);
}
return res;
}

Expand All @@ -39,5 +41,8 @@ var signedXml = fs.readFileSync("result.xml").toString();
console.log("validating signature...");

//validate an xml document
if (validateXml(signedXml, "client_public.pem")) console.log("signature is valid");
else console.log("signature not valid");
if (validateXml(signedXml, "client_public.pem")) {
console.log("signature is valid");
} else {
console.log("signature not valid");
}
14 changes: 10 additions & 4 deletions lib/c14n-canonicalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ C14nCanonicalization.prototype.attrCompare = function (a, b) {
var left = a.namespaceURI + a.localName;
var right = b.namespaceURI + b.localName;

if (left === right) return 0;
else if (left < right) return -1;
else return 1;
if (left === right) {
return 0;
} else if (left < right) {
return -1;
} else {
return 1;
}
};

C14nCanonicalization.prototype.nsCompare = function (a, b) {
Expand Down Expand Up @@ -138,7 +142,9 @@ C14nCanonicalization.prototype.renderNs = function (
if (Array.isArray(ancestorNamespaces) && ancestorNamespaces.length > 0) {
// Remove namespaces which are already present in nsListToRender
for (var p1 in ancestorNamespaces) {
if (!ancestorNamespaces.hasOwnProperty(p1)) continue;
if (!ancestorNamespaces.hasOwnProperty(p1)) {
continue;
}
var alreadyListed = false;
for (var p2 in nsListToRender) {
if (
Expand Down
8 changes: 6 additions & 2 deletions lib/enveloped-signature.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ EnvelopedSignature.prototype.process = function (node, options) {
"./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']",
node
)[0];
if (signature) signature.parentNode.removeChild(signature);
if (signature) {
signature.parentNode.removeChild(signature);
}
return node;
}
var signatureNode = options.signatureNode;
Expand All @@ -23,7 +25,9 @@ EnvelopedSignature.prototype.process = function (node, options) {
node
);
for (var h in signatures) {
if (!signatures.hasOwnProperty(h)) continue;
if (!signatures.hasOwnProperty(h)) {
continue;
}
var nodeSignature = signatures[h];
var signatureValue = utils.findFirst(
nodeSignature,
Expand Down
10 changes: 7 additions & 3 deletions lib/exclusive-canonicalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ ExclusiveCanonicalization.prototype.attrCompare = function (a, b) {
var left = a.namespaceURI + a.localName;
var right = b.namespaceURI + b.localName;

if (left === right) return 0;
else if (left < right) return -1;
else return 1;
if (left === right) {
return 0;
} else if (left < right) {
return -1;
} else {
return 1;
}
};

ExclusiveCanonicalization.prototype.nsCompare = function (a, b) {
Expand Down
120 changes: 88 additions & 32 deletions lib/signed-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ function RSASHA1() {
var signer = crypto.createSign("RSA-SHA1");
signer.update(signedInfo);
var res = signer.sign(signingKey, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand All @@ -76,7 +78,9 @@ function RSASHA1() {
var verifier = crypto.createVerify("RSA-SHA1");
verifier.update(str);
var res = verifier.verify(key, signatureValue, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand All @@ -98,7 +102,9 @@ function RSASHA256() {
var signer = crypto.createSign("RSA-SHA256");
signer.update(signedInfo);
var res = signer.sign(signingKey, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand All @@ -110,7 +116,9 @@ function RSASHA256() {
var verifier = crypto.createVerify("RSA-SHA256");
verifier.update(str);
var res = verifier.verify(key, signatureValue, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand All @@ -132,7 +140,9 @@ function RSASHA512() {
var signer = crypto.createSign("RSA-SHA512");
signer.update(signedInfo);
var res = signer.sign(signingKey, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand All @@ -144,7 +154,9 @@ function RSASHA512() {
var verifier = crypto.createVerify("RSA-SHA512");
verifier.update(str);
var res = verifier.verify(key, signatureValue, "base64");
if (callback) callback(null, res);
if (callback) {
callback(null, res);
}
return res;
};

Expand Down Expand Up @@ -239,7 +251,9 @@ function findAncestorNs(doc, docSubsetXpath, namespaceResolver) {
var isUnique = true;
for (var k = 0; k < subsetAttributes.length; k++) {
var nodeName = subsetAttributes[k].nodeName;
if (nodeName.search(/^xmlns:/) === -1) continue;
if (nodeName.search(/^xmlns:/) === -1) {
continue;
}
var prefix = nodeName.replace(/^xmlns:/, "");
if (ancestorNsWithoutDuplicate[j].prefix === prefix) {
isUnique = false;
Expand Down Expand Up @@ -315,7 +329,9 @@ function SignedXml(idMode, options) {
this.validationErrors = [];
this.keyInfo = null;
this.idAttributes = ["Id", "ID", "id"];
if (this.options.idAttribute) this.idAttributes.splice(0, 0, this.options.idAttribute);
if (this.options.idAttribute) {
this.idAttributes.splice(0, 0, this.options.idAttribute);
}
this.implicitTransforms = this.options.implicitTransforms || [];
}

Expand Down Expand Up @@ -423,7 +439,9 @@ SignedXml.prototype.checkSignature = function (xml, callback) {

SignedXml.prototype.getCanonSignedInfoXml = function (doc) {
var signedInfo = utils.findChilds(this.signatureNode, "SignedInfo");
if (signedInfo.length == 0) throw new Error("could not find SignedInfo element in the message");
if (signedInfo.length == 0) {
throw new Error("could not find SignedInfo element in the message");
}

if (
this.canonicalizationAlgorithm === "http://www.w3.org/TR/2001/REC-xml-c14n-20010315" ||
Expand Down Expand Up @@ -469,10 +487,11 @@ SignedXml.prototype.validateSignatureValue = function (doc, callback) {
var signedInfoCanon = this.getCanonSignedInfoXml(doc);
var signer = this.findSignatureAlgorithm(this.signatureAlgorithm);
var res = signer.verifySignature(signedInfoCanon, this.signingKey, this.signatureValue, callback);
if (!res && !callback)
if (!res && !callback) {
this.validationErrors.push(
"invalid signature: the signature value " + this.signatureValue + " is incorrect"
);
}
return res;
};

Expand All @@ -484,25 +503,36 @@ SignedXml.prototype.calculateSignatureValue = function (doc, callback) {

SignedXml.prototype.findSignatureAlgorithm = function (name) {
var algo = SignedXml.SignatureAlgorithms[name];
if (algo) return new algo();
else throw new Error("signature algorithm '" + name + "' is not supported");
if (algo) {
return new algo();
} else {
throw new Error("signature algorithm '" + name + "' is not supported");
}
};

SignedXml.prototype.findCanonicalizationAlgorithm = function (name) {
var algo = SignedXml.CanonicalizationAlgorithms[name];
if (algo) return new algo();
else throw new Error("canonicalization algorithm '" + name + "' is not supported");
if (algo) {
return new algo();
} else {
throw new Error("canonicalization algorithm '" + name + "' is not supported");
}
};

SignedXml.prototype.findHashAlgorithm = function (name) {
var algo = SignedXml.HashAlgorithms[name];
if (algo) return new algo();
else throw new Error("hash algorithm '" + name + "' is not supported");
if (algo) {
return new algo();
} else {
throw new Error("hash algorithm '" + name + "' is not supported");
}
};

SignedXml.prototype.validateReferences = function (doc) {
for (var r in this.references) {
if (!this.references.hasOwnProperty(r)) continue;
if (!this.references.hasOwnProperty(r)) {
continue;
}

var ref = this.references[r];

Expand All @@ -518,7 +548,9 @@ SignedXml.prototype.validateReferences = function (doc) {
var elemXpath;
var num_elements_for_id = 0;
for (var index in this.idAttributes) {
if (!this.idAttributes.hasOwnProperty(index)) continue;
if (!this.idAttributes.hasOwnProperty(index)) {
continue;
}
var tmp_elemXpath =
"//*[@*[local-name(.)='" + this.idAttributes[index] + "']='" + uri + "']";
var tmp_elem = xpath.select(tmp_elemXpath, doc);
Expand Down Expand Up @@ -582,8 +614,9 @@ SignedXml.prototype.loadSignature = function (signatureNode) {
".//*[local-name(.)='CanonicalizationMethod']/@Algorithm",
signatureNode
);
if (nodes.length == 0)
if (nodes.length == 0) {
throw new Error("could not find CanonicalizationMethod/@Algorithm element");
}
this.canonicalizationAlgorithm = nodes[0].value;

this.signatureAlgorithm = utils.findFirst(
Expand All @@ -596,10 +629,14 @@ SignedXml.prototype.loadSignature = function (signatureNode) {
".//*[local-name(.)='SignedInfo']/*[local-name(.)='Reference']",
signatureNode
);
if (references.length == 0) throw new Error("could not find any Reference elements");
if (references.length == 0) {
throw new Error("could not find any Reference elements");
}

for (var i in references) {
if (!references.hasOwnProperty(i)) continue;
if (!references.hasOwnProperty(i)) {
continue;
}

this.loadReference(references[i]);
}
Expand All @@ -617,18 +654,21 @@ SignedXml.prototype.loadSignature = function (signatureNode) {
*/
SignedXml.prototype.loadReference = function (ref) {
var nodes = utils.findChilds(ref, "DigestMethod");
if (nodes.length == 0)
if (nodes.length == 0) {
throw new Error("could not find DigestMethod in reference " + ref.toString());
}
var digestAlgoNode = nodes[0];

var attr = utils.findAttr(digestAlgoNode, "Algorithm");
if (!attr)
if (!attr) {
throw new Error("could not find Algorithm attribute in node " + digestAlgoNode.toString());
}
var digestAlgo = attr.value;

nodes = utils.findChilds(ref, "DigestValue");
if (nodes.length == 0)
if (nodes.length == 0) {
throw new Error("could not find DigestValue node in reference " + ref.toString());
}
if (nodes[0].childNodes.length == 0 || !nodes[0].firstChild.data) {
throw new Error("could not find the value of DigestValue in " + nodes[0].toString());
}
Expand All @@ -641,7 +681,9 @@ SignedXml.prototype.loadReference = function (ref) {
var transformsNode = nodes[0];
var transformsAll = utils.findChilds(transformsNode, "Transform");
for (var t in transformsAll) {
if (!transformsAll.hasOwnProperty(t)) continue;
if (!transformsAll.hasOwnProperty(t)) {
continue;
}

var trans = transformsAll[t];
transforms.push(utils.findAttr(trans, "Algorithm").value);
Expand Down Expand Up @@ -912,7 +954,9 @@ SignedXml.prototype.createReferences = function (doc, prefix) {
prefix = prefix ? prefix + ":" : prefix;

for (var n in this.references) {
if (!this.references.hasOwnProperty(n)) continue;
if (!this.references.hasOwnProperty(n)) {
continue;
}

var ref = this.references[n];
var nodes = xpath.selectWithResolver(ref.xpath, doc, this.namespaceResolver);
Expand All @@ -924,7 +968,9 @@ SignedXml.prototype.createReferences = function (doc, prefix) {
}

for (var h in nodes) {
if (!nodes.hasOwnProperty(h)) continue;
if (!nodes.hasOwnProperty(h)) {
continue;
}

var node = nodes[h];
if (ref.isEmptyUri) {
Expand All @@ -936,7 +982,9 @@ SignedXml.prototype.createReferences = function (doc, prefix) {
}
res += "<" + prefix + "Transforms>";
for (var t in ref.transforms) {
if (!ref.transforms.hasOwnProperty(t)) continue;
if (!ref.transforms.hasOwnProperty(t)) {
continue;
}

var trans = ref.transforms[t];
var transform = this.findCanonicalizationAlgorithm(trans);
Expand Down Expand Up @@ -991,7 +1039,9 @@ SignedXml.prototype.getCanonXml = function (transforms, node, options) {
var canonXml = node.cloneNode(true); // Deep clone

for (var t in transforms) {
if (!transforms.hasOwnProperty(t)) continue;
if (!transforms.hasOwnProperty(t)) {
continue;
}

var transform = this.findCanonicalizationAlgorithm(transforms[t]);
canonXml = transform.process(canonXml, options);
Expand Down Expand Up @@ -1021,14 +1071,20 @@ SignedXml.prototype.ensureHasId = function (node) {
);
} else {
for (var index in this.idAttributes) {
if (!this.idAttributes.hasOwnProperty(index)) continue;
if (!this.idAttributes.hasOwnProperty(index)) {
continue;
}

attr = utils.findAttr(node, this.idAttributes[index], null);
if (attr) break;
if (attr) {
break;
}
}
}

if (attr) return attr.value;
if (attr) {
return attr.value;
}

//add the attribute
var id = "_" + this.id++;
Expand Down
Loading

0 comments on commit b425626

Please sign in to comment.