SAML2/Binding/POST.pm

filter: Statementstmt 100% 51 / 51 Branchbran 70.0% 7 / 10 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 93.9% 78 / 83 CC 8 SCAR 11.1
CC
8
Cov
94%
handle_response
6.0
sign_xml
3.0
BEGIN
1.0
CRAP 3.0
« SAML2/Binding/SOAP.pm ↑ Summary SAML2/Types.pm »
1 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::SAML2::Binding::POST;
4 32 our $VERSION = '0.89'; # VERSION
5  
6 32 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: HTTP POST binding for SAML
10  
11  
12 32 ▶use Net::SAML2::XML::Sig;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use MIME::Base64 qw/ decode_base64 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use Crypt::OpenSSL::Verify;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 32 with 'Net::SAML2::Role::VerifyXML';
19  
20  
21 32 has 'cacert' => (isa => 'Maybe[Str]', is => 'ro');
22 32 has 'cert' => (isa => 'Str', is => 'ro', required => 0, predicate => 'has_cert');
23 32 has 'cert_text' => (isa => 'Str', is => 'ro');
24 32 has 'key' => (isa => 'Str', is => 'ro', required => 0, predicate => 'has_key');
25 32 has 'insecure_trust_embedded_cert' => (isa => 'Bool', is => 'ro', default => 0);
26  
27  
28 6 ▶sub handle_response {
Subroutine handle_response: called CC=6 SCAR=18.0
Pod documented
29 6 my ($self, $response) = @_;
30  
31 6 ▶ unless ($self->cacert
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cacert or $self->cert_text or $self->insecure_trust_embedded_cert) 1 5 6
Condition83%5/6
$self->cacert or $self->cert_text or $self->insecure_trust_embedded_cert
l!l&&r!l&&!r
411
$self->cacert or $self->cert_text
l!l&&r!l&&!r
402
MC/DC66%2/3
$self->cacert or $self->cert_text or $self->insecure_trust_embedded_cert
$self->cacert $self->cert_text $self->insecure_trust_embedded_cert
Truth table75%3/4
A or B or C
A= $self->cacertB= $self->cert_textC= $self->insecure_trust_embedded_cert
ABCresult
1XX1
01X1
0011
0000
32 || $self->cert_text
33 || $self->insecure_trust_embedded_cert) {
34 1 croak(
35 "Net::SAML2::Binding::POST::handle_response requires 'cacert' "
36 . "or 'cert_text' on the binding to verify SAML response "
37 . "signatures. To explicitly disable signature verification "
38 . "(test/dev only), pass insecure_trust_embedded_cert => 1 to new()."
39 );
40 }
41  
42 # unpack and check the signature
43 5 my $xml = decode_base64($response);
44  
45 5 ▶ $self->verify_xml(
Branch: 2 branches
BranchTrueFalseTotal
$self->cert_text ? : 0 5 5
$self->cacert ? : 4 1 5
46 $xml,
47 no_xml_declaration => 1,
48 $self->cert_text ? (
49 cert_text => $self->cert_text
50 ) : (),
51 $self->cacert ? (
52 cacert => $self->cacert
53 ) : (),
54  
55 );
56 4 return $xml;
57 }
58  
59  
60 3 ▶sub sign_xml {
Subroutine sign_xml: called CC=3 SCAR=11.1
Pod documented
61 3 my ($self, $request) = @_;
62  
63 3 ▶ croak("Need to have a cert specified") unless $self->has_cert;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cert 0 3 3
64 3 ▶ croak("Need to have a key specified") unless $self->has_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_key 0 3 3
65  
66 3 my $signer = XML::Sig->new({
67 key => $self->key,
68 cert => $self->cert,
69 no_xml_declaration => 1,
70 }
71 );
72  
73 3 my $signed_message = $signer->sign($request);
74  
75 # saml-schema-protocol-2.0.xsd Schema hack
76 #
77 # The real fix here is to fix XML::Sig to accept a XPATH to
78 # place the signature in the correct location. Or use XML::LibXML
79 # here to do so
80 #
81 # The protocol schema defines a sequence which requires the order
82 # of the child elements in a Protocol based message:
83 #
84 # The dsig:Signature (should it exist) MUST follow the saml:Issuer
85 #
86 # 1: saml:Issuer
87 # 2: dsig:Signature
88 #
89 # Seems like an oversight in the SAML schema specifiation but...
90  
91 3 $signed_message =~ s!(<dsig:Signature.*?</dsig:Signature>)!!s;
92 3 my $signature = $1;
93 3 $signed_message =~ s/(<\/saml\d*:Issuer>)/$1$signature/;
94  
95 3 my $encoded_request = encode_base64($signed_message, "\n");
96  
97 3 return $encoded_request;
98  
99 }
100 32 __PACKAGE__->meta->make_immutable;
101  
102 __END__
« SAML2/Binding/SOAP.pm ↑ Summary SAML2/Types.pm »