Net/Amazon/S3/Signature/V2.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 75.0% 15 / 20 Conditioncond 87.5% 14 / 16 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 93.7% 15 / 16 Podpod 66.6% 2 / 3 totaltotal 89.3% 134 / 150 SCAR 29.6
CC
19
Cov
90%
_canonical_string
17.5
_encode
2.0
_add_auth_header
2.0
CRAP 19.3
« Net/Amazon/S3/Response.pm ↑ Summary Net/Amazon/S3/Client/Bucket.pm »
1 package Net::Amazon::S3::Signature::V2;
2 # ABSTRACT: V2 signatures
3 99 $Net::Amazon::S3::Signature::V2::VERSION = '0.99';
4 99 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 99 ▶use URI::Escape qw( uri_escape_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 99 ▶use HTTP::Date qw[ time2str ];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 99 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 99 ▶use Net::Amazon::S3::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 99 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 99 extends 'Net::Amazon::S3::Signature';
16  
17 99 my $AMAZON_HEADER_PREFIX = 'x-amz-';
18  
19 sub enforce_use_virtual_host {
20 1 ▶ 0;
Subroutine enforce_use_virtual_host: called CC=1 SCAR=0.0
Pod undocumented
21 }
22  
23 sub sign_request {
24 209 ▶ my ($self, $request) = @_;
Subroutine sign_request: called CC=1 SCAR=0.0
Pod documented
25  
26 209 $self->_add_auth_header( $request );
27 }
28  
29 sub sign_uri {
30 2 ▶ my ($self, $request, $expires) = @_;
Subroutine sign_uri: called CC=1 SCAR=0.0
Pod documented
31  
32 2 my $aws_access_key_id = $self->http_request->s3->aws_access_key_id;
33  
34 2 my $canonical_string = $self->_canonical_string( $request, $expires );
35 2 my $encoded_canonical = $self->_encode( $canonical_string );
36  
37 2 my $uri = URI->new( $request->uri );
38  
39 2 $uri->query_param( AWSAccessKeyId => $aws_access_key_id );
40 2 $uri->query_param( Expires => $expires );
41 2 $uri->query_param( Signature => $encoded_canonical );
42  
43 2 $uri->as_string;
44 }
45  
46 sub _add_auth_header {
47 209 ▶ my ( $self, $request ) = @_;
Subroutine _add_auth_header: called CC=2 SCAR=6.9
48  
49 209 my $aws_access_key_id = $self->http_request->s3->aws_access_key_id;
50 209 my $aws_secret_access_key = $self->http_request->s3->aws_secret_access_key;
51  
52 209 ▶ if ( not $request->headers->header('Date') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->headers->header("Date")) 209 0 209
53 209 $request->header( Date => time2str(time) );
54 }
55  
56 209 $self->_append_authorization_headers ($request);
57  
58 209 my $canonical_string = $self->_canonical_string( $request );
59 209 my $encoded_canonical = $self->_encode( $canonical_string );
60 209 $request->header( Authorization => "AWS $aws_access_key_id:$encoded_canonical" );
61 }
62  
63 sub _canonical_string {
64 211 ▶ my ( $self, $request, $expires ) = @_;
Subroutine _canonical_string: called CC=17 SCAR=28.6
65 211 my $method = $request->method;
66 211 my $path = $self->http_request->path;
67  
68 211 my %interesting_headers = ();
69 211 for my $key ($request->headers->header_field_names) {
70 574 my $lk = lc $key;
71 574 ▶ if ( $lk eq 'content-md5'
Branch: 1 branch
BranchTrueFalseTotal
if ($lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/) 272 221 493
Condition100%9/9
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/
l!l&&r!l&&!r
29063221
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date"
l!l&&r!l&&!r
81209284
$lk eq "content-md5" or $lk eq "content-type"
l!l&&r!l&&!r
1863493
MC/DC100%4/4
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$AMAZON_HEADER_PREFIX/
$lk eq "content-md5" $lk eq "content-type" $lk eq "date" $lk =~ /^$AMAZON_HEADER_PREFIX/
Truth table100%5/5
A or B or C or D
A= $lk eq "content-md5"B= $lk eq "content-type"C= $lk eq "date"D= $lk =~ /^$AMAZON_HEADER_PREFIX/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
72 or $lk eq 'content-type'
73 or $lk eq 'date'
74 or $lk =~ /^$AMAZON_HEADER_PREFIX/ )
75 {
76 353 $interesting_headers{$lk} = $self->_trim( $request->header( $lk ) );
77 }
78 }
79  
80 # these keys get empty strings if they don't exist
81 211 ▶ $interesting_headers{'content-type'} ||= '';
Condition100%2/2
$interesting_headers{"content-type"} ||= ""
l!l
63148
MC/DC100%1/1
$interesting_headers{"content-type"} ||= ""
$interesting_headers{"content-type"}
Truth table100%2/2
A
A= $interesting_headers{"content-type"}
Aresult
11
00
82 211 ▶ $interesting_headers{'content-md5'} ||= '';
Condition100%2/2
$interesting_headers{"content-md5"} ||= ""
l!l
18193
MC/DC100%1/1
$interesting_headers{"content-md5"} ||= ""
$interesting_headers{"content-md5"}
Truth table100%2/2
A
A= $interesting_headers{"content-md5"}
Aresult
11
00
83  
84 # just in case someone used this. it's not necessary in this lib.
85 $interesting_headers{'date'} = ''
86 211 ▶ if $interesting_headers{Net::Amazon::S3::Constants->HEADER_DATE};
Branch: 1 branch
BranchTrueFalseTotal
if $interesting_headers{"Net::Amazon::S3::Constants"->HEADER_DATE} 0 211 211
87  
88 # if you're using expires for query string auth, then it trumps date
89 # (and x-amz-date)
90 211 ▶ $interesting_headers{'date'} = $expires if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 2 209 211
91  
92 211 my $buf = "$method\n";
93 211 foreach my $key ( sort keys %interesting_headers ) {
94 696 ▶ if ( $key =~ /^$AMAZON_HEADER_PREFIX/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$AMAZON_HEADER_PREFIX/) { } 63 633 696
95 63 $buf .= "$key:$interesting_headers{$key}\n";
96 } else {
97 633 $buf .= "$interesting_headers{$key}\n";
98 }
99 }
100  
101 # don't include anything after the first ? in the resource...
102 211 $path =~ /^([^?]*)/;
103 211 $buf .= "/$1";
104  
105 # ...unless there any parameters we're interested in...
106 211 ▶ if ( $path =~ /[&?](acl|torrent|location|uploads|delete)($|=|&)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ /[&?](acl|torrent|location|uploads|delete)($|=|&)/) { } 56 155 211
elsif (my(%query_params) = 'URI'->new($path)->query_form) { } 12 143 155
107 56 $buf .= "?$1";
108 } elsif ( my %query_params = URI->new($path)->query_form ){
109 #see if the remaining parsed query string provides us with any query string or upload id
110 12 ▶ if($query_params{partNumber} && $query_params{uploadId}){
Branch: 2 branches
BranchTrueFalseTotal
if ($query_params{'partNumber'} and $query_params{'uploadId'}) { } 0 12 12
elsif ($query_params{'uploadId'}) { } 0 12 12
Condition33%1/3
$query_params{"partNumber"} and $query_params{"uploadId"}
!ll&&!rl&&r
1200
MC/DC0%0/2
$query_params{"partNumber"} and $query_params{"uploadId"}
$query_params{"partNumber"} $query_params{"uploadId"}
Truth table33%1/3
A and B
A= $query_params{"partNumber"}B= $query_params{"uploadId"}
ABresult
0X0
100
111
111 #re-evaluate query string, the order of the params is important for request signing, so we can't depend on URI to do the right thing
112 0 $buf .= sprintf("?partNumber=%s&uploadId=%s", $query_params{partNumber}, $query_params{uploadId});
113 }
114 elsif($query_params{uploadId}){
115 0 $buf .= sprintf("?uploadId=%s",$query_params{uploadId});
116 }
117 }
118  
119 211 return $buf;
120 }
121  
122 # finds the hmac-sha1 hash of the canonical string and the aws secret access key and then
123 # base64 encodes the result (optionally urlencoding after that).
124 sub _encode {
125 211 ▶ my ( $self, $str, $urlencode ) = @_;
Subroutine _encode: called CC=2 SCAR=7.1
126 211 my $hmac = Digest::HMAC_SHA1->new($self->http_request->s3->aws_secret_access_key);
127 211 $hmac->add($str);
128 211 my $b64 = encode_base64( $hmac->digest, '' );
129 211 ▶ if ($urlencode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($urlencode) { } 0 211 211
130 0 return $self->_urlencode($b64);
131 } else {
132 211 return $b64;
133 }
134 }
135  
136 sub _urlencode {
137 0 ▶ my ( $self, $unencoded ) = @_;
Subroutine _urlencode: not called CC=1 SCAR=6.9
138 0 return uri_escape_utf8( $unencoded, '^A-Za-z0-9_-' );
139 }
140  
141 sub _trim {
142 353 ▶ my ( $self, $value ) = @_;
Subroutine _trim: called CC=1 SCAR=0.0
143 353 $value =~ s/^\s+//;
144 353 $value =~ s/\s+$//;
145 353 return $value;
146 }
147  
148 99 1;
149  
150 __END__
151  
152 =pod
153  
154 =encoding UTF-8
155  
156 =head1 NAME
157  
158 Net::Amazon::S3::Signature::V2 - V2 signatures
159  
160 =head1 VERSION
161  
162 version 0.99
163  
164 =head1 AUTHOR
165  
166 Branislav Zahradník <barney@cpan.org>
167  
168 =head1 COPYRIGHT AND LICENSE
169  
170 This software is copyright (c) 2021 by Amazon Digital Services, Leon Brocard, Brad Fitzpatrick, Pedro Figueiredo, Rusty Conover, Branislav Zahradník.
171  
172 This is free software; you can redistribute it and/or modify it under
173 the same terms as the Perl 5 programming language system itself.
174  
175 =cut
« Net/Amazon/S3/Response.pm ↑ Summary Net/Amazon/S3/Client/Bucket.pm »