File Coverage

blib/lib/Paws/Net/S3Signature.pm
Criterion Covered Total %
statement 22 23 95.6
branch 1 2 50.0
condition 2 2 100.0
subroutine 5 5 100.0
pod 0 2 0.0
total 30 34 88.2


line stmt bran cond sub pod time code
1             package Paws::Net::S3Signature;
2 8     8   35506 use Moose::Role;
  8         20  
  8         70  
3             requires 'service';
4              
5             sub BUILD {
6 63     63 0 91303 my $self = shift;
7              
8             # These calls are here so that when you construct
9             # the object the endpoint information and the _region_for_signature
10             # are calculated during construction. This is to avoid the fact that
11             # these attributes are lazy (because they depend on other attributes)
12             # and they don't get used until the first method is called, so if
13             # they are incorrect, they don't throw until the first method is called.
14             # It's much better to have them throw when $paws->service('...') is called
15             # as this is the point where the user had specified "incorrect" information,
16             # instead of the problem happening in the first method call.
17 63         2066 $self->endpoint;
18 63         1808 $self->_region_for_signature;
19             }
20              
21 8     8   42793 use Digest::SHA;
  8         5021  
  8         367  
22 8     8   787 use Net::Amazon::Signature::V4;
  8         17846  
  8         1362  
23              
24             sub sign {
25 4     4 0 21 my ($self, $request) = @_;
26              
27 4 50       38 if ($self->session_token) {
28 0         0 $request->header( 'X-Amz-Security-Token' => $self->session_token );
29             }
30              
31 4         214 my $hasher = Digest::SHA->new(256);
32 4   100     314 $hasher->add($request->content || q[]);
33 4         84 $request->header('X-Amz-Content-Sha256' => $hasher->hexdigest);
34              
35             # AWS prefers X-Amz-Date but Net::Amazon::Signature::V4 only handles Date in the headerpackage Paws::Net::S3Signature;
36 4         209 $request->header( 'Date' => $request->{'date'} );
37              
38 4         352 $request->header( 'Host' => $self->endpoint_host );
39              
40 4         247 my $sig = Net::Amazon::Signature::V4->new( $self->access_key, $self->secret_key, $self->region, $self->service );
41 4         118 my $signed_req = $sig->sign( $request );
42 4         252 return $signed_req;
43              
44             }
45             1;