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   5174 use Moose::Role;
  8         19  
  8         58  
3             requires 'service';
4              
5             sub BUILD {
6 63     63 0 74799 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         1675 $self->endpoint;
18 63         1569 $self->_region_for_signature;
19             }
20              
21 8     8   37351 use Digest::SHA;
  8         4307  
  8         441  
22 8     8   531 use Net::Amazon::Signature::V4;
  8         16721  
  8         1204  
23              
24             sub sign {
25 4     4 0 13 my ($self, $request) = @_;
26              
27 4 50       27 if ($self->session_token) {
28 0         0 $request->header( 'X-Amz-Security-Token' => $self->session_token );
29             }
30              
31 4         114 my $hasher = Digest::SHA->new(256);
32 4   100     170 $hasher->add($request->content || q[]);
33 4         62 $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         145 $request->header( 'Date' => $request->{'date'} );
37              
38 4         251 $request->header( 'Host' => $self->endpoint_host );
39              
40 4         139 my $sig = Net::Amazon::Signature::V4->new( $self->access_key, $self->secret_key, $self->region, $self->service );
41 4         79 my $signed_req = $sig->sign( $request );
42 4         142 return $signed_req;
43              
44             }
45             1;