Skip to content

Commit 9b37104

Browse files
authored
Merge pull request #177 from waterkip/GL-add-keyname_to_SP
Add key_name function to SP
2 parents 700e6ce + 466e870 commit 9b37104

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

lib/Net/SAML2/SP.pm

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Consumer Services.
157157

158158
has 'url' => (isa => Uri, is => 'ro', required => 1, coerce => 1);
159159
has 'id' => (isa => 'Str', is => 'ro', required => 1);
160-
has 'cert' => (isa => 'Str', is => 'ro', required => 1);
160+
has 'cert' => (isa => 'Str', is => 'ro', required => 1, predicate => 'has_cert');
161161
has 'key' => (isa => 'Str', is => 'ro', required => 1);
162162
has 'cacert' => (isa => 'Str', is => 'rw', required => 0, predicate => 'has_cacert');
163163

@@ -274,6 +274,7 @@ around BUILDARGS => sub {
274274
sub _build_encryption_key_text {
275275
my ($self) = @_;
276276

277+
return '' unless $self->has_encryption_key;
277278
my $cert = Crypt::OpenSSL::X509->new_from_file($self->encryption_key);
278279
my $text = $cert->as_string;
279280
$text =~ s/-----[^-]*-----//gm;
@@ -283,6 +284,7 @@ sub _build_encryption_key_text {
283284
sub _build_cert_text {
284285
my ($self) = @_;
285286

287+
return '' unless $self->has_cert;
286288
my $cert = Crypt::OpenSSL::X509->new_from_file($self->cert);
287289
my $text = $cert->as_string;
288290
$text =~ s/-----[^-]*-----//gm;
@@ -634,11 +636,25 @@ sub _generate_key_descriptors {
634636
$x->KeyInfo(
635637
$ds,
636638
$x->X509Data($ds, $x->X509Certificate($ds, $key)),
637-
$x->KeyName($ds, Digest::MD5::md5_hex($key)),
639+
$x->KeyName($ds, $self->key_name($use)),
638640
),
639641
);
640642
}
641643

644+
=head2 key_name($type)
645+
646+
Get the key name for either the C<signing> or C<encryption> key
647+
648+
=cut
649+
650+
sub key_name {
651+
my $self = shift;
652+
my $use = shift;
653+
my $key = $use eq 'signing' ? $self->_cert_text : $self->_encryption_key_text;
654+
return unless $key;
655+
return Digest::MD5::md5_hex($key);
656+
}
657+
642658
sub _generate_single_logout_service {
643659
my $self = shift;
644660
my $x = shift;

t/02-create-sp.t

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ use URN::OASIS::SAML2 qw(:bindings :urn);
7171

7272

7373
get_single_node_ok($xpath, '//ds:Signature');
74+
75+
is(
76+
'e73560b0e23602121aedc55bcb1ca637',
77+
$sp->key_name('signing'),
78+
"Got a key name for the signing key"
79+
);
80+
is(
81+
undef,
82+
$sp->key_name('encryption'),
83+
"... and there is no encryption key name"
84+
);
85+
7486
}
7587

7688
{
@@ -228,6 +240,17 @@ use URN::OASIS::SAML2 qw(:bindings :urn);
228240
is($kd->getAttribute('use'),
229241
"encryption", "Key descriptor is there for encryption");
230242

243+
is(
244+
'e73560b0e23602121aedc55bcb1ca637',
245+
$sp->key_name('signing'),
246+
"Got a key name for the signing key"
247+
);
248+
is(
249+
'e73560b0e23602121aedc55bcb1ca637',
250+
$sp->key_name('encryption'),
251+
"... and we also have a encryption key name"
252+
);
253+
231254
}
232255

233256
{
@@ -335,7 +358,6 @@ use URN::OASIS::SAML2 qw(:bindings :urn);
335358
$nodes = $xpath->findnodes('//md:SingleLogoutService');
336359
is($nodes->size, 0, "No single logout service generated without arguments");
337360

338-
339361
}
340362

341363
throws_ok(

0 commit comments

Comments
 (0)