Thin/Signer/V2.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 90.9% 20 / 22 Conditioncond 100% 13 / 13 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 94.7% 125 / 132 CC 20 SCAR 23.4
CC
20
Cov
97%
string_to_sign
17.0
sign
3.0
new
2.1
CRAP 10.3
↑ Summary Thin.pm »
1 package Amazon::S3::Thin::Signer::V2;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Digest::HMAC_SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 my $AMAZON_HEADER_PREFIX = 'x-amz-';
10  
11 # reserved subresources such as acl or torrent
12 5 our @ordered_subresources = qw(
13 acl delete lifecycle location logging notification partNumber policy
14 requestPayment torrent uploadId uploads versionId versioning versions
15 website
16 );
17  
18 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod undocumented
19 16 my ($class, $credentials, $host) = @_;
20 16 ▶ if (ref($credentials) ne 'Amazon::S3::Thin::Credentials') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $credentials ne "Amazon::S3::Thin::Credentials") 0 16 16
21 0 croak "credentials object is not given."
22 }
23 16 my $self = {
24 credentials => $credentials,
25 host => $host,
26 };
27 16 bless $self, $class;
28 }
29  
30 sub sign
31 3 ▶{
Subroutine sign: called CC=3 SCAR=11.0
Pod undocumented
32 3 my ($self, $request) = @_;
33 3 ▶ $request->header(Date => HTTP::Date::time2str(time)) unless $request->header('Date');
Branch: 1 branch
BranchTrueFalseTotal
unless $request->header("Date") 1 2 3
34 3 ▶ if (defined $self->{credentials}->session_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"credentials"}->session_token) 1 2 3
35 1 $request->header('X-Amz-Security-Token', $self->{credentials}->session_token);
36 }
37 3 my $host = $request->uri->host;
38 3 my $bucket = substr($host, 0, length($host) - length($self->{host}) - 1);
39 3 my $path = $bucket . $request->uri->path;
40 3 my $signature = $self->calculate_signature( $request->method, $path, $request->headers );
41 $request->header(
42 Authorization => sprintf("AWS %s:%s"
43 , $self->{credentials}->access_key_id,
44 3 , $signature));
45 }
46  
47 # generate a canonical string for the given parameters. expires is optional and is
48 # only used by query string authentication.
49 14 ▶sub calculate_signature {
Subroutine calculate_signature: called CC=1 SCAR=0.0
Pod undocumented
50 14 my ($self, $method, $path, $headers, $expires) = @_;
51  
52 14 my $string_to_sign = $self->string_to_sign( $method, $path, $headers, $expires );
53  
54 14 my $hmac = Digest::HMAC_SHA1->new($self->{credentials}->secret_access_key);
55 14 $hmac->add($string_to_sign);
56 14 return MIME::Base64::encode_base64($hmac->digest, '');
57 }
58  
59 24 ▶sub string_to_sign {
Subroutine string_to_sign: called CC=17 SCAR=28.3
Pod undocumented
60 24 my ($self, $method, $path, $headers, $expires) = @_;
61  
62 24 my %interesting_headers = ();
63 24 ▶ while (my ($key, $value) = each %$headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$headers) 61 24 85
64 61 my $lk = lc $key;
65 61 ▶ 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/) 41 20 61
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
301120
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date"
l!l&&r!l&&!r
62431
$lk eq "content-md5" or $lk eq "content-type"
l!l&&r!l&&!r
2455
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
66 or $lk eq 'content-type'
67 or $lk eq 'date'
68 or $lk =~ /^$AMAZON_HEADER_PREFIX/)
69 {
70 41 $interesting_headers{$lk} = $self->_trim($value);
71 }
72 }
73  
74 # these keys get empty strings if they don't exist
75 24 ▶ $interesting_headers{'content-type'} ||= '';
Condition100%2/2
$interesting_headers{"content-type"} ||= ""
l!l
420
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
76 24 ▶ $interesting_headers{'content-md5'} ||= '';
Condition100%2/2
$interesting_headers{"content-md5"} ||= ""
l!l
222
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
77  
78 # x-amz-date becomes date if it exists
79 $interesting_headers{'date'} = delete $interesting_headers{'x-amz-date'}
80 24 ▶ if exists $interesting_headers{'x-amz-date'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $interesting_headers{"x-amz-date"} 2 22 24
81  
82 # if you're using expires for query string auth, then it trumps date
83 # (and x-amz-date)
84 24 ▶ $interesting_headers{'date'} = $expires if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 0 24 24
85  
86 24 my $string_to_sign = "$method\n";
87 24 foreach my $key (sort keys %interesting_headers) {
88 81 ▶ if ($key =~ /^$AMAZON_HEADER_PREFIX/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$AMAZON_HEADER_PREFIX/) { } 9 72 81
89 9 $string_to_sign .= "$key:$interesting_headers{$key}\n";
90 }
91 else {
92 72 $string_to_sign .= "$interesting_headers{$key}\n";
93 }
94 }
95  
96 24 $path =~ /^([^?]*)(.*)/;
97 24 $string_to_sign .= "/$1";
98 24 ▶ if (! $2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($2) 16 8 24
99 16 return $string_to_sign;
100 }
101  
102 8 my $query_string = $2;
103  
104 8 my %interesting_subresources = map { $_ => '' } @ordered_subresources;
105  
106 8 foreach my $query (split /[&?]/, $query_string) {
107 28 $query =~ /^([^=]+)/;
108 28 ▶ if (exists $interesting_subresources{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $interesting_subresources{$1}) 12 16 28
109 12 $interesting_subresources{$1} = $query;
110 }
111 }
112 8 my $join_char = '?';
113 8 foreach my $name (@ordered_subresources) {
114 128 ▶ if ($interesting_subresources{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interesting_subresources{$name}) 12 116 128
115 12 $string_to_sign .= $join_char . $name;
116 12 $join_char = '&';
117 }
118 }
119 8 return $string_to_sign;
120 }
121  
122 41 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
123 41 my ($self, $value) = @_;
124 41 $value =~ s/^\s+//;
125 41 $value =~ s/\s+$//;
126 41 return $value;
127 }
128  
129 5 1;
↑ Summary Thin.pm »