Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for appending attributes to KeyInfo element #285

Merged
merged 3 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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