Skip to content

Commit 0588768

Browse files
authored
Merge pull request #203 from waterkip/GH-201-xml_signature_for_saml_metadata
Fix signature location bug
2 parents b8a297a + b98a763 commit 0588768

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/Net/SAML2/SP.pm

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,31 @@ sub metadata {
688688
sig_hash => 'sha256',
689689
digest_hash => 'sha256',
690690
x509 => 1,
691-
ns => { md => 'urn:oasis:names:tc:SAML:2.0:metadata' },
691+
ns => { md => URN_METADATA },
692692
id_attr => '/md:EntityDescriptor[@ID]',
693693
}
694694
);
695-
return $signer->sign($metadata);
695+
my $md = $signer->sign($metadata);
696+
697+
my $xp = XML::LibXML::XPathContext->new(
698+
XML::LibXML->load_xml(string =>$md)
699+
);
700+
$xp->registerNs('md', URN_METADATA);
701+
$xp->registerNs('dsig', URN_SIGNATURE);
702+
703+
my $nodes = $xp->findnodes('/md:EntityDescriptor[@ID]');
704+
my $rootnode = $nodes->get_node(1);
705+
706+
my $child = $rootnode->firstChild;
707+
return $md if $child->nodeName() eq 'dsig:Signature';
708+
709+
$nodes = $xp->findnodes('//dsig:Signature');
710+
my $signode = $nodes->get_node(1);
711+
712+
$signode->unbindNode;
713+
$rootnode->insertBefore($signode, $child);
714+
715+
return '<?xml version="1.0" encoding="UTF-8"?>' . $rootnode->toString;
696716
}
697717

698718
=head2 get_default_assertion_service

t/02-create-sp.t

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ use URN::OASIS::SAML2 qw(:bindings :urn);
7070
}
7171

7272

73-
get_single_node_ok($xpath, '//ds:Signature');
73+
my $root_node = get_single_node_ok($xpath, '/md:EntityDescriptor');
74+
my $signature_node = $root_node->firstChild;
75+
is($signature_node->nodeName(),
76+
'dsig:Signature', "First node is the signature");
7477

7578
is(
7679
'e73560b0e23602121aedc55bcb1ca637',

0 commit comments

Comments
 (0)