GoCardless/Utils.pm

filter: Statementstmt 100% 81 / 81 Branchbran 100% 26 / 26 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 97.2% 142 / 146 CC 22 SCAR 17.0
CC
22
Cov
98%
signature_valid
10.0
_flattened_size
6.0
_flatten_param
5.0
CRAP 5.5
« GoCardless/Exception.pm ↑ Summary GoCardless/Resource.pm »
1 package Business::GoCardless::Utils;
2  
3 =head1 NAME
4  
5 Business::GoCardless::Utils
6  
7 =head1 DESCRIPTION
8  
9 A role containing gocardless utilities.
10  
11 =cut
12  
13 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 58 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 58 ▶use MIME::Base64 qw/ encode_base64 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 58 ▶use Digest::SHA qw/ hmac_sha256_hex /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 58 our $max_legacy_params_size = 1024 * 1024;
22  
23 =head1 METHODS
24  
25 =head2 sign_params
26  
27 Signs the passed params hash using the app secret
28  
29 my $signature = $self->sign_params( \%params,$app_secret );
30  
31 =cut
32  
33 19 ▶sub sign_params {
Subroutine sign_params: called CC=1 SCAR=0.0
Pod documented
34 19 my ( $self,$params,$app_secret ) = @_;
35  
36 19 return hmac_sha256_hex(
37 $self->normalize_params( $params ),
38 $app_secret
39 );
40 }
41  
42 =head2 signature_valid
43  
44 Checks the signature is valid for the given params hash with the app secret
45  
46 if ( ! $self->signature_valid( \%params,$app_secret ) ) {
47 # throw an error
48 }
49  
50 A params hash that would flatten to more than
51 C<$Business::GoCardless::Utils::max_legacy_params_size> bytes of keys and
52 values (default 1 MiB, 0 for no limit) is not valid, as checking it would
53 cost time and memory quadratic in its size. An undef or empty secret never
54 validates a signature.
55  
56 =cut
57  
58 39 ▶sub signature_valid {
Subroutine signature_valid: called CC=10 SCAR=23.0
Pod documented
59 39 my ( $self,$params,$app_secret,$sig ) = @_;
60  
61 # for testing, use live at your own risk
62 39 ▶ return 1 if $ENV{GOCARDLESS_SKIP_SIG_CHECK};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GOCARDLESS_SKIP_SIG_CHECK"} 3 36 39
63  
64 # more than one secret (e.g. when rotating the secret), any can match
65 36 ▶ if ( ref( $app_secret ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app_secret eq "ARRAY") 5 31 36
66 5 foreach my $secret ( @{ $app_secret } ) {
67 9 ▶ return 1 if $self->signature_valid( $params,$secret,$sig );
Branch: 1 branch
BranchTrueFalseTotal
if $self->signature_valid($params, $secret, $sig) 3 6 9
68 }
69 2 return 0;
70 }
71  
72 # with no key anyone can compute the signature
73 31 ▶ return 0 if ! defined( $app_secret ) || ! length( $app_secret );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app_secret and length $app_secret 3 28 31
Condition100%3/3
defined $app_secret and length $app_secret
!ll&&!rl&&r
2128
MC/DC100%2/2
defined $app_secret and length $app_secret
defined $app_secret length $app_secret
Truth table100%3/3
A and B
A= defined $app_secretB= length $app_secret
ABresult
0X0
100
111
74  
75 28 ▶ if ( $sig ) { # version > 1 (since we don't have access to ->client here)
Branch: 1 branch
BranchTrueFalseTotal
if ($sig) { } 16 12 28
76 16 return $sig eq hmac_sha256_hex( $params,$app_secret );
77 } else {
78 # delete local is 5.12+ only so need to copy hash here
79 12 my $params_copy = { %{ $params } };
80 12 $sig = delete( $params_copy->{signature} );
81 12 ▶ return 0 if $max_legacy_params_size
Branch: 1 branch
BranchTrueFalseTotal
if $Business::GoCardless::Utils::max_legacy_params_size and _flattened_size($params_copy, $Business::GoCardless::Utils::max_legacy_params_size) > $Business::GoCardless::Utils::max_legacy_params_size 2 10 12
Condition100%3/3
$Business::GoCardless::Utils::max_legacy_params_size and _flattened_size($params_copy, $Business::GoCardless::Utils::max_legacy_params_size) > $Business::GoCardless::Utils::max_legacy_params_size
!ll&&!rl&&r
192
MC/DC100%2/2
$Business::GoCardless::Utils::max_legacy_params_size and _flattened_size($params_copy, $Business::GoCardless::Utils::max_legacy_params_size) > $Business::GoCardless::Utils::max_legacy_params_size
$Business::GoCardless::Utils::max_legacy_params_size _flattened_size($params_copy, $Business::GoCardless::Utils::max_legacy_params_size) > $Business::GoCardless::Utils::max_legacy_params_size
Truth table100%3/3
A and B
A= $Business::GoCardless::Utils::max_legacy_params_sizeB= _flattened_size($params_copy, $Business::GoCardless::Utils::max_legacy_params_size) > $Business::GoCardless::Utils::max_legacy_params_size
ABresult
0X0
100
111
82 && _flattened_size( $params_copy,$max_legacy_params_size )
83 > $max_legacy_params_size;
84 10 return $sig eq $self->sign_params( $params_copy,$app_secret );
85 }
86 }
87  
88 =head2 generate_nonce
89  
90 Generates a random nonce for use with a gocardless request, it being a base64
91 encoded concatination of the current seconds since epoch + | + rand(256)
92  
93 my $nonce = $self->generate_nonce;
94  
95 =cut
96  
97 4 ▶sub generate_nonce {
Subroutine generate_nonce: called CC=1 SCAR=0.0
Pod documented
98 4 my ( $self ) = @_;
99  
100 4 chomp( my $nonce = encode_base64( time . '|' . rand(256) ) );
101 4 return $nonce;
102 }
103  
104 =head2 flatten_params
105  
106 Flattens a hash as specified by the gocardless API. see
107 https://developer.gocardless.com/#constructing-the-parameter-array
108  
109 my $flat_params = $self->flatten_params( \%params );
110  
111 =cut
112  
113 102 ▶sub flatten_params {
Subroutine flatten_params: called CC=1 SCAR=0.0
Pod documented
114 102 my ( $self,$params ) = @_;
115  
116 return [
117 166 map { _flatten_param( $_,$params->{$_} ) }
118 102 sort keys( %{ $params } )
119 ];
120 }
121  
122 =head2 normalize_params
123  
124 Normalizes the passed params hash into a string for use in queries to the
125 gocardless API. Includes param flattening and RFC5849 encoding
126  
127 my $query_string = $self->normalize_params( \%params );
128  
129 =cut
130  
131 104 ▶sub normalize_params {
Subroutine normalize_params: called CC=4 SCAR=13.9
Pod documented
132 104 my ( $self,$params ) = @_;
133  
134 return join( '&',
135 2305 map { $_->[0] . '=' . $_->[1] }
136 2305 map { [ _rfc5849_encode( $_->[0] ),_rfc5849_encode( $_->[1] ) ] }
137 2474 ▶ sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] }
Condition100%3/3
$Business::GoCardless::Utils::a->[0] cmp $Business::GoCardless::Utils::b->[0] || $Business::GoCardless::Utils::a->[1] cmp $Business::GoCardless::Utils::b->[1]
l!l&&r!l&&!r
428252021
MC/DC100%2/2
$Business::GoCardless::Utils::a->[0] cmp $Business::GoCardless::Utils::b->[0] || $Business::GoCardless::Utils::a->[1] cmp $Business::GoCardless::Utils::b->[1]
$Business::GoCardless::Utils::a->[0] cmp $Business::GoCardless::Utils::b->[0] $Business::GoCardless::Utils::a->[1] cmp $Business::GoCardless::Utils::b->[1]
Truth table100%3/3
A || B
A= $Business::GoCardless::Utils::a->[0] cmp $Business::GoCardless::Utils::b->[0]B= $Business::GoCardless::Utils::a->[1] cmp $Business::GoCardless::Utils::b->[1]
ABresult
1X1
011
000
138 104 ▶ @{ ref( $params ) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $params eq 'HASH' ? : 99 5 104
Condition50%1/2
$params // []
l!l
05
MC/DC0%0/1
$params // []
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
139 ? $self->flatten_params( $params )
140 : ( $params // [] )
141 }
142 );
143 }
144  
145 3357 ▶sub _flatten_param {
Subroutine _flatten_param: called CC=5 SCAR=16.1
146 3357 my( $key,$value ) = @_;
147  
148 3357 my @r;
149  
150 3357 ▶ if ( ref( $value ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 1033 2324 3357
elsif (ref $value eq 'ARRAY') { } 11 2313 2324
151 1033 foreach my $sub_key ( sort keys( %{ $value } ) ) {
152 174 push( @r,_flatten_param( "$key\[$sub_key\]",$value->{$sub_key} ) );
153 }
154 } elsif ( ref( $value ) eq 'ARRAY' ) {
155 11 foreach my $sub_key ( @{ $value } ) {
156 3017 push( @r,_flatten_param( "$key\[\]",$sub_key ) );
157 }
158 } else {
159 2313 push( @r,[ $key,$value ] );
160 }
161  
162 3357 return @r;
163 }
164  
165 # the bytes of the keys (one per hash, array and leaf) and values that
166 # flatten_params builds, counted until they pass $limit
167 11 ▶sub _flattened_size {
Subroutine _flattened_size: called CC=6 SCAR=17.9
168 11 my ( $params,$limit ) = @_;
169  
170 11 my $size = 0;
171 11 my @todo = map { [ length( $_ ),$params->{$_} ] } keys( %{ $params } );
172  
173 11 ▶ while ( my $item = pop( @todo ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = pop @todo) 2017 9 2026
174 2017 my ( $key_length,$value ) = @{ $item };
175  
176 2017 $size += $key_length;
177 2017 ▶ return $size if $size > $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $size > $limit 2 2015 2017
178  
179 2015 ▶ if ( ref( $value ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 963 1052 2015
elsif (ref $value eq 'ARRAY') { } 7 1045 1052
180 963 push( @todo,map { [ $key_length + length( $_ ) + 2,$value->{$_} ] } keys( %{ $value } ) );
181 } elsif ( ref( $value ) eq 'ARRAY' ) {
182 7 push( @todo,map { [ $key_length + 2,$_ ] } @{ $value } );
183 } else {
184 1045 ▶ $size += length( $value // '' );
Condition50%1/2
$value // ""
l!l
10450
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
185 }
186 }
187  
188 9 return $size;
189 }
190  
191 4610 ▶sub _rfc5849_encode {
Subroutine _rfc5849_encode: called CC=1 SCAR=0.0
192 4610 my ( $str ) = @_;
193  
194 4610 $str =~ s#([^-.~_a-z0-9])#sprintf('%%%02X', ord($1))#gei;
195 4610 return $str;
196 }
197  
198 =head1 AUTHOR
199  
200 Lee Johnson - C<lee@payprop.com>
201  
202 This library is free software; you can redistribute it and/or modify it under
203 the same terms as Perl itself. If you would like to contribute documentation,
204 features, bug fixes, or anything else then please raise an issue / pull request:
205  
206 https://github.com/payprop/business-gocardless
207  
208 =cut
209  
210 58 1;
211  
212 # vim: ts=4:sw=4:et
« GoCardless/Exception.pm ↑ Summary GoCardless/Resource.pm »