Net/Amazon/S3/Signature/V4Implementation.pm

filter: Statementstmt 97.5% 158 / 162 Branchbran 65.7% 25 / 38 Conditioncond 68.4% 13 / 19 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 27 / 27 Podpod 66.6% 2 / 3 totaltotal 88.1% 230 / 261 SCAR 34.0
CC
29
Cov
89%
_augment_uri
10.4
_simplify_uri
7.6
_sort_query_string
5.0
CRAP 30.0
« Shared/Examples/Net/Amazon/S3.pm ↑ Summary Net/Amazon/S3.pm »
1 package Net::Amazon::S3::Signature::V4Implementation;
2 # ABSTRACT: Implements the Amazon Web Services signature version 4, AWS4-HMAC-SHA256 (copy of Net::Amazon::Signature::V4)
3 99 $Net::Amazon::S3::Signature::V4Implementation::VERSION = '0.99';
4  
5 99 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 99 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 99 ▶use Digest::SHA qw/sha256_hex hmac_sha256 hmac_sha256_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 99 ▶use Time::Piece ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 99 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 99 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 99 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 99 our $ALGORITHM = 'AWS4-HMAC-SHA256';
16 99 our $MAX_EXPIRES = 604800; # Max, 7 days
17  
18 99 our $X_AMZ_ALGORITHM = 'X-Amz-Algorithm';
19 99 our $X_AMZ_CONTENT_SHA256 = 'X-Amz-Content-Sha256';
20 99 our $X_AMZ_CREDENTIAL = 'X-Amz-Credential';
21 99 our $X_AMZ_DATE = 'X-Amz-Date';
22 99 our $X_AMZ_EXPIRES = 'X-Amz-Expires';
23 99 our $X_AMZ_SIGNEDHEADERS = 'X-Amz-SignedHeaders';
24 99 our $X_AMZ_SIGNATURE = 'X-Amz-Signature';
25  
26  
27  
28 sub new {
29 7 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 7 my ( $access_key_id, $secret, $endpoint, $service ) = @_;
31 7 my $self = {
32 access_key_id => $access_key_id,
33 secret => $secret,
34 endpoint => $endpoint,
35 service => $service,
36 };
37 7 bless $self, $class;
38 7 return $self;
39 }
40  
41  
42 sub sign {
43 1 ▶ my ( $self, $request ) = @_;
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
44  
45 1 $request = $self->_augment_request( $request );
46  
47 1 my $authz = $self->_authorization( $request );
48 1 $request->header( Authorization => $authz );
49 1 return $request;
50 }
51  
52  
53 sub sign_uri {
54 6 ▶ my ( $self, $uri, $expires_in, $for_method ) = @_;
Subroutine sign_uri: called CC=1 SCAR=0.0
Pod documented
55  
56 6 my $request = $self->_augment_uri( $uri, $expires_in, $for_method );
57  
58 6 my $signature = $self->_signature( $request );
59  
60 6 $uri = $request->uri;
61 6 my $query = $uri->query;
62 6 $uri->query( undef );
63 6 $uri = $uri . '?' . $self->_sort_query_string( $query );
64 6 $uri .= "&$X_AMZ_SIGNATURE=$signature";
65  
66 6 return $uri;
67 }
68  
69 # _headers_to_sign:
70 # Return the sorted lower case headers as required by the generation of canonical headers
71  
72 sub _headers_to_sign {
73 15 ▶ my $req = shift;
Subroutine _headers_to_sign: called CC=2 SCAR=6.9
74  
75 15 ▶ my @headers_to_sign = $req->uri->query_param( $X_AMZ_SIGNEDHEADERS )
Branch: 1 branch
BranchTrueFalseTotal
$req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_SIGNEDHEADERS) ? : 12 3 15
76 ? $req->uri->query_param( $X_AMZ_SIGNEDHEADERS )
77 : $req->headers->header_field_names
78 ;
79  
80 15 return sort { $a cmp $b } map { lc } @headers_to_sign
81 }
82  
83 # _augment_request:
84 # Append mandatory header fields
85  
86 sub _augment_request {
87 1 ▶ my ( $self, $request ) = @_;
Subroutine _augment_request: called CC=3 SCAR=11.4
88  
89 1 ▶ $request->header($X_AMZ_DATE => $self->_format_amz_date( $self->_req_timepiece($request) ))
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) 1 0 1
90 unless $request->header($X_AMZ_DATE);
91  
92 1 ▶ $request->header($X_AMZ_CONTENT_SHA256 => sha256_hex($request->content))
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256) 1 0 1
93 unless $request->header($X_AMZ_CONTENT_SHA256);
94  
95 1 return $request;
96 }
97  
98 # _augment_uri:
99 # Append mandatory uri parameters
100  
101 sub _augment_uri {
102 6 ▶ my ($self, $uri, $expires_in, $method) = @_;
Subroutine _augment_uri: called CC=8 SCAR=23.4
103  
104 6 ▶ my $request = HTTP::Request->new( $method || GET => $uri );
Condition50%1/2
$method || 'GET'
l!l
60
MC/DC0%0/1
$method || 'GET'
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
105  
106 6 ▶ $request->uri->query_param( $X_AMZ_DATE => $self->_format_amz_date( $self->_now ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) 6 0 6
107 unless $request->uri->query_param( $X_AMZ_DATE );
108  
109 6 ▶ $request->uri->query_param( $X_AMZ_ALGORITHM => $ALGORITHM )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_ALGORITHM) 6 0 6
110 unless $request->uri->query_param( $X_AMZ_ALGORITHM );
111  
112 6 ▶ $request->uri->query_param( $X_AMZ_CREDENTIAL => $self->_credential( $request ) )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CREDENTIAL) 6 0 6
113 unless $request->uri->query_param( $X_AMZ_CREDENTIAL );
114  
115 6 ▶ $request->uri->query_param( $X_AMZ_EXPIRES => $expires_in || $MAX_EXPIRES )
Branch: 1 branch
BranchTrueFalseTotal
unless $request->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_EXPIRES) 6 0 6
Condition33%1/3
$expires_in || $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
l!l&&r!l&&!r
600
MC/DC0%0/2
$expires_in || $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
$expires_in $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
Truth table33%1/3
A || B
A= $expires_inB= $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
ABresult
1X1
011
000
116 unless $request->uri->query_param( $X_AMZ_EXPIRES );
117 6 ▶ $request->uri->query_param( $X_AMZ_EXPIRES => $MAX_EXPIRES )
Branch: 1 branch
BranchTrueFalseTotal
if $request->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_EXPIRES) > $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES 0 6 6
118 if $request->uri->query_param( $X_AMZ_EXPIRES ) > $MAX_EXPIRES;
119  
120 6 $request->uri->query_param( $X_AMZ_SIGNEDHEADERS => 'host' );
121  
122 6 return $request;
123 }
124  
125 # _canonical_request:
126 # Construct the canonical request string from an HTTP::Request.
127  
128 sub _canonical_request {
129 7 ▶ my ( $self, $req ) = @_;
Subroutine _canonical_request: called CC=5 SCAR=16.1
130  
131 7 my $creq_method = $req->method;
132  
133 7 ▶ my ( $creq_canonical_uri, $creq_canonical_query_string ) =
Branch: 1 branch
BranchTrueFalseTotal
$req->uri =~ /([^?]*)\?(.*)$/ ? : 6 1 7
134 ( $req->uri =~ m@([^?]*)\?(.*)$@ )
135 ? ( $1, $2 )
136 : ( $req->uri, '' );
137 7 $creq_canonical_uri =~ s@^https?://[^/]*/?@/@;
138 7 $creq_canonical_uri = _simplify_uri( $creq_canonical_uri );
139 7 $creq_canonical_query_string = $self->_sort_query_string( $creq_canonical_query_string );
140  
141 # Ensure Host header is present as its required
142 7 ▶ if (!$req->header('host')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req->header("host")) 7 0 7
143 7 ▶ my $host = $req->uri->_port
Branch: 1 branch
BranchTrueFalseTotal
$req->uri->_port ? : 2 5 7
144 ? $req->uri->host_port
145 : $req->uri->host
146 ;
147 7 $req->header('Host' => $host);
148 }
149 7 ▶ my $creq_payload_hash = $req->header($X_AMZ_CONTENT_SHA256)
Condition100%2/2
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256) || 'UNSIGNED-PAYLOAD'
l!l
16
MC/DC100%1/1
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256) || 'UNSIGNED-PAYLOAD'
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256)
Truth table100%2/2
A
A= $req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256)
Aresult
11
00
150 # Signed uri doesn't have content
151 || 'UNSIGNED-PAYLOAD';
152  
153 # There's a bug in AMS4 which causes requests without x-amz-date set to be rejected
154 # so we always add one if its not present.
155 7 my $amz_date = $req->header($X_AMZ_DATE);
156 7 my @sorted_headers = _headers_to_sign( $req );
157 my $creq_canonical_headers = join '',
158 map {
159 7 sprintf "%s:%s\x0a",
160 lc,
161 13 join ',', sort {$a cmp $b } _trim_whitespace($req->header($_) )
162 }
163 @sorted_headers;
164 7 my $creq_signed_headers = $self->_signed_headers( $req );
165 7 my $creq = join "\x0a",
166 $creq_method, $creq_canonical_uri, $creq_canonical_query_string,
167 $creq_canonical_headers, $creq_signed_headers, $creq_payload_hash;
168  
169 7 return $creq;
170 }
171  
172 # _string_to_sign
173 # Construct the string to sign.
174  
175 sub _string_to_sign {
176 7 ▶ my ( $self, $req ) = @_;
Subroutine _string_to_sign: called CC=1 SCAR=0.0
177 7 my $dt = $self->_req_timepiece( $req );
178 7 my $creq = $self->_canonical_request($req);
179 7 my $sts_request_date = $self->_format_amz_date( $dt );
180 7 my $sts_credential_scope = join '/', $dt->strftime('%Y%m%d'), $self->{endpoint}, $self->{service}, 'aws4_request';
181 7 my $sts_creq_hash = sha256_hex( $creq );
182  
183 7 my $sts = join "\x0a", $ALGORITHM, $sts_request_date, $sts_credential_scope, $sts_creq_hash;
184 7 return $sts;
185 }
186  
187 # _authorization
188 # Construct the authorization string
189  
190 sub _signature {
191 7 ▶ my ( $self, $req ) = @_;
Subroutine _signature: called CC=1 SCAR=0.0
192  
193 7 my $dt = $self->_req_timepiece( $req );
194 7 my $sts = $self->_string_to_sign( $req );
195 7 my $k_date = hmac_sha256( $dt->strftime('%Y%m%d'), 'AWS4' . $self->{secret} );
196 7 my $k_region = hmac_sha256( $self->{endpoint}, $k_date );
197 7 my $k_service = hmac_sha256( $self->{service}, $k_region );
198 7 my $k_signing = hmac_sha256( 'aws4_request', $k_service );
199  
200 7 my $authz_signature = hmac_sha256_hex( $sts, $k_signing );
201 7 return $authz_signature;
202 }
203  
204 sub _credential {
205 7 ▶ my ( $self, $req ) = @_;
Subroutine _credential: called CC=1 SCAR=0.0
206  
207 7 my $dt = $self->_req_timepiece( $req );
208  
209 7 my $authz_credential = join '/', $self->{access_key_id}, $dt->strftime('%Y%m%d'), $self->{endpoint}, $self->{service}, 'aws4_request';
210 7 return $authz_credential;
211 }
212  
213 sub _signed_headers {
214 8 ▶ my ( $self, $req ) = @_;
Subroutine _signed_headers: called CC=1 SCAR=0.0
215  
216 8 my $authz_signed_headers = join ';', _headers_to_sign( $req );
217 8 return $authz_signed_headers;
218 }
219  
220 sub _authorization {
221 1 ▶ my ( $self, $req ) = @_;
Subroutine _authorization: called CC=1 SCAR=0.0
222  
223 1 my $authz_signature = $self->_signature( $req );
224 1 my $authz_credential = $self->_credential( $req );
225 1 my $authz_signed_headers = $self->_signed_headers( $req );
226  
227 1 my $authz = "$ALGORITHM Credential=$authz_credential,SignedHeaders=$authz_signed_headers,Signature=$authz_signature";
228 1 return $authz;
229  
230 }
231  
232 sub _simplify_uri {
233 7 ▶ my $orig_uri = shift;
Subroutine _simplify_uri: called CC=7 SCAR=20.2
234 7 my @parts = split /\//, $orig_uri;
235 7 my @simple_parts = ();
236 7 for my $part ( @parts ) {
237 18 ▶ if ( ! length $part || $part eq '.' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $part or $part eq '.') { } 6 12 18
elsif ($part eq '..') { } 0 12 12
Condition66%2/3
not length $part or $part eq "."
l!l&&r!l&&!r
6012
MC/DC50%1/2
not length $part or $part eq "."
not length $part $part eq "."
Truth table66%2/3
not(A) or B
A= not length $partB= $part eq "."
ABresult
1X1
011
000
238 } elsif ( $part eq '..' ) {
239 0 pop @simple_parts;
240 } else {
241 12 push @simple_parts, $part;
242 }
243 }
244 7 my $simple_uri = '/' . join '/', @simple_parts;
245 7 ▶ $simple_uri .= '/' if $orig_uri =~ m@/$@ && $simple_uri !~ m@/$@;
Branch: 1 branch
BranchTrueFalseTotal
if $orig_uri =~ m[/$] and not $simple_uri =~ m[/$] 0 7 7
Condition66%2/3
$orig_uri =~ m[/$] and not $simple_uri =~ m[/$]
!ll&&!rl&&r
610
MC/DC0%0/2
$orig_uri =~ m[/$] and not $simple_uri =~ m[/$]
$orig_uri =~ m[/$] not $simple_uri =~ m[/$]
Truth table66%2/3
A and not(B)
A= $orig_uri =~ m[/$]B= not $simple_uri =~ m[/$]
ABresult
0X0
100
111
246 7 return $simple_uri;
247 }
248 sub _sort_query_string {
249 13 ▶ my $self = shift;
Subroutine _sort_query_string: called CC=5 SCAR=16.2
250 13 ▶ return '' unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 1 12 13
251 12 my @params;
252 12 for my $param ( split /&/, $_[0] ) {
253 my ( $key, $value ) =
254 60 map { tr/+/ /; uri_escape( uri_unescape( $_ ) ) } # escape all non-unreserved chars
255 split /=/, $param;
256 60 ▶ push @params, [$key, (defined $value ? $value : '')];
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 60 0 60
257 #push @params, [$key, $value];
258 }
259 return join '&',
260 60 map { join '=', grep defined, @$_ }
261 12 ▶ sort { ( $a->[0] cmp $b->[0] ) || ( $a->[1] cmp $b->[1] ) }
Branch: 1 branch
BranchTrueFalseTotal
$Net::Amazon::S3::Signature::V4Implementation::a->[0] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[0] or $Net::Amazon::S3::Signature::V4Implementation::a->[1] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[1] 0 108 108
262 @params;
263 }
264 sub _trim_whitespace {
265 13 ▶ return map { my $str = $_; $str =~ s/^\s*//; $str =~ s/\s*$//; $str } @_;
Subroutine _trim_whitespace: called CC=1 SCAR=0.0
266 }
267 sub _str_to_timepiece {
268 21 ▶ my $date = shift;
Subroutine _str_to_timepiece: called CC=2 SCAR=8.4
269 21 ▶ if ( $date =~ m/^\d{8}T\d{6}Z$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^\d{8}T\d{6}Z$/) { } 21 0 21
270 # assume basic ISO 8601, as demanded by AWS
271 21 return Time::Piece->strptime($date, '%Y%m%dT%H%M%SZ');
272 } else {
273 # assume the format given in the AWS4 test suite
274 0 $date =~ s/^.{5}//; # remove weekday, as Amazon's test suite contains internally inconsistent dates
275 0 return Time::Piece->strptime($date, '%d %b %Y %H:%M:%S %Z');
276 }
277 }
278  
279 sub _format_amz_date {
280 14 ▶ my ($self, $dt) = @_;
Subroutine _format_amz_date: called CC=1 SCAR=0.0
281  
282 14 $dt->strftime('%Y%m%dT%H%M%SZ');
283 }
284  
285 sub _now {
286 7 ▶ return scalar Time::Piece->gmtime;
Subroutine _now: called CC=1 SCAR=0.0
287 }
288  
289 sub _req_timepiece {
290 22 ▶ my ($self, $req) = @_;
Subroutine _req_timepiece: called CC=4 SCAR=13.9
291 22 ▶ my $x_date = $req->header($X_AMZ_DATE) || $req->uri->query_param($X_AMZ_DATE);
Condition100%3/3
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) || $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
l!l&&r!l&&!r
3181
MC/DC100%2/2
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) || $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
Truth table100%3/3
A || B
A= $req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)B= $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
ABresult
1X1
011
000
292 22 ▶ my $date = $x_date || $req->header('Date');
Condition66%2/3
$x_date || $req->header('Date')
l!l&&r!l&&!r
2101
MC/DC50%1/2
$x_date || $req->header('Date')
$x_date $req->header('Date')
Truth table66%2/3
A || B
A= $x_dateB= $req->header('Date')
ABresult
1X1
011
000
293 22 ▶ if (!$date) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date) 1 21 22
294 # No date set by the caller so set one up
295 1 my $piece = $self->_now;
296 1 $req->date($piece->epoch);
297 1 return $piece
298 }
299 21 return _str_to_timepiece($date);
300 }
301  
302 99 1;
303  
304 __END__
305  
306 =pod
307  
308 =encoding UTF-8
309  
310 =head1 NAME
311  
312 Net::Amazon::S3::Signature::V4Implementation - Implements the Amazon Web Services signature version 4, AWS4-HMAC-SHA256 (copy of Net::Amazon::Signature::V4)
313  
314 =head1 VERSION
315  
316 version 0.99
317  
318 =head1 DESCRIPTION
319  
320 This package clones L<Net::Amazon::Signature::V4> 0.19 adding support for
321 signing URIs (GET request)
322  
323 Until https://github.com/Grinnz/Net-Amazon-Signature-V4/pull/5 will be merged
324 we have to maintain our clone.
325  
326 =head1 Net::Amazon::Signature::S4 AUTHORS
327  
328 Tim Nordenfur, C<< <tim at gurka.se> >>
329  
330 Maintained by Dan Book, C<< <dbook at cpan.org> >>
331  
332 =head2 sign( $request )
333  
334 Signs a request with your credentials by appending the Authorization header. $request should be an HTTP::Request. The signed request is returned.
335  
336 =head2 sign_uri( $uri, $expires_in?, $for_method? )
337  
338 Signs an uri with your credentials by appending the Authorization query parameters.
339  
340 C<< $expires_in >> integer value in range 1..604800 (1 second .. 7 days).
341  
342 C<< $expires_in >> default value is its maximum: 604800
343  
344 C<< $for_method >> HTTP method this uri should be signed for, default C<GET>
345  
346 The signed uri is returned.
347  
348 =head1 AUTHOR
349  
350 Branislav Zahradník <barney@cpan.org>
351  
352 =head1 COPYRIGHT AND LICENSE
353  
354 This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
355  
356 This is free software; you can redistribute it and/or modify it under
357 the same terms as the Perl 5 programming language system itself.
358  
359 =cut
« Shared/Examples/Net/Amazon/S3.pm ↑ Summary Net/Amazon/S3.pm »