SAML2/Binding/SOAP.pm

filter: Statementstmt 78.9% 86 / 109 Branchbran 45.4% 10 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 18 / 21 Podpod 100% 5 / 5 totaltotal 74.0% 120 / 162 CC 15 SCAR 11.2
CC
15
Cov
72%
handle_request
7.0
request
6.0
handle_response
4.0
CRAP 3.1
« SAML2/Protocol/Artifact.pm ↑ Summary SAML2/Binding/POST.pm »
1 package Net::SAML2::Binding::SOAP;
2 32 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 our $VERSION = '0.89'; # VERSION
5  
6 32 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use MooseX::Types::URI qw/ Uri /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 32 ▶use Net::SAML2::XML::Sig;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use Net::SAML2::XML::Util qw/ no_comments /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use Net::SAML2::Util qw/ deprecation_warning /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 32 with 'Net::SAML2::Role::VerifyXML';
18  
19 # ABSTRACT: SOAP binding for SAML
20  
21  
22  
23 32 has 'ua' => (
24 isa => 'Object',
25 is => 'ro',
26 lazy => 1,
27 builder => 'build_user_agent',
28 );
29  
30  
31 0 ▶sub build_user_agent {
Subroutine build_user_agent: not called CC=1 SCAR=6.9
Pod documented
32 0 return LWP::UserAgent->new();
33 }
34  
35 32 has 'url' => (isa => Uri, is => 'ro', required => 1, coerce => 1);
36 32 has 'key' => (isa => 'Str', is => 'ro', required => 1);
37 32 has 'cert' => (isa => 'Str', is => 'ro', required => 1);
38 32 has 'idp_cert' => (isa => 'ArrayRef[Str]', is => 'ro', required => 1, predicate => 'has_idp_cert');
39 32 has 'cacert' => (
40 is => 'ro',
41 isa => 'Str',
42 required => 0,
43 predicate => 'has_cacert'
44 );
45 32 has 'anchors' => (
46 is => 'ro',
47 isa => 'HashRef',
48 required => 0,
49 predicate => 'has_anchors'
50 );
51  
52 32 has verify => (
53 is => 'ro',
54 isa => 'HashRef',
55 predicate => 'has_verify',
56 );
57  
58 # BUILDARGS
59  
60 # Earlier versions expected the idp_cert to be a string. However, metadata
61 # can include multiple signing certificates so the $idp->cert is now
62 # expected to be an arrayref to the certificates. To avoid breaking existing
63 # applications this changes the the cert to an arrayref if it is not
64 # already an array ref.
65 #
66 # Please remove the build args logic after 6 months from april 18th 2024
67  
68 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=13.0
69 6 my $orig = shift;
70 6 my $self = shift;
71  
72 6 my %params = @_;
73 6 ▶ if ($params{idp_cert} && ref($params{idp_cert}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"idp_cert"} and ref $params{"idp_cert"} ne "ARRAY") 0 6 6
Condition33%1/3
$params{"idp_cert"} and ref $params{"idp_cert"} ne "ARRAY"
!ll&&!rl&&r
060
MC/DC0%0/2
$params{"idp_cert"} and ref $params{"idp_cert"} ne "ARRAY"
$params{"idp_cert"} ref $params{"idp_cert"} ne "ARRAY"
Truth table33%1/3
A and B
A= $params{"idp_cert"}B= ref $params{"idp_cert"} ne "ARRAY"
ABresult
0X0
100
111
74 0 $params{idp_cert} = [$params{idp_cert}];
75 0 deprecation_warning("Please use an array ref for idp_cert");
76 }
77  
78 6 return $self->$orig(%params);
79 32 };
80  
81  
82 0 ▶sub request {
Subroutine request: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $message) = @_;
84 0 my $request = $self->create_soap_envelope($message);
85  
86 0 my $soap_action = 'http://www.oasis-open.org/committees/security';
87  
88 0 my $req = POST $self->url, Content => $request;
89 # SOAP actions should be wrapped in double quotes:
90 # https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383528
91 0 $req->header('SOAPAction' => sprintf('"%s"', $soap_action));
92 0 $req->header('Content-Type' => 'text/xml');
93 0 $req->header('Content-Length' => length $request);
94  
95 0 my $res = $self->ua->request($req);
96  
97 0 ▶ if (!$res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
98 0 croak(
99 sprintf(
100 "Unable to perform request: %s (%s)",
101 $res->message, $res->code
102 )
103 );
104 }
105  
106 0 return $self->handle_response($res->decoded_content);
107  
108 }
109  
110  
111 5 ▶sub handle_response {
Subroutine handle_response: called CC=4 SCAR=13.9
Pod documented
112 5 my ($self, $response) = @_;
113  
114 5 my $saml = _get_saml_from_soap($response);
115 5 my @errors;
116 5 foreach my $cert (@{$self->idp_cert}) {
117 6 ▶ my $success = try {
Subroutine __ANON__: called CC=2 SCAR=7.2
118 $self->verify_xml(
119 $saml,
120 no_xml_declaration => 1,
121 cert_text => $cert,
122 cacert => $self->cacert,
123 anchors => $self->anchors,
124 $self->has_verify ? (
125 ns => { 'artifact' => $self->verify->{ns} },
126 id_attr => '/artifact:' . $self->verify->{attr_id},
127 6 ▶ ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$self->has_verify ? : 0 6 6
128 );
129 4 return 1;
130 }
131 6 ▶ catch { push (@errors, $_); return 0; };
Subroutine __ANON__: called CC=1 SCAR=0.0
132  
133 6 ▶ return $saml if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 4 2 6
134 }
135  
136 1 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 1 0 1
137 1 croak "Unable to verify XML with the given certificates: "
138 . join(", ", @errors);
139 }
140 }
141  
142  
143 1 ▶sub handle_request {
Subroutine handle_request: called CC=5 SCAR=19.4
Pod documented
144 1 my ($self, $request) = @_;
145  
146 1 my $saml = _get_saml_from_soap($request);
147 1 my @errors;
148 1 ▶ if (defined $saml) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $saml) 1 0 1
149 1 foreach my $cert (@{$self->idp_cert}) {
150 1 ▶ my $success = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
151 1 $self->verify_xml(
152 $saml,
153 cert_text => $cert,
154 cacert => $self->cacert
155 );
156 1 return 1;
157 }
158 0 ▶ catch { push (@errors, $_); return 0; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
159 1 ▶ return $saml if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 1 0 1
160 }
161  
162 0 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 0 0
163 0 croak "Unable to verify XML with the given certificates: "
164 . join(", ", @errors);
165 }
166 }
167  
168 0 return;
169 }
170  
171 2 ▶sub _get_saml_from_soap {
Subroutine _get_saml_from_soap: called CC=2 SCAR=7.1
172 2 my $soap = shift;
173 2 my $dom = no_comments($soap);
174 2 my $parser = XML::LibXML::XPathContext->new($dom);
175 2 $parser->registerNs('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
176 2 $parser->registerNs('samlp', 'urn:oasis:names:tc:SAML:2.0:protocol');
177 2 my $set = $parser->findnodes('/soap-env:Envelope/soap-env:Body/*');
178 2 ▶ if ($set->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->size) 2 0 2
179 2 return $set->get_node(1)->toString();
180 }
181 0 return;
182 }
183  
184  
185 1 ▶sub create_soap_envelope {
Subroutine create_soap_envelope: called CC=3 SCAR=12.0
Pod documented
186 1 my ($self, $message) = @_;
187  
188 # sign the message
189 1 my $sig = Net::SAML2::XML::Sig->new({
190 x509 => 1,
191 key => $self->key,
192 cert => $self->cert,
193 exclusive => 1,
194 no_xml_declaration => 1,
195 });
196 1 my $signed_message = $sig->sign($message);
197  
198 # OpenSSO ArtifactResolve hack
199 #
200 # OpenSSO's ArtifactResolve parser is completely hateful. It demands that
201 # the order of child elements in an ArtifactResolve message be:
202 #
203 # 1: saml:Issuer
204 # 2: dsig:Signature
205 # 3: samlp:Artifact
206 #
207 # Really.
208 #
209 1 ▶ if ($signed_message =~ /ArtifactResolve/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signed_message =~ /ArtifactResolve/) 0 1 1
210 0 $signed_message =~ s!(<dsig:Signature.*?</dsig:Signature>)!!s;
211 0 my $signature = $1;
212 0 $signed_message =~ s/(<\/saml:Issuer>)/$1$signature/;
213 }
214  
215 # test verify
216 1 my $ret = $sig->verify($signed_message);
217 1 ▶ die "failed to sign" unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 1 1
218  
219 1 my $soap = <<"SOAP";
220 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body>$signed_message</SOAP-ENV:Body></SOAP-ENV:Envelope>
221 SOAP
222 1 return $soap;
223 }
224  
225 32 __PACKAGE__->meta->make_immutable;
226  
227 __END__
« SAML2/Protocol/Artifact.pm ↑ Summary SAML2/Binding/POST.pm »