Skip to content

Commit

Permalink
Add support for appending attributes to KeyInfo element (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
shunkica authored May 29, 2023
1 parent c6848e7 commit 8960954
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 {
Expand Down
8 changes: 7 additions & 1 deletion lib/signed-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,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 += "</" + currentPrefix + "KeyInfo>"
}
Expand Down
30 changes: 30 additions & 0 deletions test/signature-unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,36 @@ module.exports = {
test.done();
},

"adds attributes to KeyInfo element when attrs are present in keyInfoProvider": function (test) {
var xml = "<root><x /></root>";
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 "<dummy/>";
}
};

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) {
Expand Down

0 comments on commit 8960954

Please sign in to comment.