Skip to content

Commit 378c815

Browse files
authored
Merge pull request #43 from timlegge/metadata
Update for Metadata custom urls and signing
2 parents 9824127 + bfe0966 commit 378c815

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

lib/Net/SAML2/SP.pm

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ has 'cert' => (isa => 'Str', is => 'ro', required => 1);
9898
has 'key' => (isa => 'Str', is => 'ro', required => 1);
9999
has 'cacert' => (isa => 'Maybe[Str]', is => 'ro', required => 1);
100100

101+
has 'error_url' => (isa => 'Str', is => 'ro', required => 1);
102+
has 'slo_url_soap' => (isa => 'Str', is => 'ro', required => 1);
103+
has 'slo_url_redirect' => (isa => 'Str', is => 'ro', required => 1);
104+
has 'slo_url_post' => (isa => 'Str', is => 'ro', required => 1);
105+
has 'acs_url_post' => (isa => 'Str', is => 'ro', required => 1);
106+
has 'acs_url_artifact' => (isa => 'Str', is => 'ro', required => 1);
107+
101108
has 'org_name' => (isa => 'Str', is => 'ro', required => 1);
102109
has 'org_display_name' => (isa => 'Str', is => 'ro', required => 1);
103110
has 'org_contact' => (isa => 'Str', is => 'ro', required => 1);
@@ -304,20 +311,23 @@ Returns the metadata XML document for this SP.
304311
sub metadata {
305312
my ($self) = @_;
306313

314+
use Net::SAML2::Util qw/generate_id/;
315+
307316
my $x = XML::Generator->new(':pretty', conformance => 'loose');
308317
my $md = ['md' => 'urn:oasis:names:tc:SAML:2.0:metadata'];
309318
my $ds = ['ds' => 'http://www.w3.org/2000/09/xmldsig#'];
310319

311-
$x->EntityDescriptor(
320+
my $metadata = $x->EntityDescriptor(
312321
$md,
313322
{
314323
entityID => $self->id },
315324
$x->SPSSODescriptor(
316325
$md,
317326
{ AuthnRequestsSigned => defined($self->authnreq_signed) ? $self->authnreq_signed : '1',
318327
WantAssertionsSigned => defined($self->want_assertions_signed) ? $self->want_assertions_signed : '1',
319-
errorURL => $self->url . '/saml/error',
320-
protocolSupportEnumeration => 'urn:oasis:names:tc:SAML:2.0:protocol' },
328+
errorURL => $self->url . $self->error_url,
329+
protocolSupportEnumeration => 'urn:oasis:names:tc:SAML:2.0:protocol',
330+
ID => generate_id()},
321331
$x->KeyDescriptor(
322332
$md,
323333
{
@@ -336,29 +346,29 @@ sub metadata {
336346
$x->SingleLogoutService(
337347
$md,
338348
{ Binding => 'urn:oasis:names:tc:SAML:2.0:bindings:SOAP',
339-
Location => $self->url . '/saml/slo-soap' },
349+
Location => $self->url . $self->slo_url_soap },
340350
),
341351
$x->SingleLogoutService(
342352
$md,
343353
{ Binding => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
344-
Location => $self->url . '/saml/sls-redirect-response' },
354+
Location => $self->url . $self->slo_url_redirect },
345355
),
346356
$x->SingleLogoutService(
347357
$md,
348358
{ Binding => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
349-
Location => $self->url . '/saml/sls-post-response' },
359+
Location => $self->url . $self->slo_url_post },
350360
),
351361
$x->AssertionConsumerService(
352362
$md,
353363
{ Binding => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
354-
Location => $self->url . '/saml/consumer-post',
364+
Location => $self->url . $self->acs_url_post,
355365
index => '1',
356366
isDefault => 'true' },
357367
),
358368
$x->AssertionConsumerService(
359369
$md,
360370
{ Binding => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact',
361-
Location => $self->url . '/saml/consumer-artifact',
371+
Location => $self->url . $self->acs_url_artifact,
362372
index => '2',
363373
isDefault => 'false' },
364374
),
@@ -398,6 +408,21 @@ sub metadata {
398408
),
399409
)
400410
);
411+
412+
use Net::SAML2::XML::Sig;
413+
414+
my $signer = Net::SAML2::XML::Sig->new({
415+
key => $self->key,
416+
cert => $self->cert,
417+
sig_hash => 'sha256',
418+
digest_hash => 'sha256',
419+
x509 => 1,
420+
});
421+
422+
# create a signature
423+
my $signed = $signer->sign($metadata);
424+
425+
return $signed;
401426
}
402427

403428
__PACKAGE__->meta->make_immutable;

lib/Net/SAML2/XML/Sig.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ sub _signedinfo_xml {
15901590

15911591
#return qq{<dsig:SignedInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
15921592
return qq{<dsig:SignedInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
1593-
<dsig:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" />
1593+
<dsig:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
15941594
<dsig:SignatureMethod Algorithm="$algorithm" />
15951595
$digest_xml
15961596
</dsig:SignedInfo>};

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ sub net_saml2_sp {
4242
org_display_name => 'Test',
4343
org_contact => 'test@example.com',
4444
org_url => 'http://www.example.com',
45+
slo_url_soap => '/slo-soap',
46+
slo_url_redirect => '/sls-redirect-response',
47+
slo_url_post => '/sls-post-response',
48+
acs_url_post => '/consumer-post',
49+
acs_url_artifact => '/consumer-artifact',
50+
org_name => 'Net::SAML2 Saml2Test',
51+
org_display_name => 'Saml2Test app for Net::SAML2',
52+
org_contact => 'saml2test@example.com',
53+
error_url => '/error',
4554
authnreq_signed => '0',
4655
want_assertions_signed => '0',
4756
@_,

xt/testapp/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ url: "https://netsaml2-testapp.local"
88
cert: "sign-certonly.pem"
99
key: "sign-nopw-cert.pem"
1010
cacert: "saml_cacert.pem"
11+
slo_url_soap: "/slo-soap"
12+
slo_url_redirect: "/sls-redirect-response"
13+
slo_url_post: "/sls-post-response"
14+
acs_url_post: "/consumer-post"
15+
acs_url_artifact: "/consumer-artifact"
16+
org_name: "Net::SAML2 Saml2Test"
17+
org_display_name: "Saml2Test app for Net::SAML2"
18+
org_contact: "saml2test@example.com"
19+
error_url: "/error"

xt/testapp/lib/Saml2Test.pm

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,16 @@ sub _sp {
201201
cert => config->{cert},
202202
key => config->{key},
203203
cacert => config->{cacert},
204+
slo_url_soap => config->{slo_url_soap},
205+
slo_url_redirect => config->{slo_url_redirect},
206+
slo_url_post => config->{slo_url_post},
207+
acs_url_post => config->{acs_url_post},
208+
acs_url_artifact => config->{acs_url_artifact},
209+
error_url => config->{error_url},
204210

205-
org_name => 'Net::SAML2 Saml2Test',
206-
org_display_name => 'Saml2Test app for Net::SAML2',
207-
org_contact => 'saml2test@example.com',
211+
org_name => config->{org_name},
212+
org_display_name => config->{org_display_name},
213+
org_contact => config->{org_contact},
208214
);
209215
return $sp;
210216
}

0 commit comments

Comments
 (0)