SAML2/IdP.pm

filter: Statementstmt 98.4% 128 / 130 Branchbran 82.1% 23 / 28 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 21 / 21 Podpod 100% 8 / 8 totaltotal 93.6% 190 / 203 CC 26 SCAR 16.5
CC
26
Cov
94%
new_from_xml
12.0
__ANON__
6.1
format
4.0
CRAP 5.2
« SAML2/Role/VerifyXML.pm ↑ Summary SAML2/SP.pm »
1 package Net::SAML2::IdP;
2 32 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 with 'Net::SAML2::Role::XMLCertificate';
4  
5 32 our $VERSION = '0.89'; # VERSION
6  
7 # ABSTRACT: SAML Identity Provider object
8  
9  
10 32 ▶use Crypt::OpenSSL::Verify;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use Crypt::OpenSSL::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use MooseX::Types::URI qw/ Uri /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 32 ▶use Net::SAML2::XML::Util qw/ no_comments /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 32 has 'entityid' => (isa => 'Str', is => 'ro', required => 1);
22 32 has 'cacert' => (isa => 'Maybe[Str]', is => 'ro', required => 1);
23 32 has 'sso_urls' => (isa => 'HashRef[Str]', is => 'ro', required => 1);
24 32 has 'slo_urls' => (isa => 'Maybe[HashRef[Str]]', is => 'ro');
25 32 has 'art_urls' => (isa => 'Maybe[HashRef[Str]]', is => 'ro');
26 32 has 'certs' => (isa => 'HashRef[ArrayRef[Str]]', is => 'ro', required => 1);
27  
28 has 'formats' => (
29 isa => 'HashRef[Str]',
30 is => 'ro',
31 required => 0,
32 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
33 32 );
34 32 has 'default_format' => (isa => 'Str', is => 'ro', required => 0);
35 32 has 'debug' => (isa => 'Bool', is => 'ro', required => 0, default => 0);
36  
37  
38 5 ▶sub new_from_url {
Subroutine new_from_url: called CC=4 SCAR=13.9
Pod documented
39 5 my ($class, %args) = @_;
40  
41 5 my $req = GET $args{url};
42 5 my $ua = $args{ua};
43 5 ▶ if (!$ua) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ua) 4 1 5
44 4 $ua = LWP::UserAgent->new;
45 4 ▶ if (defined $args{ssl_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ssl_opts"}) 1 3 4
46 1 require LWP::Protocol::https;
47 1 $ua->ssl_opts(%{ $args{ssl_opts} });
48 }
49 }
50  
51 5 my $res = $ua->request($req);
52 5 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 1 4 5
53 1 die(
54 sprintf(
55 "Error retrieving metadata: %s (%s)\n",
56 $res->message, $res->code
57 )
58 );
59 }
60  
61 4 my $xml = $res->decoded_content;
62  
63 return $class->new_from_xml(
64 xml => $xml,
65 cacert => $args{cacert},
66 4 );
67 }
68  
69  
70 19 ▶sub new_from_xml {
Subroutine new_from_xml: called CC=12 SCAR=24.9
Pod documented
71 19 my($class, %args) = @_;
72  
73 19 my $dom = no_comments($args{xml});
74  
75 19 my $xpath = XML::LibXML::XPathContext->new($dom);
76 19 $xpath->registerNs('md', 'urn:oasis:names:tc:SAML:2.0:metadata');
77 19 $xpath->registerNs('ds', 'http://www.w3.org/2000/09/xmldsig#');
78  
79 19 my $data;
80  
81 19 my $basepath = '//md:EntityDescriptor/md:IDPSSODescriptor';
82  
83 19 for my $sso ($xpath->findnodes("$basepath/md:SingleSignOnService")) {
84 55 my $binding = $sso->getAttribute('Binding');
85 55 $data->{SSO}->{$binding} = $sso->getAttribute('Location');
86 }
87  
88 19 for my $slo ($xpath->findnodes("$basepath/md:SingleLogoutService")) {
89 46 my $binding = $slo->getAttribute('Binding');
90 46 $data->{SLO}->{$binding} = $slo->getAttribute('Location');
91 }
92  
93 19 for my $art ($xpath->findnodes("$basepath/md:ArtifactResolutionService")) {
94 15 my $binding = $art->getAttribute('Binding');
95 15 $data->{Art}->{$binding} = $art->getAttribute('Location');
96 }
97  
98 19 for my $format ($xpath->findnodes("$basepath/md:NameIDFormat")) {
99 95 $format = $format->string_value;
100 95 $format =~ s/^\s+//g;
101 95 $format =~ s/\s+$//g;
102  
103 95 my($short_format)
104 = $format =~ /urn:oasis:names:tc:SAML:(?:2.0|1.1):nameid-format:(.*)$/;
105  
106 95 ▶ if(defined $short_format) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $short_format) 95 0 95
107 95 $data->{NameIDFormat}{$short_format} = $format;
108 95 ▶ $data->{DefaultFormat} = $short_format unless exists $data->{DefaultFormat};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"DefaultFormat"} 17 78 95
109 }
110 }
111  
112 19 my %certs = ();
113 19 for my $key ($xpath->findnodes("$basepath/md:KeyDescriptor")) {
114 24 my $use = $key->getAttribute('use');
115 24 my $pem = $class->get_pem_from_keynode($key);
116 24 ▶ if (!$use) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $use) { } 1 23 24
117 1 push(@{$certs{signing}}, $pem);
118 1 push(@{$certs{encryption}}, $pem);
119 }
120 else {
121 23 push(@{$certs{$use}}, $pem);
122 }
123 }
124  
125 return $class->new(
126 entityid => $xpath->findvalue('//md:EntityDescriptor/@entityID'),
127 sso_urls => $data->{SSO},
128 slo_urls => $data->{SLO} || {},
129 art_urls => $data->{Art} || {},
130 certs => \%certs,
131 cacert => $args{cacert},
132 debug => $args{debug},
133 $data->{DefaultFormat}
134 ? (
135 default_format => $data->{DefaultFormat},
136 formats => $data->{NameIDFormat},
137 )
138 19 ▶ : (),
Branch: 1 branch
BranchTrueFalseTotal
$data->{'DefaultFormat'} ? : 17 2 19
Condition100%4/4
$data->{'SLO'} || {}
l!l
163
$data->{'Art'} || {}
l!l
154
MC/DC100%1/1
$data->{'SLO'} || {}
$data->{'SLO'}
MC/DC100%1/1
$data->{'Art'} || {}
$data->{'Art'}
Truth table100%2/2
A
A= $data->{'SLO'}
Aresult
11
00
Truth table100%2/2
A
A= $data->{'Art'}
Aresult
11
00
139 );
140  
141 }
142  
143 # BUILDARGS ( hashref of the parameters passed to the constructor )
144 #
145 # Called after the object is created to validate the IdP using the cacert
146 #
147  
148 19 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
149 19 my $orig = shift;
150 19 my $self = shift;
151  
152 19 my %params = @_;
153  
154 19 ▶ if ($params{cacert}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"cacert"}) 17 2 19
155 17 my $ca = Crypt::OpenSSL::Verify->new($params{cacert}, { strict_certs => 0, });
156  
157 17 my %certificates;
158 my @errors;
159 17 for my $use (keys %{$params{certs}}) {
160 18 my $certs = $params{certs}{$use};
161 18 for my $pem (@{$certs}) {
162 20 my $cert = Crypt::OpenSSL::X509->new_from_string($pem);
163 20 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
164 20 $ca->verify($cert);
165 17 push(@{$certificates{$use}}, $pem);
166 }
167 20 ▶ catch { push (@errors, $_); };
Subroutine __ANON__: called CC=1 SCAR=0.0
168 }
169 }
170  
171 17 ▶ if ( $params{debug} && @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"debug"} and @errors) 0 17 17
Condition33%1/3
$params{"debug"} and @errors
!ll&&!rl&&r
1700
MC/DC0%0/2
$params{"debug"} and @errors
$params{"debug"} @errors
Truth table33%1/3
A and B
A= $params{"debug"}B= @errors
ABresult
0X0
100
111
172 0 warn "Can't verify IdP cert(s): " . join(", ", @errors);
173 }
174  
175 17 $params{certs} = \%certificates;
176 }
177  
178 19 return $self->$orig(%params);
179 32 };
180  
181  
182 19 ▶sub sso_url {
Subroutine sso_url: called CC=1 SCAR=0.0
Pod documented
183 19 my($self, $binding) = @_;
184 19 return $self->sso_urls->{$binding};
185 }
186  
187  
188 8 ▶sub slo_url {
Subroutine slo_url: called CC=2 SCAR=7.2
Pod documented
189 8 my ($self, $binding) = @_;
190 8 ▶ return $self->slo_urls ? $self->slo_urls->{$binding} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->slo_urls ? : 8 0 8
191 }
192  
193  
194 6 ▶sub art_url {
Subroutine art_url: called CC=2 SCAR=7.2
Pod documented
195 6 my ($self, $binding) = @_;
196 6 ▶ return $self->art_urls ? $self->art_urls->{$binding} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->art_urls ? : 6 0 6
197 }
198  
199  
200 16 ▶sub cert {
Subroutine cert: called CC=1 SCAR=0.0
Pod documented
201 16 my($self, $use) = @_;
202 16 return $self->certs->{$use};
203 }
204  
205  
206 20 ▶sub binding {
Subroutine binding: called CC=2 SCAR=7.4
Pod documented
207 20 my($self, $name) = @_;
208  
209 20 my $bindings = {
210 post => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST',
211 redirect => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
212 soap => 'urn:oasis:names:tc:SAML:2.0:bindings:SOAP',
213 };
214  
215 20 ▶ if(exists $bindings->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bindings->{$name}) 20 0 20
216 20 return $bindings->{$name};
217 }
218  
219 0 return;
220 }
221  
222  
223 12 ▶sub format {
Subroutine format: called CC=4 SCAR=13.9
Pod documented
224 12 my($self, $short_name) = @_;
225  
226 12 ▶ if(defined $short_name && exists $self->formats->{$short_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $short_name and exists $self->formats->{$short_name}) { } 6 6 12
elsif ($self->default_format) { } 5 1 6
Condition66%2/3
defined $short_name and exists $self->formats->{$short_name}
!ll&&!rl&&r
606
MC/DC50%1/2
defined $short_name and exists $self->formats->{$short_name}
defined $short_name exists $self->formats->{$short_name}
Truth table66%2/3
A and B
A= defined $short_nameB= exists $self->formats->{$short_name}
ABresult
0X0
100
111
227 6 return $self->formats->{$short_name};
228 }
229 elsif($self->default_format) {
230 5 return $self->formats->{$self->default_format};
231 }
232  
233 1 return;
234 }
235  
236 32 __PACKAGE__->meta->make_immutable;
237  
238 __END__
« SAML2/Role/VerifyXML.pm ↑ Summary SAML2/SP.pm »