| 1 | 3 | ▶ | use v5.32; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 3 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use open qw(:std :utf8); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | no feature qw(indirect); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 3 | ▶ | use feature qw(signatures); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 3 | ▶ | no warnings qw(experimental::signatures); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | package Net::Amazon::SignatureVersion4; | ||||||||||||||
| 10 | 3 | $Net::Amazon::SignatureVersion4::VERSION = '0.007'; | |||||||||||||
| 11 | 3 | ▶ | use MooseX::App qw(Config); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 3 | ▶ | use POSIX qw(strftime); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 3 | ▶ | use Digest::SHA qw(sha256_hex hmac_sha256_hex hmac_sha256 hmac_sha256_base64); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 3 | ▶ | use URI::Encode; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 3 | ▶ | use HTTP::Date; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | # ABSTRACT: Signs requests using Amazon's Signature Version 4. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | |||||||||||||||
| 20 | 3 | option 'Access_Key_Id' => ( | |||||||||||||
| 21 | is => 'rw', | ||||||||||||||
| 22 | isa => 'Str', | ||||||||||||||
| 23 | reader => 'get_Access_Key_ID', | ||||||||||||||
| 24 | predicate => 'has_Access_Key_ID', | ||||||||||||||
| 25 | writer => 'set_Access_Key_ID', | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 3 | option 'Secret_Access_Key' => ( | |||||||||||||
| 29 | is => 'rw', | ||||||||||||||
| 30 | isa => 'Str', | ||||||||||||||
| 31 | reader => 'get_Secret_Access_Key', | ||||||||||||||
| 32 | predicate => 'has_Secret_Access_Key', | ||||||||||||||
| 33 | writer => 'set_Secret_Access_Key', | ||||||||||||||
| 34 | ); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 3 | option 'region' => ( | |||||||||||||
| 37 | is => 'rw', | ||||||||||||||
| 38 | isa => 'Str', | ||||||||||||||
| 39 | writer => 'set_region', | ||||||||||||||
| 40 | reader => 'get_region', | ||||||||||||||
| 41 | default => 'us-east-1', | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 3 | option 'request' => ( | |||||||||||||
| 45 | is => 'rw', | ||||||||||||||
| 46 | isa => 'Object', | ||||||||||||||
| 47 | writer => 'set_request', | ||||||||||||||
| 48 | reader => 'get_request', | ||||||||||||||
| 49 | ); | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 3 | option 'service' => ( | |||||||||||||
| 52 | is => 'rw', | ||||||||||||||
| 53 | isa => 'Str', | ||||||||||||||
| 54 | writer => 'set_service', | ||||||||||||||
| 55 | reader => 'get_service', | ||||||||||||||
| 56 | ); | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 3 | option 'time' => ( | |||||||||||||
| 59 | is => 'rw', | ||||||||||||||
| 60 | isa => 'Str', | ||||||||||||||
| 61 | writer => 'set_time', | ||||||||||||||
| 62 | reader => 'get_time', | ||||||||||||||
| 63 | ); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 3 | option 'date_stamp' => ( | |||||||||||||
| 66 | is => 'rw', | ||||||||||||||
| 67 | isa => 'Str', | ||||||||||||||
| 68 | writer => 'set_date_stamp', | ||||||||||||||
| 69 | reader => 'get_date_stamp', | ||||||||||||||
| 70 | ); | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 3 | option 'signed_headers' => ( | |||||||||||||
| 73 | is => 'rw', | ||||||||||||||
| 74 | isa => 'Str', | ||||||||||||||
| 75 | writer => 'set_signed_headers', | ||||||||||||||
| 76 | reader => 'get_signed_headers', | ||||||||||||||
| 77 | ); | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 30 | ▶ | sub get_authorized_request{ | ||||||||||||
|
Subroutine
get_authorized_request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 80 | |||||||||||||||
| 81 | 30 | my $self=shift; | |||||||||||||
| 82 | 30 | my $request=$self->get_request(); | |||||||||||||
| 83 | 30 | $request->header( Authorization => $self->get_authorization() ); | |||||||||||||
| 84 | 30 | return $request | |||||||||||||
| 85 | |||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 60 | ▶ | sub get_authorization{ | ||||||||||||
|
Subroutine
get_authorization: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 89 | 60 | my $self=shift; | |||||||||||||
| 90 | 60 | my %dk=$self->get_derived_signing_key(); | |||||||||||||
| 91 | 60 | my $sts=$self->get_string_to_sign(); | |||||||||||||
| 92 | 60 | $sts=~tr/\r//d; | |||||||||||||
| 93 | 60 | my $signature=hmac_sha256_hex($sts,$dk{'kSigning'}); | |||||||||||||
| 94 | 60 | return "AWS4-HMAC-SHA256 Credential=".$self->get_Access_Key_ID()."/".$self->get_date_stamp()."/".$self->get_region()."/".$self->get_service()."/aws4_request, SignedHeaders=".$self->get_signed_headers().", Signature=$signature"; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 61 | ▶ | sub get_derived_signing_key{ | ||||||||||||
|
Subroutine
get_derived_signing_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 98 | 61 | my $self=shift; | |||||||||||||
| 99 | 61 | $self->get_canonical_request(); # This is a hack to get the date set before using it to derive the signing key. | |||||||||||||
| 100 | 61 | my %rv=(); | |||||||||||||
| 101 | 61 | $rv{'kSecret'}="AWS4".$self->get_Secret_Access_Key(); | |||||||||||||
| 102 | #say("kSecret: ".unpack('H*',$rv{'kSecret'})); | ||||||||||||||
| 103 | 61 | $rv{'kDate'}=hmac_sha256($self->get_date_stamp(),$rv{'kSecret'}); | |||||||||||||
| 104 | #say("kDate: ".unpack('H*',$rv{'kDate'})); | ||||||||||||||
| 105 | 61 | $rv{'kRegion'}=hmac_sha256($self->get_region(),$rv{'kDate'}); | |||||||||||||
| 106 | #say("kRegion: ".unpack('H*',$rv{'kRegion'})); | ||||||||||||||
| 107 | 61 | $rv{'kService'}=hmac_sha256($self->get_service(),$rv{'kRegion'}); | |||||||||||||
| 108 | #say("kService: ".unpack('H*',$rv{'kService'})); | ||||||||||||||
| 109 | 61 | $rv{'kSigning'}=hmac_sha256("aws4_request",$rv{'kService'}); | |||||||||||||
| 110 | #say("kSigning: ".unpack('H*',$rv{'kSigning'})); | ||||||||||||||
| 111 | 61 | return %rv; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | 90 | ▶ | sub get_string_to_sign{ | ||||||||||||
|
Subroutine
get_string_to_sign: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 114 | 90 | my $self=shift; | |||||||||||||
| 115 | |||||||||||||||
| 116 | 90 | my $creq=$self->get_canonical_request(); | |||||||||||||
| 117 | 90 | $creq=~tr/\r//d; | |||||||||||||
| 118 | 90 | my $StringToSign="AWS4-HMAC-SHA256\r\n". | |||||||||||||
| 119 | $self->get_time()."\r\n". | ||||||||||||||
| 120 | $self->get_date_stamp()."/". | ||||||||||||||
| 121 | $self->get_region()."/". | ||||||||||||||
| 122 | $self->get_service()."/aws4_request\r\n". | ||||||||||||||
| 123 | sha256_hex($creq); | ||||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 181 | ▶ | sub get_canonical_request{ | ||||||||||||
|
Subroutine
get_canonical_request: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||
| 127 | 181 | my $self=shift; | |||||||||||||
| 128 | 3 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 129 | |||||||||||||||
| 130 | 181 | my $method; | |||||||||||||
| 131 | 181 | my $full_uri=""; | |||||||||||||
| 132 | 181 | my $version; | |||||||||||||
| 133 | 181 | my $canonical_query_string=""; | |||||||||||||
| 134 | 181 | my %headers=(); | |||||||||||||
| 135 | |||||||||||||||
| 136 | 181 | foreach my $name ( $self->get_request()->header_field_names() ){ | |||||||||||||
| 137 | 406 | my @value=$self->get_request()->header($name); | |||||||||||||
| 138 | 406 | ▶ | next unless (defined $name & defined $value[0]); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 406 | ▶ | if (lc($name) eq 'date'){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | 181 | my $time=str2time($value[0]); | |||||||||||||
| 141 | 181 | $self->set_date_stamp(strftime("%Y%m%d", gmtime($time))); | |||||||||||||
| 142 | 181 | $self->set_time(strftime("%Y%m%dT%H%M%SZ",gmtime($time))); | |||||||||||||
| 143 | |||||||||||||||
| 144 | } | ||||||||||||||
| 145 | 406 | foreach my $value (@value){ | |||||||||||||
| 146 | 436 | local $/ = " "; | |||||||||||||
| 147 | 436 | chomp($value); | |||||||||||||
| 148 | 436 | ▶ | if (defined $headers{lc($name)}){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | 30 | push @{$headers{lc($name)}}, $value; | |||||||||||||
| 150 | }else{ | ||||||||||||||
| 151 | 406 | $headers{lc($name)}=[$value ]; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | } | ||||||||||||||
| 154 | } | ||||||||||||||
| 155 | 181 | $full_uri=$self->get_request()->uri(); | |||||||||||||
| 156 | 181 | $full_uri =~ s@^(http|https)://.*?/@/@; | |||||||||||||
| 157 | 181 | ▶ | if ($full_uri=~m/(.*?)\?(.*)/){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 60 | $full_uri=$1; | |||||||||||||
| 159 | 60 | $canonical_query_string=$2; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | 181 | my @canonical_query_list; | |||||||||||||
| 162 | 181 | ▶ | if ( defined $canonical_query_string){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 181 | ▶ | if ($canonical_query_string=~m/(.*?)\s.*/){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 0 | $canonical_query_string=$1 | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | 181 | @canonical_query_list=split(/\&/,$canonical_query_string); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | 181 | $canonical_query_string=""; | |||||||||||||
| 169 | 181 | foreach my $param (sort @canonical_query_list){ | |||||||||||||
| 170 | 84 | (my $name, my $value)=split(/=/, $param); | |||||||||||||
| 171 | 84 | ▶ | $name="" unless (defined $name); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 84 | ▶ | $value="" unless (defined $value); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 84 | $canonical_query_string=$canonical_query_string._encode($name)."="._encode($value)."&"; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | 181 | ▶ | $canonical_query_string=substr($canonical_query_string, 0, -1) unless ($canonical_query_string eq ""); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 176 | 181 | $full_uri=~tr/\///s; | |||||||||||||
| 177 | 181 | my $ends_in_slash=0; | |||||||||||||
| 178 | 181 | ▶ | if ($full_uri=~m/\w\/$/){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 179 | 6 | $ends_in_slash=1; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | 181 | my @uri_source=split /\//, $full_uri; | |||||||||||||
| 182 | 181 | my @uri_stack; | |||||||||||||
| 183 | 181 | foreach my $path_component (@uri_source){ | |||||||||||||
| 184 | 135 | ▶ | if ($path_component =~ m/^\.$/){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 185 | 12 | sleep 0; | |||||||||||||
| 186 | }elsif ($path_component =~ m/^..$/){ | ||||||||||||||
| 187 | 18 | pop @uri_stack; | |||||||||||||
| 188 | }else{ | ||||||||||||||
| 189 | 105 | push @uri_stack, $path_component; | |||||||||||||
| 190 | } | ||||||||||||||
| 191 | } | ||||||||||||||
| 192 | 181 | $full_uri="/"; | |||||||||||||
| 193 | 181 | foreach my $path_component (@uri_stack){ | |||||||||||||
| 194 | 87 | $full_uri=$full_uri."$path_component/"; | |||||||||||||
| 195 | } | ||||||||||||||
| 196 | 181 | $full_uri=~tr/\///s; | |||||||||||||
| 197 | 181 | ▶ | chop $full_uri unless ( $full_uri eq "/" ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 198 | 181 | ▶ | if ($ends_in_slash){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 199 | 6 | $full_uri=$full_uri."/"; | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | 181 | my $CanonicalHeaders=""; | |||||||||||||
| 202 | 181 | my $SignedHeaders=""; | |||||||||||||
| 203 | 181 | foreach my $header ( sort keys %headers ){ | |||||||||||||
| 204 | 406 | $CanonicalHeaders=$CanonicalHeaders.lc($header).':'; | |||||||||||||
| 205 | 406 | foreach my $element(sort @{$headers{$header}}){ | |||||||||||||
| 206 | 436 | $CanonicalHeaders=$CanonicalHeaders.($element).","; | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | 406 | $CanonicalHeaders=substr($CanonicalHeaders, 0, -1); | |||||||||||||
| 209 | 406 | $CanonicalHeaders=$CanonicalHeaders."\r\n"; | |||||||||||||
| 210 | 406 | $SignedHeaders=$SignedHeaders.lc($header).";"; | |||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | 181 | $SignedHeaders=substr($SignedHeaders, 0, -1); | |||||||||||||
| 214 | 181 | $self->set_signed_headers($SignedHeaders); | |||||||||||||
| 215 | 181 | my $CanonicalRequest = | |||||||||||||
| 216 | $self->get_request()->method() . "\r\n" . | ||||||||||||||
| 217 | $full_uri . "\r\n" . | ||||||||||||||
| 218 | $canonical_query_string . "\r\n" . | ||||||||||||||
| 219 | $CanonicalHeaders . "\r\n" . | ||||||||||||||
| 220 | $SignedHeaders . "\r\n" . | ||||||||||||||
| 221 | sha256_hex($self->get_request()->content()); | ||||||||||||||
| 222 | 181 | return $CanonicalRequest; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | 168 | ▶ | sub _encode{ | ||||||||||||
|
Subroutine
_encode: called CC=1 SCAR=0.0
| |||||||||||||||
| 226 | #This method is used to add some additional encodings that are not enforced by the URI::Encode module. AWS expects these. | ||||||||||||||
| 227 | 168 | my $encoder = URI::Encode->new({ double_encode => 0 }); | |||||||||||||
| 228 | 168 | my $rv=shift; | |||||||||||||
| 229 | # %20=%2F%2C%3F%3E%3C%60%22%3B%3A%5C%7C%5D%5B%7B%7D&%40%23%24%25%5E= | ||||||||||||||
| 230 | # + =/ , ? %3E%3C%60%22; : %5C%7C] [ %7B%7D&@ # $ %25%5E= | ||||||||||||||
| 231 | 168 | $rv=$encoder->encode($rv); | |||||||||||||
| 232 | 168 | $rv=~s/\+/\%20/g; | |||||||||||||
| 233 | 168 | $rv=~s/\//\%2F/g; | |||||||||||||
| 234 | 168 | $rv=~s/\,/\%2C/g; | |||||||||||||
| 235 | 168 | $rv=~s/\?/\%3F/g; | |||||||||||||
| 236 | 168 | $rv=~s/\;/\%3B/g; | |||||||||||||
| 237 | 168 | $rv=~s/\:/\%3A/g; | |||||||||||||
| 238 | 168 | $rv=~s/\]/\%5D/g; | |||||||||||||
| 239 | 168 | $rv=~s/\[/\%5B/g; | |||||||||||||
| 240 | 168 | $rv=~s/\@/\%40/g; | |||||||||||||
| 241 | 168 | $rv=~s/\#/\%23/g; | |||||||||||||
| 242 | 168 | $rv=~s/\$/\%24/g; | |||||||||||||
| 243 | # $rv=~s///g; | ||||||||||||||
| 244 | 168 | return $rv; | |||||||||||||
| 245 | } | ||||||||||||||
| 246 | 3 | 1; | |||||||||||||
| 247 | |||||||||||||||
| 248 | __END__ | ||||||||||||||