SAML2/Object/Response.pm

filter: Statementstmt 99.0% 103 / 104 Branchbran 90.6% 29 / 32 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 19 / 19 Podpod 100% 3 / 3 totaltotal 97.7% 176 / 180 CC 22 SCAR 19.6
CC
22
Cov
97%
new_from_xml
15.0
to_assertion
5.1
__ANON__
4.0
CRAP 7.1
« SAML2/Protocol/Assertion.pm ↑ Summary SAML2/Binding/Redirect.pm »
1 package Net::SAML2::Object::Response;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = '0.89'; # VERSION
5  
6 3 ▶use overload '""' => 'to_string';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: A response object
9  
10 3 ▶use MooseX::Types::DateTime qw/ DateTime /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DateTime::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use DateTime::Format::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Net::SAML2::XML::Util qw/ no_comments /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Net::SAML2::XML::Sig;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Net::SAML2::Protocol::Assertion;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use XML::Enc;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use URN::OASIS::SAML2 qw(STATUS_SUCCESS URN_ASSERTION URN_PROTOCOL);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 with 'Net::SAML2::Role::ProtocolMessage';
25  
26  
27 3 has _dom => (
28 is => 'ro',
29 isa => 'XML::LibXML::Node',
30 init_arg => 'dom',
31 required => 1,
32 );
33  
34 3 has status => (
35 is => 'ro',
36 isa => 'Str',
37 required => 1,
38 );
39  
40 3 has substatus => (
41 is => 'ro',
42 isa => 'Str',
43 required => 0,
44 predicate => 'has_substatus',
45 );
46  
47 3 has assertions => (
48 is => 'ro',
49 isa => 'XML::LibXML::NodeList',
50 required => 0,
51 predicate => 'has_assertions',
52 );
53  
54 3 has 'cacert' => (
55 isa => 'Str',
56 is => 'ro',
57 required => 0);
58  
59 3 has 'cert_text' => (
60 isa => 'Str',
61 is => 'ro',
62 required => 0);
63  
64 3 has 'insecure_trust_embedded_cert' => (
65 isa => 'Bool',
66 is => 'ro',
67 default => 0,
68 );
69  
70 3 has 'require_signed_response' => (
71 isa => 'Bool',
72 is => 'ro',
73 default => 0,
74 );
75  
76 # BUILDARGS
77  
78 13 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
79 13 my $orig = shift;
80 13 my $self = shift;
81  
82 13 my %params = @_;
83 13 ▶ unless ($params{cacert}
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"cacert"} or $params{"cert_text"} or $params{"insecure_trust_embedded_cert"}) 1 12 13
Condition100%6/6
$params{"cacert"} or $params{"cert_text"} or $params{"insecure_trust_embedded_cert"}
l!l&&r!l&&!r
571
$params{"cacert"} or $params{"cert_text"}
l!l&&r!l&&!r
418
MC/DC100%3/3
$params{"cacert"} or $params{"cert_text"} or $params{"insecure_trust_embedded_cert"}
$params{"cacert"} $params{"cert_text"} $params{"insecure_trust_embedded_cert"}
Truth table100%4/4
A or B or C
A= $params{"cacert"}B= $params{"cert_text"}C= $params{"insecure_trust_embedded_cert"}
ABCresult
1XX1
01X1
0011
0000
84 || $params{cert_text}
85 || $params{insecure_trust_embedded_cert}) {
86 1 croak(
87 "Net::SAML2::Object::Response->new() requires 'cacert' or "
88 . "'cert_text' on the object to verify SAML response signatures. "
89 . "To explicitly disable signature verification (test/dev only) "
90 . ", pass insecure_trust_embedded_cert => 1 to new()."
91 );
92 }
93  
94 12 return $self->$orig(%params);
95 3 };
96  
97  
98 16 ▶sub new_from_xml {
Subroutine new_from_xml: called CC=15 SCAR=27.1
Pod documented
99 16 my $self = shift;
100 16 my %args = @_;
101  
102 16 my $xml = no_comments($args{xml});
103 16 my $destination = delete $args{destination};
104 16 my $cacert = delete $args{cacert};
105 16 my $cert_text = delete $args{cert_text};
106 16 my $insecure_trust_embedded_cert = delete $args{insecure_trust_embedded_cert};
107  
108 # The default may change in the future
109 16 ▶ my $require_signed_response = delete $args{require_signed_response} // 0;
Condition100%2/2
delete $args{"require_signed_response"} // 0
l!l
97
MC/DC100%1/1
delete $args{"require_signed_response"} // 0
delete $args{"require_signed_response"}
Truth table100%2/2
A
A= delete $args{"require_signed_response"}
Aresult
11
00
110  
111 16 my $xpath = XML::LibXML::XPathContext->new($xml);
112 16 $xpath->registerNs('saml', URN_ASSERTION);
113 16 $xpath->registerNs('samlp', URN_PROTOCOL);
114 16 $xpath->registerNs('dsig', 'http://www.w3.org/2000/09/xmldsig#');
115  
116 16 my $actual_destination = $xpath->findvalue(
117 '/samlp:Response/@Destination | /samlp:ArtifactResponse/@Destination');
118 16 ▶ if (defined $destination && $destination ne $actual_destination) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $destination and $destination ne $actual_destination) 2 14 16
Condition100%3/3
defined $destination and $destination ne $actual_destination
!ll&&!rl&&r
1222
MC/DC100%2/2
defined $destination and $destination ne $actual_destination
defined $destination $destination ne $actual_destination
Truth table100%3/3
A and B
A= defined $destinationB= $destination ne $actual_destination
ABresult
0X0
100
111
119 2 croak(sprintf("Response Destination (%s) does not match expected value (%s)",
120 $actual_destination,
121 $destination)
122 );
123 }
124  
125 14 my $response = $xpath->findnodes('/samlp:Response|/samlp:ArtifactResponse');
126 14 ▶ croak("Unable to parse response") unless $response->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->size 0 14 14
127 14 $response = $response->get_node(1);
128  
129 # require_signed_response only applies to front-channel Responses.
130 #
131 # ArtifactResponse is retrieved by the SP over a mutually authenticated
132 # TLS back-channel (the SP initiates the connection to the IdP's known
133 # artifact resolution endpoint), so transport already authenticates it
134 # and it is exempt from this check.
135  
136 14 my $is_artifact = $response->nodePath eq '/samlp:ArtifactResponse';
137  
138 14 ▶ if ($require_signed_response && !$is_artifact) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_signed_response and not $is_artifact) 2 12 14
Condition100%3/3
$require_signed_response and not $is_artifact
!ll&&!rl&&r
1112
MC/DC100%2/2
$require_signed_response and not $is_artifact
$require_signed_response not $is_artifact
Truth table100%3/3
A and not(B)
A= $require_signed_responseB= not $is_artifact
ABresult
0X0
100
111
139 2 my $signature = $xpath->findnodes('/samlp:Response/dsig:Signature', $response);
140 # FIXME: should check that a Response-level Signature cryptographically
141 # validates and it actually covers this Response - Assertion verification
142 # happens downstream.
143 2 ▶ if ($signature->size != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature->size != 1) 1 1 2
144 1 croak "Net::SAML2::Object::Response requires that the Response "
145 . "include exactly one Signature. Pass `require_signed_response` = 0 "
146 . "to allow unsigned Responses";
147 }
148 }
149  
150 13 my $code_path = 'samlp:Status/samlp:StatusCode';
151 13 ▶ if ($is_artifact) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_artifact) 2 11 13
152 2 $code_path = "samlp:Response/$code_path";
153 }
154  
155 13 my $status = $xpath->findnodes($code_path, $response);
156 13 ▶ croak("Unable to parse status from response") unless $status->size;
Branch: 1 branch
BranchTrueFalseTotal
unless $status->size 0 13 13
157  
158 13 my $status_node = $status->get_node(1);
159 13 $status = $status_node->getAttribute('Value');
160  
161 13 my $substatus = $xpath->findvalue('samlp:StatusCode/@Value', $status_node);
162  
163 13 my $nodes = $xpath->findnodes('//saml:EncryptedAssertion|//saml:Assertion', $response);
164  
165 13 ▶ return $self->new(
Branch: 5 branches
BranchTrueFalseTotal
$substatus ? : 1 12 13
$nodes->size ? : 10 3 13
$cacert ? : 4 9 13
$cert_text ? : 1 12 13
$insecure_trust_embedded_cert ? : 7 6 13
166 dom => $xml,
167 status => $status,
168 $substatus ? ( substatus => $substatus) : (),
169 issuer => $xpath->findvalue('saml:Issuer', $response),
170 id => $response->getAttribute('ID'),
171 in_response_to => $response->getAttribute('InResponseTo'),
172 $nodes->size ? (assertions => $nodes) : (),
173 $cacert ? (cacert => $cacert) : (),
174 $cert_text ? (cert_text => $cert_text) : (),
175 $insecure_trust_embedded_cert ? (insecure_trust_embedded_cert => $insecure_trust_embedded_cert) : (),
176 require_signed_response => $require_signed_response,
177 );
178 }
179  
180  
181 7 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
182 7 my $self = shift;
183 7 return $self->_dom->toString;
184 }
185  
186  
187 5 ▶sub to_assertion {
Subroutine to_assertion: called CC=5 SCAR=16.3
Pod documented
188 5 my $self = shift;
189 5 my %args = @_;
190  
191 5 ▶ if (!$self->has_assertions) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_assertions) 0 5 5
192 0 croak("There are no assertions found in the response object");
193 }
194  
195 # Propagate the trust configuration from the Response so the Assertion
196 # inherits the same trust anchor. Without this the caller would have to
197 # supply cacert (or insecure_trust_embedded_cert) a second time, and a
198 # Response built with a trust anchor could silently produce an Assertion
199 # built with none. Caller-supplied %args still override these defaults.
200 5 ▶ return Net::SAML2::Protocol::Assertion->new_from_xml(
Branch: 3 branches
BranchTrueFalseTotal
$self->cacert ? : 1 4 5
$self->cert_text ? : 1 4 5
$self->insecure_trust_embedded_cert ? : 3 2 5
201 $self->cacert ? (cacert => $self->cacert) : (),
202 $self->cert_text ? (cert_text => $self->cert_text) : (),
203 $self->insecure_trust_embedded_cert
204 ? (insecure_trust_embedded_cert => $self->insecure_trust_embedded_cert)
205 : (),
206 %args,
207 xml => $self->to_string,
208 );
209 }
210  
211 3 1;
212  
213  
214 3 __PACKAGE__->meta->make_immutable;
215  
216 __END__
« SAML2/Protocol/Assertion.pm ↑ Summary SAML2/Binding/Redirect.pm »