From 46e5447ad3bb7becb9efbd1f1272edf57592fa6c Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 24 May 2023 09:39:15 +0200 Subject: [PATCH 1/2] Add support for appending attributes to KeyInfo element --- index.d.ts | 1 + lib/signed-xml.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 2166a10a..29850ee8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -79,6 +79,7 @@ export class SignedXml { export interface KeyInfo { getKey(keyInfo?: Node[] | null): Buffer; getKeyInfo(key?: string, prefix?: string): string; + attrs?: {[key: string]: any} | undefined; } export class FileKeyInfo implements KeyInfo { diff --git a/lib/signed-xml.js b/lib/signed-xml.js index f582bb19..d1c13ff5 100644 --- a/lib/signed-xml.js +++ b/lib/signed-xml.js @@ -845,7 +845,13 @@ SignedXml.prototype.getKeyInfo = function(prefix) { currentPrefix = currentPrefix ? currentPrefix + ':' : currentPrefix if (this.keyInfoProvider) { - res += "<" + currentPrefix + "KeyInfo>" + var keyInfoAttrs = "" + if (this.keyInfoProvider.attrs) { + Object.keys(this.keyInfoProvider.attrs).forEach((name) => { + keyInfoAttrs += " " + name + "=\"" + this.keyInfoProvider.attrs[name] + "\"" + }) + } + res += "<" + currentPrefix + "KeyInfo" + keyInfoAttrs + ">" res += this.keyInfoProvider.getKeyInfo(this.signingCert || this.signingKey, prefix) res += "" } From 75e61ee3b14ebd6640631ff1d754489b89464bfb Mon Sep 17 00:00:00 2001 From: Ivan Novak Date: Sun, 28 May 2023 09:23:37 +0200 Subject: [PATCH 2/2] add test for attribute generation in KeyInfo element when attrs is set on keyInfoProvider --- test/signature-unit-tests.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/signature-unit-tests.js b/test/signature-unit-tests.js index 4467f9e0..eff239e8 100644 --- a/test/signature-unit-tests.js +++ b/test/signature-unit-tests.js @@ -734,6 +734,36 @@ module.exports = { test.done(); }, + "adds attributes to KeyInfo element when attrs are present in keyInfoProvider": function (test) { + var xml = ""; + var sig = new SignedXml(); + sig.signingKey = fs.readFileSync("./test/static/client.pem"); + sig.keyInfoProvider = { + attrs: { + CustomUri: "http://www.example.com/keyinfo", + CustomAttribute: "custom-value" + }, + getKeyInfo: function () { + return ""; + } + }; + + sig.computeSignature(xml); + var signedXml = sig.getSignedXml(); + + var doc = new dom().parseFromString(signedXml); + var keyInfoElement = select("//*[local-name(.)='KeyInfo']", doc.documentElement); + test.equal(keyInfoElement.length, 1, "KeyInfo element should exist"); + + var algorithmAttribute = keyInfoElement[0].getAttribute('CustomUri'); + test.equal(algorithmAttribute, 'http://www.example.com/keyinfo', "KeyInfo element should have the correct CustomUri attribute value"); + + var customAttribute = keyInfoElement[0].getAttribute('CustomAttribute'); + test.equal(customAttribute, 'custom-value', "KeyInfo element should have the correct CustomAttribute attribute value"); + + test.done(); + }, + } function passValidSignature(test, file, mode) {