| 1 | package Net::Amazon::S3::Signature::V4Implementation; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Implements the Amazon Web Services signature version 4, AWS4-HMAC-SHA256 (copy of Net::Amazon::Signature::V4) | ||||||||||||||||||||||||||||||||
| 3 | 99 | $Net::Amazon::S3::Signature::V4Implementation::VERSION = '0.992'; | |||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 99 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 99 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 99 | ▶ | use Digest::SHA qw/sha256_hex hmac_sha256 hmac_sha256_hex/; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 99 | ▶ | use Time::Piece (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 99 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 99 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 99 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 99 | our $ALGORITHM = 'AWS4-HMAC-SHA256'; | |||||||||||||||||||||||||||||||
| 15 | 99 | our $MAX_EXPIRES = 604800; # Max, 7 days | |||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 99 | our $X_AMZ_ALGORITHM = 'X-Amz-Algorithm'; | |||||||||||||||||||||||||||||||
| 18 | 99 | our $X_AMZ_CONTENT_SHA256 = 'X-Amz-Content-Sha256'; | |||||||||||||||||||||||||||||||
| 19 | 99 | our $X_AMZ_CREDENTIAL = 'X-Amz-Credential'; | |||||||||||||||||||||||||||||||
| 20 | 99 | our $X_AMZ_DATE = 'X-Amz-Date'; | |||||||||||||||||||||||||||||||
| 21 | 99 | our $X_AMZ_EXPIRES = 'X-Amz-Expires'; | |||||||||||||||||||||||||||||||
| 22 | 99 | our $X_AMZ_SIGNEDHEADERS = 'X-Amz-SignedHeaders'; | |||||||||||||||||||||||||||||||
| 23 | 99 | our $X_AMZ_SIGNATURE = 'X-Amz-Signature'; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 28 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||
| 29 | 7 | my ( $access_key_id, $secret, $endpoint, $service ) = @_; | |||||||||||||||||||||||||||||||
| 30 | 7 | my $self = { | |||||||||||||||||||||||||||||||
| 31 | access_key_id => $access_key_id, | ||||||||||||||||||||||||||||||||
| 32 | secret => $secret, | ||||||||||||||||||||||||||||||||
| 33 | endpoint => $endpoint, | ||||||||||||||||||||||||||||||||
| 34 | service => $service, | ||||||||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||||||
| 36 | 7 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 37 | 7 | return $self; | |||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub sign { | ||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 42 | 1 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 1 | $request = $self->_augment_request( $request ); | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 1 | my $authz = $self->_authorization( $request ); | |||||||||||||||||||||||||||||||
| 47 | 1 | $request->header( Authorization => $authz ); | |||||||||||||||||||||||||||||||
| 48 | 1 | return $request; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 6 | ▶ | sub sign_uri { | ||||||||||||||||||||||||||||||
|
Subroutine
sign_uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 6 | my ( $self, $uri, $expires_in, $for_method ) = @_; | |||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 6 | my $request = $self->_augment_uri( $uri, $expires_in, $for_method ); | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 6 | my $signature = $self->_signature( $request ); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 6 | $uri = $request->uri; | |||||||||||||||||||||||||||||||
| 60 | 6 | my $query = $uri->query; | |||||||||||||||||||||||||||||||
| 61 | 6 | $uri->query( undef ); | |||||||||||||||||||||||||||||||
| 62 | 6 | $uri = $uri . '?' . $self->_sort_query_string( $query ); | |||||||||||||||||||||||||||||||
| 63 | 6 | $uri .= "&$X_AMZ_SIGNATURE=$signature"; | |||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 6 | return $uri; | |||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | # _headers_to_sign: | ||||||||||||||||||||||||||||||||
| 69 | # Return the sorted lower case headers as required by the generation of canonical headers | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 15 | ▶ | sub _headers_to_sign { | ||||||||||||||||||||||||||||||
|
Subroutine
_headers_to_sign: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 72 | 15 | my $req = shift; | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 15 | ▶ | my @headers_to_sign = $req->uri->query_param( $X_AMZ_SIGNEDHEADERS ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | ? $req->uri->query_param( $X_AMZ_SIGNEDHEADERS ) | ||||||||||||||||||||||||||||||||
| 76 | : $req->headers->header_field_names | ||||||||||||||||||||||||||||||||
| 77 | ; | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 15 | return sort { $a cmp $b } map { lc } @headers_to_sign | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # _augment_request: | ||||||||||||||||||||||||||||||||
| 83 | # Append mandatory header fields | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | sub _augment_request { | ||||||||||||||||||||||||||||||
|
Subroutine
_augment_request: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||
| 86 | 1 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | $request->header($X_AMZ_DATE => $self->_format_amz_date( $self->_req_timepiece($request) )) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | unless $request->header($X_AMZ_DATE); | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | $request->header($X_AMZ_CONTENT_SHA256 => sha256_hex($request->content)) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | unless $request->header($X_AMZ_CONTENT_SHA256); | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 1 | return $request; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | # _augment_uri: | ||||||||||||||||||||||||||||||||
| 98 | # Append mandatory uri parameters | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub _augment_uri { | ||||||||||||||||||||||||||||||
|
Subroutine
_augment_uri: called CC=8 SCAR=23.4
| |||||||||||||||||||||||||||||||||
| 101 | 6 | my ($self, $uri, $expires_in, $method) = @_; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 6 | ▶ | my $request = HTTP::Request->new( $method || GET => $uri ); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$method || 'GET'
MC/DC0%0/1
$method || 'GET'
$method
Truth table50%1/2
A
A= $method
| |||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 6 | ▶ | $request->uri->query_param( $X_AMZ_DATE => $self->_format_amz_date( $self->_now ) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | unless $request->uri->query_param( $X_AMZ_DATE ); | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | $request->uri->query_param( $X_AMZ_ALGORITHM => $ALGORITHM ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | unless $request->uri->query_param( $X_AMZ_ALGORITHM ); | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 6 | ▶ | $request->uri->query_param( $X_AMZ_CREDENTIAL => $self->_credential( $request ) ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 112 | unless $request->uri->query_param( $X_AMZ_CREDENTIAL ); | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | $request->uri->query_param( $X_AMZ_EXPIRES => $expires_in || $MAX_EXPIRES ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$expires_in || $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
MC/DC0%0/2
$expires_in || $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
$expires_in
$Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
Truth table33%1/3
A || B
A= $expires_inB= $Net::Amazon::S3::Signature::V4Implementation::MAX_EXPIRES
| |||||||||||||||||||||||||||||||||
| 115 | unless $request->uri->query_param( $X_AMZ_EXPIRES ); | ||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | $request->uri->query_param( $X_AMZ_EXPIRES => $MAX_EXPIRES ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | if $request->uri->query_param( $X_AMZ_EXPIRES ) > $MAX_EXPIRES; | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 6 | $request->uri->query_param( $X_AMZ_SIGNEDHEADERS => 'host' ); | |||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 6 | return $request; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # _canonical_request: | ||||||||||||||||||||||||||||||||
| 125 | # Construct the canonical request string from an HTTP::Request. | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 7 | ▶ | sub _canonical_request { | ||||||||||||||||||||||||||||||
|
Subroutine
_canonical_request: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 128 | 7 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 7 | my $creq_method = $req->method; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 7 | ▶ | my ( $creq_canonical_uri, $creq_canonical_query_string ) = | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | ( $req->uri =~ m@([^?]*)\?(.*)$@ ) | ||||||||||||||||||||||||||||||||
| 134 | ? ( $1, $2 ) | ||||||||||||||||||||||||||||||||
| 135 | : ( $req->uri, '' ); | ||||||||||||||||||||||||||||||||
| 136 | 7 | $creq_canonical_uri =~ s@^https?://[^/]*/?@/@; | |||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | # Documentation says "do not normalize URI paths for requests to Amazon S3" | ||||||||||||||||||||||||||||||||
| 139 | # https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | #$creq_canonical_uri = _simplify_uri( $creq_canonical_uri ); | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 7 | $creq_canonical_query_string = $self->_sort_query_string( $creq_canonical_query_string ); | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | # Ensure Host header is present as its required | ||||||||||||||||||||||||||||||||
| 146 | 7 | ▶ | if (!$req->header('host')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | 7 | ▶ | my $host = $req->uri->_port | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | ? $req->uri->host_port | ||||||||||||||||||||||||||||||||
| 149 | : $req->uri->host | ||||||||||||||||||||||||||||||||
| 150 | ; | ||||||||||||||||||||||||||||||||
| 151 | 7 | $req->header('Host' => $host); | |||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||
| 153 | 7 | ▶ | my $creq_payload_hash = $req->header($X_AMZ_CONTENT_SHA256) | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256) || 'UNSIGNED-PAYLOAD'
MC/DC100%1/1
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256) || 'UNSIGNED-PAYLOAD'
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256)
Truth table100%2/2
A
A= $req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_CONTENT_SHA256)
| |||||||||||||||||||||||||||||||||
| 154 | # Signed uri doesn't have content | ||||||||||||||||||||||||||||||||
| 155 | || 'UNSIGNED-PAYLOAD'; | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | # There's a bug in AMS4 which causes requests without x-amz-date set to be rejected | ||||||||||||||||||||||||||||||||
| 158 | # so we always add one if its not present. | ||||||||||||||||||||||||||||||||
| 159 | 7 | my $amz_date = $req->header($X_AMZ_DATE); | |||||||||||||||||||||||||||||||
| 160 | 7 | my @sorted_headers = _headers_to_sign( $req ); | |||||||||||||||||||||||||||||||
| 161 | my $creq_canonical_headers = join '', | ||||||||||||||||||||||||||||||||
| 162 | map { | ||||||||||||||||||||||||||||||||
| 163 | 7 | sprintf "%s:%s\x0a", | |||||||||||||||||||||||||||||||
| 164 | lc, | ||||||||||||||||||||||||||||||||
| 165 | 0 | join ',', sort {$a cmp $b } _trim_whitespace($req->header($_) ) | |||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||
| 167 | @sorted_headers; | ||||||||||||||||||||||||||||||||
| 168 | 7 | my $creq_signed_headers = $self->_signed_headers( $req ); | |||||||||||||||||||||||||||||||
| 169 | 7 | my $creq = join "\x0a", | |||||||||||||||||||||||||||||||
| 170 | $creq_method, $creq_canonical_uri, $creq_canonical_query_string, | ||||||||||||||||||||||||||||||||
| 171 | $creq_canonical_headers, $creq_signed_headers, $creq_payload_hash; | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 7 | return $creq; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | # _string_to_sign | ||||||||||||||||||||||||||||||||
| 177 | # Construct the string to sign. | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 7 | ▶ | sub _string_to_sign { | ||||||||||||||||||||||||||||||
|
Subroutine
_string_to_sign: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 180 | 7 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 181 | 7 | my $dt = $self->_req_timepiece( $req ); | |||||||||||||||||||||||||||||||
| 182 | 7 | my $creq = $self->_canonical_request($req); | |||||||||||||||||||||||||||||||
| 183 | 7 | my $sts_request_date = $self->_format_amz_date( $dt ); | |||||||||||||||||||||||||||||||
| 184 | 7 | my $sts_credential_scope = join '/', $dt->strftime('%Y%m%d'), $self->{endpoint}, $self->{service}, 'aws4_request'; | |||||||||||||||||||||||||||||||
| 185 | 7 | my $sts_creq_hash = sha256_hex( $creq ); | |||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | 7 | my $sts = join "\x0a", $ALGORITHM, $sts_request_date, $sts_credential_scope, $sts_creq_hash; | |||||||||||||||||||||||||||||||
| 188 | 7 | return $sts; | |||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | # _authorization | ||||||||||||||||||||||||||||||||
| 192 | # Construct the authorization string | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | 7 | ▶ | sub _signature { | ||||||||||||||||||||||||||||||
|
Subroutine
_signature: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 195 | 7 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 7 | my $dt = $self->_req_timepiece( $req ); | |||||||||||||||||||||||||||||||
| 198 | 7 | my $sts = $self->_string_to_sign( $req ); | |||||||||||||||||||||||||||||||
| 199 | 7 | my $k_date = hmac_sha256( $dt->strftime('%Y%m%d'), 'AWS4' . $self->{secret} ); | |||||||||||||||||||||||||||||||
| 200 | 7 | my $k_region = hmac_sha256( $self->{endpoint}, $k_date ); | |||||||||||||||||||||||||||||||
| 201 | 7 | my $k_service = hmac_sha256( $self->{service}, $k_region ); | |||||||||||||||||||||||||||||||
| 202 | 7 | my $k_signing = hmac_sha256( 'aws4_request', $k_service ); | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 7 | my $authz_signature = hmac_sha256_hex( $sts, $k_signing ); | |||||||||||||||||||||||||||||||
| 205 | 7 | return $authz_signature; | |||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 7 | ▶ | sub _credential { | ||||||||||||||||||||||||||||||
|
Subroutine
_credential: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 209 | 7 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | 7 | my $dt = $self->_req_timepiece( $req ); | |||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 7 | my $authz_credential = join '/', $self->{access_key_id}, $dt->strftime('%Y%m%d'), $self->{endpoint}, $self->{service}, 'aws4_request'; | |||||||||||||||||||||||||||||||
| 214 | 7 | return $authz_credential; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 8 | ▶ | sub _signed_headers { | ||||||||||||||||||||||||||||||
|
Subroutine
_signed_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 218 | 8 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 8 | my $authz_signed_headers = join ';', _headers_to_sign( $req ); | |||||||||||||||||||||||||||||||
| 221 | 8 | return $authz_signed_headers; | |||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | 1 | ▶ | sub _authorization { | ||||||||||||||||||||||||||||||
|
Subroutine
_authorization: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 225 | 1 | my ( $self, $req ) = @_; | |||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | 1 | my $authz_signature = $self->_signature( $req ); | |||||||||||||||||||||||||||||||
| 228 | 1 | my $authz_credential = $self->_credential( $req ); | |||||||||||||||||||||||||||||||
| 229 | 1 | my $authz_signed_headers = $self->_signed_headers( $req ); | |||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | 1 | my $authz = "$ALGORITHM Credential=$authz_credential,SignedHeaders=$authz_signed_headers,Signature=$authz_signature"; | |||||||||||||||||||||||||||||||
| 232 | 1 | return $authz; | |||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 0 | ▶ | sub _simplify_uri { | ||||||||||||||||||||||||||||||
|
Subroutine
_simplify_uri: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||
| 237 | 0 | my $orig_uri = shift; | |||||||||||||||||||||||||||||||
| 238 | 0 | my @parts = split /\//, $orig_uri; | |||||||||||||||||||||||||||||||
| 239 | 0 | my @simple_parts = (); | |||||||||||||||||||||||||||||||
| 240 | 0 | for my $part ( @parts ) { | |||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if ( ! length $part || $part eq '.' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
not length $part or $part eq "."
MC/DC0%0/2
not length $part or $part eq "."
not length $part
$part eq "."
Truth table0%0/3
not(A) or B
A= not length $partB= $part eq "."
| |||||||||||||||||||||||||||||||||
| 242 | } elsif ( $part eq '..' ) { | ||||||||||||||||||||||||||||||||
| 243 | 0 | pop @simple_parts; | |||||||||||||||||||||||||||||||
| 244 | } else { | ||||||||||||||||||||||||||||||||
| 245 | 0 | push @simple_parts, $part; | |||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | 0 | my $simple_uri = '/' . join '/', @simple_parts; | |||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | $simple_uri .= '/' if $orig_uri =~ m@/$@ && $simple_uri !~ m@/$@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$orig_uri =~ m[/$] and not $simple_uri =~ m[/$]
MC/DC0%0/2
$orig_uri =~ m[/$] and not $simple_uri =~ m[/$]
$orig_uri =~ m[/$]
not $simple_uri =~ m[/$]
Truth table0%0/3
A and not(B)
A= $orig_uri =~ m[/$]B= not $simple_uri =~ m[/$]
| |||||||||||||||||||||||||||||||||
| 250 | 0 | return $simple_uri; | |||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||
| 252 | 13 | ▶ | sub _sort_query_string { | ||||||||||||||||||||||||||||||
|
Subroutine
_sort_query_string: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||
| 253 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 254 | 13 | ▶ | return '' unless $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 255 | 12 | my @params; | |||||||||||||||||||||||||||||||
| 256 | 12 | for my $param ( split /&/, $_[0] ) { | |||||||||||||||||||||||||||||||
| 257 | my ( $key, $value ) = | ||||||||||||||||||||||||||||||||
| 258 | 60 | map { tr/+/ /; uri_escape( uri_unescape( $_ ) ) } # escape all non-unreserved chars | |||||||||||||||||||||||||||||||
| 259 | split /=/, $param; | ||||||||||||||||||||||||||||||||
| 260 | 60 | ▶ | push @params, [$key, (defined $value ? $value : '')]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | #push @params, [$key, $value]; | ||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||
| 263 | return join '&', | ||||||||||||||||||||||||||||||||
| 264 | 60 | map { join '=', grep defined, @$_ } | |||||||||||||||||||||||||||||||
| 265 | 12 | ▶ | sort { ( $a->[0] cmp $b->[0] ) || ( $a->[1] cmp $b->[1] ) } | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$Net::Amazon::S3::Signature::V4Implementation::a->[0] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[0] || $Net::Amazon::S3::Signature::V4Implementation::a->[1] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[1]
MC/DC0%0/2
$Net::Amazon::S3::Signature::V4Implementation::a->[0] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[0] || $Net::Amazon::S3::Signature::V4Implementation::a->[1] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[1]
$Net::Amazon::S3::Signature::V4Implementation::a->[0] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[0]
$Net::Amazon::S3::Signature::V4Implementation::a->[1] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[1]
Truth table33%1/3
A || B
A= $Net::Amazon::S3::Signature::V4Implementation::a->[0] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[0]B= $Net::Amazon::S3::Signature::V4Implementation::a->[1] cmp $Net::Amazon::S3::Signature::V4Implementation::b->[1]
| |||||||||||||||||||||||||||||||||
| 266 | @params; | ||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||
| 268 | 13 | ▶ | sub _trim_whitespace { | ||||||||||||||||||||||||||||||
|
Subroutine
_trim_whitespace: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 269 | 13 | return map { my $str = $_; $str =~ s/^\s*//; $str =~ s/\s*$//; $str } @_; | |||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||
| 271 | 21 | ▶ | sub _str_to_timepiece { | ||||||||||||||||||||||||||||||
|
Subroutine
_str_to_timepiece: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||||
| 272 | 21 | my $date = shift; | |||||||||||||||||||||||||||||||
| 273 | 21 | ▶ | if ( $date =~ m/^\d{8}T\d{6}Z$/ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 274 | # assume basic ISO 8601, as demanded by AWS | ||||||||||||||||||||||||||||||||
| 275 | 21 | return Time::Piece->strptime($date, '%Y%m%dT%H%M%SZ'); | |||||||||||||||||||||||||||||||
| 276 | } else { | ||||||||||||||||||||||||||||||||
| 277 | # assume the format given in the AWS4 test suite | ||||||||||||||||||||||||||||||||
| 278 | 0 | $date =~ s/^.{5}//; # remove weekday, as Amazon's test suite contains internally inconsistent dates | |||||||||||||||||||||||||||||||
| 279 | 0 | return Time::Piece->strptime($date, '%d %b %Y %H:%M:%S %Z'); | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | 14 | ▶ | sub _format_amz_date { | ||||||||||||||||||||||||||||||
|
Subroutine
_format_amz_date: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 284 | 14 | my ($self, $dt) = @_; | |||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | 14 | $dt->strftime('%Y%m%dT%H%M%SZ'); | |||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | 7 | ▶ | sub _now { | ||||||||||||||||||||||||||||||
|
Subroutine
_now: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 290 | 7 | return scalar Time::Piece->gmtime; | |||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | 22 | ▶ | sub _req_timepiece { | ||||||||||||||||||||||||||||||
|
Subroutine
_req_timepiece: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 294 | 22 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||
| 295 | 22 | ▶ | my $x_date = $req->header($X_AMZ_DATE) || $req->uri->query_param($X_AMZ_DATE); | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) || $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
MC/DC100%2/2
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE) || $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
$req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
$req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
Truth table100%3/3
A || B
A= $req->header($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)B= $req->uri->query_param($Net::Amazon::S3::Signature::V4Implementation::X_AMZ_DATE)
| |||||||||||||||||||||||||||||||||
| 296 | 22 | ▶ | my $date = $x_date || $req->header('Date'); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$x_date || $req->header('Date')
MC/DC50%1/2
$x_date || $req->header('Date')
$x_date
$req->header('Date')
Truth table66%2/3
A || B
A= $x_dateB= $req->header('Date')
| |||||||||||||||||||||||||||||||||
| 297 | 22 | ▶ | if (!$date) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 298 | # No date set by the caller so set one up | ||||||||||||||||||||||||||||||||
| 299 | 1 | my $piece = $self->_now; | |||||||||||||||||||||||||||||||
| 300 | 1 | $req->date($piece->epoch); | |||||||||||||||||||||||||||||||
| 301 | 1 | return $piece | |||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||
| 303 | 21 | return _str_to_timepiece($date); | |||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||
| 306 | 99 | 1; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | __END__ | ||||||||||||||||||||||||||||||||