| 1 | package Amazon::S3::SignedURLGenerator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Digest::HMAC_SHA1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | my $class = ref($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my %args = scalar(@_) % 2 ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | $args{aws_access_key_id} or croak 'aws_access_key_id is required'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | $args{aws_secret_access_key} or croak 'aws_secret_access_key is required'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | $args{prefix} ||= 'https://s3.amazonaws.com'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"prefix"} ||= "https://s3.amazonaws.com"
MC/DC0%0/1
$args{"prefix"} ||= "https://s3.amazonaws.com"
$args{"prefix"}
Truth table0%0/2
A
A= $args{"prefix"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | $args{expires} ||= 3600; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"expires"} ||= 3600
MC/DC0%0/1
$args{"expires"} ||= 3600
$args{"expires"}
Truth table0%0/2
A
A= $args{"expires"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | $args{prefix} =~ s/\/$//; # remove last / | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | return bless \%args, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub generate_url { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_url: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my ($self, $method, $path, $headers) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | $path =~ s/^\///; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$headers ||= {}
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $expires = $headers->{expires} || (time() + $self->{expires}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$headers->{'expires'} || time + $self->{'expires'}
MC/DC0%0/2
$headers->{'expires'} || time + $self->{'expires'}
$headers->{'expires'}
time + $self->{'expires'}
Truth table0%0/3
A || B
A= $headers->{'expires'}B= time + $self->{'expires'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $x_path = $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($self->{prefix} =~ '//(.*)\.s3') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $x_path = $1 . '/' . $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $canonical_string = __canonical_string($method, $x_path, $headers, $expires); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $encoded_canonical = __encode($self->{aws_secret_access_key}, $canonical_string, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if (index($path, '?') == -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | return "$self->{prefix}/$path?Signature=$encoded_canonical&Expires=$expires&AWSAccessKeyId=$self->{aws_access_key_id}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | return "$self->{prefix}/$path&Signature=$encoded_canonical&Expires=$expires&AWSAccessKeyId=$self->{aws_access_key_id}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | our $AMAZON_HEADER_PREFIX = 'x-amz-'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | our $METADATA_PREFIX = 'x-amz-meta-'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub __trim { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__trim: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $value =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $value =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # generate a canonical string for the given parameters. expires is optional and is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # only used by query string authentication. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub __canonical_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__canonical_string: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ($method, $path, $headers, $expires) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my %interesting_headers = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | while (my ($key, $value) = each %$headers) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $lk = lc $key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$Amazon::S3::SignedURLGenerator::AMAZON_HEADER_PREFIX/
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date"
$lk eq "content-md5" or $lk eq "content-type"
MC/DC0%0/4
$lk eq "content-md5" or $lk eq "content-type" or $lk eq "date" or $lk =~ /^$Amazon::S3::SignedURLGenerator::AMAZON_HEADER_PREFIX/
$lk eq "content-md5"
$lk eq "content-type"
$lk eq "date"
$lk =~ /^$Amazon::S3::SignedURLGenerator::AMAZON_HEADER_PREFIX/
Truth table0%0/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::S3::SignedURLGenerator::AMAZON_HEADER_PREFIX/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | $lk eq 'content-md5' or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $lk eq 'content-type' or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | $lk eq 'date' or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | $lk =~ /^$AMAZON_HEADER_PREFIX/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $interesting_headers{$lk} = __trim($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # these keys get empty strings if they don't exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $interesting_headers{'content-type'} ||= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$interesting_headers{"content-type"} ||= ""
MC/DC0%0/1
$interesting_headers{"content-type"} ||= ""
$interesting_headers{"content-type"}
Truth table0%0/2
A
A= $interesting_headers{"content-type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $interesting_headers{'content-md5'} ||= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$interesting_headers{"content-md5"} ||= ""
MC/DC0%0/1
$interesting_headers{"content-md5"} ||= ""
$interesting_headers{"content-md5"}
Truth table0%0/2
A
A= $interesting_headers{"content-md5"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # just in case someone used this. it's not necessary in this lib. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $interesting_headers{'date'} = '' if $interesting_headers{'x-amz-date'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # if you're using expires for query string auth, then it trumps date | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # (and x-amz-date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $interesting_headers{'date'} = $expires if $expires; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $buf = "$method\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | foreach my $key (sort keys %interesting_headers) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($key =~ /^$AMAZON_HEADER_PREFIX/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $buf .= "$key:$interesting_headers{$key}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $buf .= "$interesting_headers{$key}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # don't include anything after the first ? in the resource... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $path =~ /^([^?]*)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $buf .= "/$1"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # ...unless there is an acl or torrent parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($path =~ /[&?]acl($|=|&)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $buf .= '?acl'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } elsif ($path =~ /[&?]torrent($|=|&)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $buf .= '?torrent'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } elsif ($path =~ /[&?]logging($|=|&)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $buf .= '?logging'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return $buf; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # finds the hmac-sha1 hash of the canonical string and the aws secret access key and then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # base64 encodes the result (optionally urlencoding after that). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub __encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__encode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my ($aws_secret_access_key, $str, $urlencode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my $hmac = Digest::HMAC_SHA1->new($aws_secret_access_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $hmac->add($str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $b64 = encode_base64($hmac->digest, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($urlencode) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | return __urlencode($b64); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | return $b64; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub __urlencode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__urlencode: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my ($unencoded) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | return uri_escape($unencoded, '^A-Za-z0-9_-'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||