| 1 | package Amazon::CloudFront::SignedURL; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Data::Validator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | our $VERSION = "0.03"; | |||||||||
| 12 | |||||||||||
| 13 | 3 | my $validator = Data::Validator->new( | |||||||||
| 14 | resource => { isa => 'Str', }, | ||||||||||
| 15 | expires => { | ||||||||||
| 16 | isa => 'Int', | ||||||||||
| 17 | xor => [qw(policy)], | ||||||||||
| 18 | }, | ||||||||||
| 19 | policy => { isa => 'Str', }, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | has private_key_string => ( | ||||||||||
| 23 | is => 'rw', | ||||||||||
| 24 | isa => 'Str', | ||||||||||
| 25 | required => 1, | ||||||||||
| 26 | 0 | ▶ | trigger => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 27 | 0 | $_[0]->clear_private_key; | |||||||||
| 28 | }, | ||||||||||
| 29 | 3 | ); | |||||||||
| 30 | |||||||||||
| 31 | 3 | has private_key => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | isa => 'Crypt::OpenSSL::RSA', | ||||||||||
| 34 | lazy_build => 1, | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | 3 | has key_pair_id => ( | |||||||||
| 38 | is => 'rw', | ||||||||||
| 39 | isa => 'Str', | ||||||||||
| 40 | required => 1, | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | sub _build_private_key { | ||||||||
|
Subroutine
_build_private_key: not called CC=2 SCAR=17.9
| |||||||||||
| 44 | 0 | my $private_key; | |||||||||
| 45 | 0 | eval { $private_key = Crypt::OpenSSL::RSA->new_private_key( $_[0]->private_key_string() ); }; | |||||||||
| 46 | 0 | ▶ | if ($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | croak "Private Key Error: Maybe your key is invalid. ($@)"; | |||||||||
| 48 | } | ||||||||||
| 49 | 0 | $private_key->use_sha1_hash(); | |||||||||
| 50 | 0 | return $private_key; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub generate { | ||||||||
|
Subroutine
generate: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 54 | 0 | my $self = shift; | |||||||||
| 55 | 0 | my $args = $validator->validate(@_); | |||||||||
| 56 | |||||||||||
| 57 | 0 | my $resource = $args->{resource}; | |||||||||
| 58 | 0 | ▶ | my $policy = exists $args->{policy} ? $args->{policy} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | my $expires = exists $args->{expires} ? $args->{expires} : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 0 | ▶ | if ($policy) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 0 | $policy =~ s/ //g; | |||||||||
| 63 | } | ||||||||||
| 64 | else { | ||||||||||
| 65 | 0 | $policy = sprintf( qq/{"Statement":[{"Resource":"%s","Condition":{"DateLessThan":{"AWS:EpochTime":%d}}}]}/, | |||||||||
| 66 | $resource, $expires ); | ||||||||||
| 67 | } | ||||||||||
| 68 | 0 | my $encoded_policy = $self->_encode_url_safe_base64($policy); | |||||||||
| 69 | 0 | my $signature = $self->_sign($policy); | |||||||||
| 70 | |||||||||||
| 71 | 0 | return $self->_create_url( $resource, $expires, $encoded_policy, $signature ); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub _encode_url_safe_base64 { | ||||||||
|
Subroutine
_encode_url_safe_base64: not called CC=1 SCAR=6.9
| |||||||||||
| 75 | 0 | my ( $self, $str ) = @_; | |||||||||
| 76 | 0 | my $encoded = encode_base64($str); | |||||||||
| 77 | 0 | $encoded =~ s/\r|\n//g; | |||||||||
| 78 | 0 | $encoded =~ tr|+=/|-_~|; | |||||||||
| 79 | 0 | return $encoded; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub _sign { | ||||||||
|
Subroutine
_sign: not called CC=1 SCAR=6.9
| |||||||||||
| 83 | 0 | my ( $self, $str ) = @_; | |||||||||
| 84 | 0 | my $signature = $self->_encode_url_safe_base64( $self->private_key->sign($str) ); | |||||||||
| 85 | 0 | return $signature; | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _create_url { | ||||||||
|
Subroutine
_create_url: not called CC=2 SCAR=17.9
| |||||||||||
| 89 | 0 | my ( $self, $resource, $expires, $policy, $signature ) = @_; | |||||||||
| 90 | 0 | my $uri = URI->new($resource); | |||||||||
| 91 | 0 | ▶ | if ($expires) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 0 | $uri->query_form( | |||||||||
| 93 | 'Expires' => $expires, | ||||||||||
| 94 | 'Signature' => $signature, | ||||||||||
| 95 | 'Key-Pair-Id' => $self->key_pair_id(), | ||||||||||
| 96 | ); | ||||||||||
| 97 | } | ||||||||||
| 98 | else { | ||||||||||
| 99 | 0 | $uri->query_form( | |||||||||
| 100 | 'Policy' => $policy, | ||||||||||
| 101 | 'Signature' => $signature, | ||||||||||
| 102 | 'Key-Pair-Id' => $self->key_pair_id(), | ||||||||||
| 103 | ); | ||||||||||
| 104 | } | ||||||||||
| 105 | 0 | return $uri->as_string; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 3 | ▶ | no Mouse; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 110 | |||||||||||
| 111 | 3 | 1; | |||||||||
| 112 | __END__ | ||||||||||