From 68ebcb2953863c1324ba27f1efdc82290bbdb52e Mon Sep 17 00:00:00 2001 From: davidadzgi Date: Tue, 15 Apr 2025 16:44:02 +0200 Subject: [PATCH] Added support for custom signature childs --- lib/src/signed_xml.dart | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/src/signed_xml.dart b/lib/src/signed_xml.dart index a1d4d34..85ea89c 100644 --- a/lib/src/signed_xml.dart +++ b/lib/src/signed_xml.dart @@ -144,6 +144,7 @@ class SignedXml { String? keyInfo; final idAttributes = ['Id', 'ID', 'id']; late List implicitTransforms; + final customSignatureChilds = []; /// Xml signature implementation /// @@ -596,6 +597,10 @@ class SignedXml { isEmptyUri)); } + void addCustomSignatureChild(String node) { + customSignatureChilds.add(node); + } + /// Compute the signature of the given xml (using the already defined settings) /// /// Options: @@ -670,6 +675,7 @@ class SignedXml { StringBuffer('<${currentPrefix}Signature ${signatureAttrs.join(' ')}>') ..write(_createSignedInfo(doc, prefix)) ..write(_getKeyInfo(prefix)) + ..write(customSignatureChilds.join('')) ..write(''); _originalXmlWithIds = doc.toString(); @@ -801,8 +807,18 @@ class SignedXml { for (final ref in references) { final nodes = XmlXPath.node(doc).query(ref.xpath ?? ''); if (nodes.nodes.isEmpty) { - throw ArgumentError( - 'the following xpath cannot be signed because it was not found: ${ref.xpath}'); + //Search if the xpath is in customSignatureChilds + for (final customSignatureChild in customSignatureChilds) { + final customSignatureChildXml = parseFromString(customSignatureChild); + final customSignatureChildQuery = XmlXPath.node(customSignatureChildXml).query(ref.xpath ?? ''); + if (customSignatureChildQuery.nodes.isNotEmpty) { + nodes.nodes.add(customSignatureChildQuery.node!); + } + } + + if (nodes.nodes.isEmpty) { + throw ArgumentError('the following xpath cannot be signed because it was not found: ${ref.xpath}'); + } } for (final node in nodes.nodes) {