| 1 | package Net::Amazon::AWSSign; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | $VERSION = '0.12'; | |||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Digest::SHA qw(hmac_sha256_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use URI::Escape; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | # General stuff | ||||||||||
| 16 | 1 | my $request; | |||||||||
| 17 | my $SOAPAction; | ||||||||||
| 18 | 1 | my $SOAPTimestamp; | |||||||||
| 19 | 1 | my @params; | |||||||||
| 20 | 1 | my $finalString=''; | |||||||||
| 21 | 1 | my $finalParams=''; | |||||||||
| 22 | 1 | my $AWSSignature=''; | |||||||||
| 23 | 1 | my $finalRequestURL; | |||||||||
| 24 | # My throwaway temp arrays | ||||||||||
| 25 | my @a; | ||||||||||
| 26 | 1 | my @b; | |||||||||
| 27 | # My throwaway temp variables | ||||||||||
| 28 | 1 | my $z; | |||||||||
| 29 | 1 | my $y; | |||||||||
| 30 | # For the secret generator | ||||||||||
| 31 | 1 | my $requestProtocol; | |||||||||
| 32 | 1 | my $requestHost; | |||||||||
| 33 | 1 | my $requestPath; | |||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | ## OO Subs | ||||||||||
| 37 | # Construct an object, called with your AWS key and secret | ||||||||||
| 38 | sub new | ||||||||||
| 39 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | 1 | my $class = shift; | |||||||||
| 41 | 1 | my $self = { | |||||||||
| 42 | _AWSKey => shift, | ||||||||||
| 43 | _AWSSecret => shift, | ||||||||||
| 44 | }; | ||||||||||
| 45 | 1 | bless $self, $class; | |||||||||
| 46 | 1 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub addRESTSecret { | ||||||||
|
Subroutine
addRESTSecret: called CC=8 SCAR=21.3
Pod
documented
| |||||||||||
| 51 | 1 | my ($self, $request)=@_; | |||||||||
| 52 | 1 | ▶ | unless ($request=~m/\&Timestamp=2/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | $request=$request . "&Timestamp=" . &getAWSTimeStamp(); | |||||||||
| 54 | } | ||||||||||
| 55 | 1 | $finalString="GET\n"; | |||||||||
| 56 | # Not sure why I thought this was important, but I probably had some rationale, so leaving it in here. | ||||||||||
| 57 | 1 | ▶ | if ($request=~m/^(http|https)?:\/\/(.*?)\/(.*?)\?/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 1 | $requestProtocol="$1"; | |||||||||
| 59 | 1 | $requestHost="$2"; | |||||||||
| 60 | 1 | $requestPath="/$3"; | |||||||||
| 61 | 1 | $finalString=$finalString . "$2\n/$3\n"; | |||||||||
| 62 | }else{ | ||||||||||
| 63 | 0 | return "ERROR: Cannot determine hostname and base path of request"; | |||||||||
| 64 | } | ||||||||||
| 65 | # Get just the parameters | ||||||||||
| 66 | 1 | @a=split(/\?/, $request, 2); | |||||||||
| 67 | # If we don't already have the subscription ID in the argument list, then add it. | ||||||||||
| 68 | 1 | ▶ | unless ($a[1]=~m/$self->{_AWSKey}/) { $a[1]="$a[1]&AWSAccessKeyId=$self->{_AWSKey}"; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | # Ditto for the SHA version and Signature version | ||||||||||
| 70 | 1 | ▶ | unless ($a[1]=~m/HmacSHA256/) { $a[1]="$a[1]&SignatureMethod=HmacSHA256"; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | ▶ | unless ($a[1]=~m/SignatureVersion/) { $a[1]="$a[1]&SignatureVersion=2"; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | 1 | @params=split(/\&/, $a[1]); | |||||||||
| 74 | # Sort and URI encode arguments, slam them into @b | ||||||||||
| 75 | 1 | undef @b; | |||||||||
| 76 | 1 | foreach $z (sort @params) { | |||||||||
| 77 | 9 | @a=split(/=/, $z, 2); | |||||||||
| 78 | # To allow for passing in url-encoded strings, we decode it then encode it | ||||||||||
| 79 | 9 | $a[1]=URI::Escape::uri_unescape( "$a[1]"); | |||||||||
| 80 | 9 | $a[1]=URI::Escape::uri_escape( "$a[1]", "^A-Za-z0-9\-_.~" ); | |||||||||
| 81 | 9 | $z=join('=', @a); | |||||||||
| 82 | 9 | push (@b, $z); | |||||||||
| 83 | } | ||||||||||
| 84 | 1 | $finalString="$finalString" . join ('&', @b); | |||||||||
| 85 | |||||||||||
| 86 | 1 | $AWSSignature=hmac_sha256_base64("$finalString", "$self->{_AWSSecret}"); | |||||||||
| 87 | # For some reason we usually need an equals sign appended. Check if required | ||||||||||
| 88 | 1 | ▶ | unless ($AWSSignature=~m/=$/) { $AWSSignature=$AWSSignature . "="; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | $AWSSignature=URI::Escape::uri_escape( "$AWSSignature", "^A-Za-z0-9\-_.~" ); | |||||||||
| 90 | 1 | return "$requestProtocol://" . $requestHost . $requestPath . "?" . join ('&', @b) . "&Signature=" . $AWSSignature; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 1 | ▶ | sub SOAPSig { | ||||||||
|
Subroutine
SOAPSig: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 94 | 1 | my ($self, $SOAPAction)=@_; | |||||||||
| 95 | 1 | $SOAPTimestamp=&getAWSTimeStamp(); | |||||||||
| 96 | 1 | $finalString=$SOAPAction . $SOAPTimestamp; | |||||||||
| 97 | 1 | $AWSSignature=hmac_sha256_base64("$finalString", "$self->{_AWSSecret}"); | |||||||||
| 98 | # For some reason we usually need an equals sign appended. Check if required | ||||||||||
| 99 | 1 | ▶ | unless ($AWSSignature=~m/=$/) { $AWSSignature=$AWSSignature . "="; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 1 | @a=("$SOAPTimestamp", "$AWSSignature"); | |||||||||
| 101 | 1 | return @a; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | ## Internal subs | ||||||||||
| 106 | 1 | ▶ | sub getAWSTimeStamp { | ||||||||
|
Subroutine
getAWSTimeStamp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 107 | # The Timestamp must be generated in a specific format. | ||||||||||
| 108 | return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.000Z", | ||||||||||
| 109 | 1 | ▶ | sub { ($_[5]+1900, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 110 | $_[4]+1, | ||||||||||
| 111 | $_[3], | ||||||||||
| 112 | $_[2], | ||||||||||
| 113 | $_[1], | ||||||||||
| 114 | $_[0]) | ||||||||||
| 115 | 1 | }->(gmtime(time))); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 1 | 1; | |||||||||
| 119 | |||||||||||
| 120 | __END__ | ||||||||||