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 CC 19 SCAR 22.4
CC
19
Cov
90%
_canonical_string
17.5
_encode
2.0
_add_auth_header
2.0
CRAP 9.4
« Net/Amazon/S3/Signature/V4Implementation.pm ↑ Summary Net/Amazon/S3/Operation/Bucket/Location/Response.pm »
1 package Net::Amazon::S3::Signature::V2;
2 # ABSTRACT: V2 signatures
3 99 $Net::Amazon::S3::Signature::V2::VERSION = '0.992';
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 1 ▶sub enforce_use_virtual_host {
Subroutine enforce_use_virtual_host: called CC=1 SCAR=0.0
Pod undocumented
20 1 0;
21 }
22  
23 209 ▶sub sign_request {
Subroutine sign_request: called CC=1 SCAR=0.0
Pod documented
24 209 my ($self, $request) = @_;
25  
26 209 $self->_add_auth_header( $request );
27 }
28  
29 2 ▶sub sign_uri {
Subroutine sign_uri: called CC=1 SCAR=0.0
Pod documented
30 2 my ($self, $request, $expires) = @_;
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 209 ▶sub _add_auth_header {
Subroutine _add_auth_header: called CC=2 SCAR=6.9
47 209 my ( $self, $request ) = @_;
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 211 ▶sub _canonical_string {
Subroutine _canonical_string: called CC=17 SCAR=28.6
64 211 my ( $self, $request, $expires ) = @_;
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/) 353 221 574
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 211 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=7.1
125 211 my ( $self, $str, $urlencode ) = @_;
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 0 ▶sub _urlencode {
Subroutine _urlencode: not called CC=1 SCAR=6.9
137 0 my ( $self, $unencoded ) = @_;
138 0 return uri_escape_utf8( $unencoded, '^A-Za-z0-9_-' );
139 }
140  
141 353 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
142 353 my ( $self, $value ) = @_;
143 353 $value =~ s/^\s+//;
144 353 $value =~ s/\s+$//;
145 353 return $value;
146 }
147  
148 99 1;
149  
150 __END__
« Net/Amazon/S3/Signature/V4Implementation.pm ↑ Summary Net/Amazon/S3/Operation/Bucket/Location/Response.pm »