Braintree/WebhookNotificationGateway.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 54 CC 4 SCAR 14.3
CC
4
Cov
0%
_matching_signature
6.0
_validate_signature
6.0
verify
6.0
CRAP 4.2
1 package Net::Braintree::WebhookNotificationGateway;
2  
3 use MIME::Base64;
4 use Net::Braintree::Digest qw(hexdigest);
5 use Net::Braintree::Xml qw(xml_to_hash);
6 use Moose;
7  
8 has 'gateway' => (is => 'ro');
9  
10 sub parse {
11 my ($self, $signature, $payload) = @_;
12 $self->_validate_signature($signature, $payload);
13 my $xml_payload = decode_base64($payload);
14 my $attributes = xml_to_hash($xml_payload);
15  
16 Net::Braintree::WebhookNotification->new($attributes->{notification});
17 }
18  
19 sub _matching_signature {
20 my ($self, $signature, $payload) = @_;
21 foreach my $signature_pair (split("&", $signature)) {
22 my @components = split("\\|", $signature_pair);
23  
24 if ($components[0] eq $self->gateway->config->public_key) {
25 return $components[1];
26 }
27 }
28 }
29  
30 sub _validate_signature {
31 my ($self, $signature, $payload) = @_;
32 my $matching_signature = $self->_matching_signature($signature, $payload);
33 if (defined($matching_signature) && $matching_signature ne hexdigest($self->gateway->config->private_key, $payload)) {
34 confess "InvalidSignature";
35 }
36 }
37  
38 sub verify {
39 my ($self, $challenge) = @_;
40 if ($challenge !~ /^[a-f0-9]{20,32}$/) {
41 confess "InvalidChallenge";
42 }
43 return $self->gateway->config->public_key . "|" . hexdigest($self->gateway->config->private_key, $challenge);
44 }
45  
46 1;