Skip to content

Commit c6b9dfb

Browse files
committed
Fix SAML metadata signing
This requires an update on XML::Sig (see perl-net-saml2/perl-XML-Sig#40) Fixes: #61 Signed-off-by: Wesley Schwengle <wesley@opndev.io>
1 parent b18d316 commit c6b9dfb

File tree

6 files changed

+117
-38
lines changed

6 files changed

+117
-38
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ xt/testapp/sign-nopw-cert-dsa.pem
2222
xt/testapp/sign-private-dsa.pem
2323
Release-*
2424
xt/testapp/IdPs/*/*
25+
/Net-SAML2-*/**
26+
/Net-SAML2-*.tar.gz

Makefile.PL

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ my %WriteMakefileArgs = (
6868
"URI::URL" => 0,
6969
"XML::LibXML::XPathContext" => 0
7070
},
71-
"VERSION" => "0.55",
71+
"VERSION" => "0.56",
7272
"test" => {
7373
"TESTS" => "t/*.t t/author/*.t"
7474
}

README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ NAME
22
Net::SAML2 - SAML2 bindings and protocol implementation
33

44
VERSION
5-
version 0.55
5+
version 0.56
66

77
SYNOPSIS
88
See TUTORIAL.md for implementation documentation and

lib/Net/SAML2/SP.pm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,15 @@ sub generate_metadata {
327327
return $x->EntityDescriptor(
328328
$md,
329329
{
330-
entityID => $self->id },
330+
entityID => $self->id,
331+
ID => $self->generate_sp_desciptor_id(),
332+
},
331333
$x->SPSSODescriptor(
332334
$md,
333335
{ AuthnRequestsSigned => $self->authnreq_signed,
334336
WantAssertionsSigned => $self->want_assertions_signed,
335337
errorURL => $self->url . $self->error_url,
336338
protocolSupportEnumeration => 'urn:oasis:names:tc:SAML:2.0:protocol',
337-
ID => $self->generate_sp_desciptor_id(),
338339
},
339340
$x->KeyDescriptor(
340341
$md,
@@ -438,6 +439,8 @@ sub metadata {
438439
sig_hash => 'sha256',
439440
digest_hash => 'sha256',
440441
x509 => 1,
442+
ns => { md => 'urn:oasis:names:tc:SAML:2.0:metadata' },
443+
id_attr => '/md:EntityDescriptor[@ID]',
441444
}
442445
);
443446
return $signer->sign($metadata);

t/02-create-sp.t

Lines changed: 99 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ my $xpath = get_xpath(
1111
ds => 'http://www.w3.org/2000/09/xmldsig#'
1212
);
1313

14-
my $nodes = $xpath->findnodes('//md:EntityDescriptor/md:SPSSODescriptor');
15-
is($nodes->size, 1, "We have one PSSODescriptor");
16-
my $node = $nodes->get_node(1);
14+
my $node
15+
= get_single_node_ok($xpath, '//md:EntityDescriptor/md:SPSSODescriptor');
1716
ok(!$node->getAttribute('WantAssertionsSigned'),
1817
'Wants assertions to be signed');
1918
ok(
@@ -37,54 +36,120 @@ if (is(@ssos, 2, "Got two assertionConsumerService(s)")) {
3736
);
3837
}
3938

39+
get_single_node_ok($xpath, '//ds:Signature');
40+
41+
{
42+
my $sp = net_saml2_sp(sign_metadata => 0);
43+
my $xpath = get_xpath(
44+
$sp->metadata,
45+
md => 'urn:oasis:names:tc:SAML:2.0:metadata',
46+
ds => 'http://www.w3.org/2000/09/xmldsig#'
47+
);
48+
49+
my $nodes = $xpath->findnodes('//ds:Signature');
50+
is($nodes->size(), 0, "We don't have any ds:Signature present");
51+
52+
}
53+
4054
{
4155
my $sp = Net::SAML2::SP->new(
42-
id => 'http://localhost:3000',
43-
url => 'http://localhost:3000',
44-
cert => 't/sign-nopw-cert.pem',
45-
key => 't/sign-nopw-cert.pem',
46-
cacert => 't/cacert.pem',
47-
org_name => 'Test',
48-
org_display_name => 'Test',
56+
id => 'Some entity ID',
57+
url => 'http://localhost:3000',
58+
cert => 't/sign-nopw-cert.pem',
59+
key => 't/sign-nopw-cert.pem',
60+
cacert => 't/cacert.pem',
61+
62+
org_name => 'Net::SAML2::SP',
63+
org_display_name => 'Net::SAML2::SP testsuite',
4964
org_contact => 'test@example.com',
65+
5066
org_url => 'http://www.example.com',
5167
slo_url_soap => '/slo-soap',
5268
slo_url_redirect => '/sls-redirect-response',
5369
slo_url_post => '/sls-post-response',
5470
acs_url_post => '/consumer-post',
5571
acs_url_artifact => '/consumer-artifact',
56-
org_name => 'Net::SAML2 Saml2Test',
57-
org_display_name => 'Saml2Test app for Net::SAML2',
58-
org_contact => 'saml2test@example.com',
5972
error_url => '/error',
6073
);
6174

62-
my $xpath = get_xpath($sp->metadata,
63-
md => 'urn:oasis:names:tc:SAML:2.0:metadata');
64-
my $nodes = $xpath->findnodes('//md:EntityDescriptor/md:SPSSODescriptor');
65-
is($nodes->size, 1, "We have one PSSODescriptor");
66-
my $node = $nodes->get_node(1);
67-
ok($node->getAttribute('WantAssertionsSigned'),
68-
'Wants assertions to be signed');
69-
ok(
70-
$node->getAttribute('AuthnRequestsSigned'),
71-
'.. and also authn requests to be signed'
72-
);
73-
}
74-
75-
$nodes = $xpath->findnodes('//ds:Signature');
76-
is($nodes->size(), 1, "We have a signed metadata document ds:Signature present");
77-
78-
{
79-
my $sp = net_saml2_sp(sign_metadata => 0);
80-
my $xpath = get_xpath(
75+
my $xpc = get_xpath(
8176
$sp->metadata,
8277
md => 'urn:oasis:names:tc:SAML:2.0:metadata',
8378
ds => 'http://www.w3.org/2000/09/xmldsig#'
8479
);
8580

86-
my $nodes = $xpath->findnodes('//ds:Signature');
87-
is($nodes->size(), 0, "We don't have any ds:Signature present");
81+
my $node = get_single_node_ok($xpc, '/md:EntityDescriptor');
82+
is(
83+
$node->getAttribute('entityID'),
84+
'Some entity ID',
85+
'.. has the correct entity ID'
86+
);
87+
88+
ok($node->getAttribute('ID'), '.. has an ID');
89+
90+
{
91+
# Test ContactPerson
92+
my $node = get_single_node_ok($xpc, '/node()/md:ContactPerson');
93+
my $p = $node->nodePath();
94+
95+
my $company = get_single_node_ok($xpc, "$p/md:Company");
96+
is(
97+
$company->textContent,
98+
'Net::SAML2::SP testsuite',
99+
"Got the correct company name for the contact person"
100+
);
101+
102+
my $email = get_single_node_ok($xpc, "$p/md:EmailAddress");
103+
is($email->textContent, 'test@example.com',
104+
".. and the correct email");
105+
}
106+
107+
{
108+
# Test Organisation
109+
my $node = get_single_node_ok($xpc, '/node()/md:Organization');
110+
my $p = $node->nodePath();
111+
112+
my $name = get_single_node_ok($xpc, "$p/md:OrganizationName");
113+
is(
114+
$name->textContent,
115+
'Net::SAML2::SP',
116+
"Got the correct company name"
117+
);
118+
119+
my $display_name
120+
= get_single_node_ok($xpc, "$p/md:OrganizationDisplayName");
121+
is(
122+
$display_name->textContent,
123+
'Net::SAML2::SP testsuite',
124+
".. and the correct display name"
125+
);
126+
127+
my $url = get_single_node_ok($xpc, "$p/md:OrganizationURL");
128+
is($url->textContent, 'http://www.example.com',
129+
".. and the correct URI");
130+
}
131+
132+
{
133+
# Test SPSSODescriptor
134+
my $node = get_single_node_ok($xpc, '/node()/md:SPSSODescriptor');
135+
is($node->getAttribute('AuthnRequestsSigned'),
136+
'1', '.. and authn request needs signing');
137+
is($node->getAttribute('WantAssertionsSigned'),
138+
'1', '.. as does assertions');
139+
is(
140+
$node->getAttribute('errorURL'),
141+
'http://localhost:3000/error',
142+
'Got the correct error URI'
143+
);
144+
145+
# TODO: Add more tests for other metadata parts
146+
147+
}
148+
149+
{
150+
# Test Signature
151+
my $node = get_single_node_ok($xpc, '/node()/ds:Signature');
152+
}
88153

89154
}
90155

t/lib/Test/Net/SAML2/Util.pm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ our @EXPORT = qw(
1010
get_xpath
1111
test_xml_attribute_ok
1212
test_xml_value_ok
13+
get_single_node_ok
1314
net_saml2_sp
1415
looks_like_a_cert
1516
net_saml2_binding_redirect_request
@@ -216,6 +217,14 @@ sub get_xpath {
216217
return $xp;
217218
}
218219

220+
sub get_single_node_ok {
221+
my $xpc = shift;
222+
my $xpath = shift;
223+
my $nodes = $xpc->findnodes($xpath);
224+
is($nodes->size, 1, "Got 1 node for $xpath");
225+
return $nodes->get_node(1);
226+
}
227+
219228
sub test_xml_attribute_ok {
220229
my ($xpath, $search, $value) = @_;
221230

0 commit comments

Comments
 (0)