lib/WebService/Braintree/WebhookNotificationGateway.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 6 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 97.1% 69 / 71 CC 6 SCAR 6.0
CC
6
Cov
100%
_matching_signature
3.0
_validate_signature
3.0
verify
2.0
CRAP 1.8
« lib/WebService/Braintree/Configuration.pm ↑ Summary lib/WebService/Braintree/ValidationErrorCollection.pm »
1 # vim: sw=4 ts=4 ft=perl
2  
3 package # hide from pause
4 WebService::Braintree::WebhookNotificationGateway;
5  
6 22 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 22 ▶use WebService::Braintree::Digest qw(hexdigest);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use WebService::Braintree::Xml qw(xml_to_hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 ▶use WebService::Braintree::_::WebhookNotification;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # This does not use Role::MakeRequest which provides the gateway attribute.
20 22 has 'gateway' => (is => 'ro');
21  
22 15 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
23 15 my ($self, $signature, $payload) = @_;
24  
25 15 $self->_validate_signature($signature, $payload);
26  
27 12 my $xml_payload = decode_base64($payload);
28 12 my $attributes = xml_to_hash($xml_payload);
29  
30 12 WebService::Braintree::_::WebhookNotification->new($attributes->{notification});
31 }
32  
33 15 ▶sub _matching_signature {
Subroutine _matching_signature: called CC=3 SCAR=11.0
34 15 my ($self, $signature, $payload) = @_;
35  
36 15 foreach my $signature_pair (split("&", $signature)) {
37 15 my @components = split("\\|", $signature_pair);
38  
39 15 ▶ if ($components[0] eq $self->gateway->config->public_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($components[0] eq $self->gateway->config->public_key) 13 2 15
40 13 return $components[1];
41 }
42 }
43  
44 # Definitively return undef if nothing is found.
45 2 return;
46 }
47  
48 15 ▶sub _validate_signature {
Subroutine _validate_signature: called CC=3 SCAR=11.0
49 15 my ($self, $signature, $payload) = @_;
50  
51 15 my $matching_signature = $self->_matching_signature($signature, $payload);
52  
53 15 ▶ if (!defined($matching_signature) || $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $matching_signature or $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)) 3 12 15
Condition100%3/3
not defined $matching_signature or $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)
l!l&&r!l&&!r
2112
MC/DC100%2/2
not defined $matching_signature or $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)
not defined $matching_signature $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)
Truth table100%3/3
not(A) or B
A= not defined $matching_signatureB= $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)
ABresult
1X1
011
000
54 3 confess "InvalidSignature";
55 }
56  
57 12 return 1;
58 }
59  
60 2 ▶sub verify {
Subroutine verify: called CC=2 SCAR=6.9
Pod undocumented
61 2 my ($self, $challenge) = @_;
62  
63 2 ▶ if ($challenge !~ /^[a-f0-9]{20,32}$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($challenge =~ /^[a-f0-9]{20,32}$/) 1 1 2
64 1 confess "InvalidChallenge";
65 }
66  
67 1 return $self->gateway->config->public_key . "|" . hexdigest($self->gateway->config->private_key, $challenge);
68 }
69  
70 22 __PACKAGE__->meta->make_immutable;
71  
72 22 1;
73 __END__
« lib/WebService/Braintree/Configuration.pm ↑ Summary lib/WebService/Braintree/ValidationErrorCollection.pm »