Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ To sign xml documents:
- `existingPrefixes` - A hash of prefixes and namespaces `prefix: namespace` that shouldn't be in the signature because they already exist in the xml
- `getSignedXml()` - returns the original xml document with the signature in it, **must be called only after `computeSignature`**
- `getSignatureXml()` - returns just the signature part, **must be called only after `computeSignature`**
- `getOriginalXmlWithIds()` - returns the original xml with Id attributes added on relevant elements (required for validation), **must be called only after `computeSignature`**
- `getOriginalXmlWithIds()` - **[deprecated]** returns the original xml with Id attributes added on relevant elements, **must be called only after `computeSignature`**. This method is deprecated and will be removed in a future version. Use `ComputeSignatureOptionsLocation` to control where the signature will be placed in the original XML.

To verify xml documents:

Expand Down
7 changes: 4 additions & 3 deletions src/signed-xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,14 @@ export class SignedXml {
}

/**
* Returns the original xml with Id attributes added on relevant elements (required for validation), must be called only after {@link computeSignature}
* Returns the original xml with Id attributes added on relevant elements, must be called only after {@link computeSignature}
*
* @returns The original XML with IDs.
* @deprecated This function is deprecated and will be removed in a future version. Use ComputeSignatureOptionsLocation to control where the signature will be placed in the original XML.
*/
getOriginalXmlWithIds(): string {
getOriginalXmlWithIds = deprecate((): string => {
return this.originalXmlWithIds;
}
}, "`getOriginalXmlWithIds()` is deprecated and will be removed in a future version. Use ComputeSignatureOptionsLocation to control where the signature will be placed in the original XML.");

/**
* Returns the original xml document with the signature in it, must be called only after {@link computeSignature}
Expand Down
37 changes: 26 additions & 11 deletions test/signature-unit-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe("Signature unit tests", function () {
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getOriginalXmlWithIds();
const signedXml = sig.getSignedXml();
const doc = new xmldom.DOMParser().parseFromString(signedXml);

const op = nsMode === "equal" ? "=" : "!=";
Expand Down Expand Up @@ -172,9 +172,10 @@ describe("Signature unit tests", function () {
sig.canonicalizationAlgorithm = "http://www.w3.org/2001/10/xml-exc-c14n#";
sig.signatureAlgorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
sig.computeSignature(xml);
const signedXml = sig.getOriginalXmlWithIds();
const signedXml = sig.getSignedXml();
const doc = new xmldom.DOMParser().parseFromString(signedXml);
const attrs = xpath.select("//@*", doc);
// Only count attributes on the 'x' element, not the entire document (which includes signature attributes)
const attrs = xpath.select("//*[local-name(.)='x']/@*", doc);
isDomNode.assertIsArrayOfNodes(attrs);
expect(attrs.length, "wrong number of attributes").to.equal(2);
}
Expand Down Expand Up @@ -535,10 +536,17 @@ describe("Signature unit tests", function () {

expect(expectedSignedXml, "wrong signedXml format").to.equal(signedXml);

const originalXmlWithIds = sig.getOriginalXmlWithIds();
const expectedOriginalXmlWithIds =
'<root><x xmlns="ns" Id="_0"/><y attr="value" Id="_1"/><z><w Id="_2"/></z></root>';
expect(expectedOriginalXmlWithIds, "wrong OriginalXmlWithIds").to.equal(originalXmlWithIds);
// Verify IDs were added to the signed XML document
const signedDoc = new xmldom.DOMParser().parseFromString(signedXml);
const xId = xpath.select1("//*[local-name(.)='x']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(xId);
expect(xId.value).to.equal("_0");
const yId = xpath.select1("//*[local-name(.)='y']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(yId);
expect(yId.value).to.equal("_1");
const wId = xpath.select1("//*[local-name(.)='w']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(wId);
expect(wId.value).to.equal("_2");
});

it("signer creates signature with correct structure (with prefix)", function () {
Expand Down Expand Up @@ -699,10 +707,17 @@ describe("Signature unit tests", function () {

expect(signedXml, "wrong signedXml format").to.equal(expectedSignedXml);

const originalXmlWithIds = sig.getOriginalXmlWithIds();
const expectedOriginalXmlWithIds =
'<root><x xmlns="ns" Id="_0"/><y attr="value" Id="_1"/><z><w Id="_2"/></z></root>';
expect(originalXmlWithIds, "wrong OriginalXmlWithIds").to.equal(expectedOriginalXmlWithIds);
// Verify IDs were added to the signed XML document
const signedDoc = new xmldom.DOMParser().parseFromString(signedXml);
const xId = xpath.select1("//*[local-name(.)='x']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(xId);
expect(xId.value).to.equal("_0");
const yId = xpath.select1("//*[local-name(.)='y']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(yId);
expect(yId.value).to.equal("_1");
const wId = xpath.select1("//*[local-name(.)='w']/@*[local-name(.)='Id']", signedDoc);
isDomNode.assertIsAttributeNode(wId);
expect(wId.value).to.equal("_2");
});

it("signer creates correct signature values", function () {
Expand Down