| 1 | package Net::AWS::SES::Signature4; | ||
| 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 | ▶ | use Carp ('croak'); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use MIME::Base64; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Time::Piece; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use HTTP::Headers; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Digest::HMAC_SHA1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Net::AWS::SES::Response; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use AWS::Signature4; | ||
| 13 | use HTTP::Request::Common; | ||
| 14 | |||
| 15 | |||
| 16 | require Exporter; | ||
| 17 | |||
| 18 | our @ISA = qw(Exporter); | ||
| 19 | |||
| 20 | # Items to export into callers namespace by default. Note: do not export | ||
| 21 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 22 | # Do not simply export all your public functions/methods/constants. | ||
| 23 | |||
| 24 | # This allows declaration use Net::AWS::SES::Signature4 ':all'; | ||
| 25 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 26 | # will save memory. | ||
| 27 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 28 | |||
| 29 | ) ] ); | ||
| 30 | |||
| 31 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 32 | |||
| 33 | our @EXPORT = qw( | ||
| 34 | |||
| 35 | ); | ||
| 36 | |||
| 37 | our $VERSION = '0.07'; | ||
| 38 | |||
| 39 | |||
| 40 | # Preloaded methods go here. | ||
| 41 | |||
| 42 | sub __timestamp { | ||
| 43 | return localtime->datetime; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub __signature { | ||
| 47 | my $self = shift; | ||
| 48 | my ($date) = @_; | ||
| 49 | unless ($date) { | ||
| 50 | croak "signature(): usage error"; | ||
| 51 | } | ||
| 52 | my $hmac = Digest::HMAC_SHA1->new( $self->secret_key ); | ||
| 53 | $hmac->add($date); | ||
| 54 | return encode_base64( $hmac->digest ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub __user_agent { | ||
| 58 | my $self = shift; | ||
| 59 | my $ua = LWP::UserAgent->new( | ||
| 60 | agent => sprintf( "%s/%s", __PACKAGE__, $VERSION ), | ||
| 61 | default_headers => $self->__header | ||
| 62 | ); | ||
| 63 | return $ua; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub __signer { | ||
| 67 | my $self = shift; | ||
| 68 | my $signer = AWS::Signature4->new(-access_key => $self->access_key, -secret_key => $self->secret_key); | ||
| 69 | return $signer; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub __header { | ||
| 73 | my $self = shift; | ||
| 74 | my $h = HTTP::Headers->new; | ||
| 75 | $h->date(time); | ||
| 76 | $h->header( | ||
| 77 | 'Content-type' => 'application/x-www-form-urlencoded', | ||
| 78 | 'X-Amzn-Authorization' => sprintf( | ||
| 79 | "AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA1,Signature=%s", | ||
| 80 | $self->access_key, $self->__signature( $h->header('Date') ) | ||
| 81 | ) | ||
| 82 | ); | ||
| 83 | return $h; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub new { | ||
| 87 | my $class = shift; | ||
| 88 | my %data = ( | ||
| 89 | access_key => '', | ||
| 90 | secret_key => '', | ||
| 91 | region => 'us-east-1', | ||
| 92 | from => '', | ||
| 93 | __user_agent => undef, | ||
| 94 | __response => undef, | ||
| 95 | __signer => undef, | ||
| 96 | @_ | ||
| 97 | ); | ||
| 98 | unless ( $data{access_key} && $data{secret_key} ) { | ||
| 99 | croak "new(): usage error"; | ||
| 100 | } | ||
| 101 | return bless \%data, $class; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub DESTROY { | ||
| 105 | my $self = shift; | ||
| 106 | $self->{__response} = undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub response { | ||
| 110 | my $self = shift; | ||
| 111 | return $self->{__response}; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub access_key { | ||
| 115 | my $self = shift; | ||
| 116 | my ($key) = @_; | ||
| 117 | return $self->{access_key} unless $key; | ||
| 118 | return $self->{access_key} = $key; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub secret_key { | ||
| 122 | my $self = shift; | ||
| 123 | my ($key) = @_; | ||
| 124 | return $self->{secret_key} unless $key; | ||
| 125 | return $self->{secret_key} = $key; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub region { | ||
| 129 | my $self = shift; | ||
| 130 | my ($key) = @_; | ||
| 131 | return $self->{region} unless $key; | ||
| 132 | return $self->{region} = $key; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub call { | ||
| 136 | my $self = shift; | ||
| 137 | my ( $action, $args, $responseClass ) = @_; | ||
| 138 | unless ($action) { | ||
| 139 | croak "call(): usage error"; | ||
| 140 | } | ||
| 141 | $args->{AWSAccessKeyId} = $self->access_key; | ||
| 142 | $args->{Action} = $action; | ||
| 143 | $args->{Timestamp} = $self->__timestamp; | ||
| 144 | my $ua = LWP::UserAgent->new(); | ||
| 145 | my $request = POST("https://email." . $self->region . ".amazonaws.com", [$args]); | ||
| 146 | my $signer = $self->__signer; | ||
| 147 | $signer->sign($request); | ||
| 148 | my $response = $ua->request($request); | ||
| 149 | return Net::AWS::SES::Response->new( $response, $action ); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub send { | ||
| 153 | my $self = shift; | ||
| 154 | return $self->send_mime(@_) if ( @_ == 1 ); | ||
| 155 | my (%args) = @_; | ||
| 156 | unless ( ref( $args{To} ) ) { | ||
| 157 | $args{To} = [ $args{To} ]; | ||
| 158 | } | ||
| 159 | my $from = $args{From} || $self->{from}; | ||
| 160 | unless ($from) { | ||
| 161 | croak "send(): usage error"; | ||
| 162 | } | ||
| 163 | unless ( $from && ( $args{Body} || $args{Body_html} ) && $args{To} ) { | ||
| 164 | croak "Usage Error"; | ||
| 165 | } | ||
| 166 | my %call_args = ( | ||
| 167 | 'Message.Subject.Data' => $args{Subject}, | ||
| 168 | 'Message.Subject.Charset' => 'UTF-8', | ||
| 169 | 'Source' => $from | ||
| 170 | ); | ||
| 171 | if ( $args{Body} ) { | ||
| 172 | $call_args{'Message.Body.Text.Data'} = $args{Body}; | ||
| 173 | $call_args{'Message.Body.Text.Charset'} = 'UTF-8',; | ||
| 174 | } | ||
| 175 | if ( $args{Body_html} ) { | ||
| 176 | $call_args{'Message.Body.Html.Data'} = $args{Body_html}; | ||
| 177 | $call_args{'Message.Body.Html.Charset'} = 'UTF-8'; | ||
| 178 | } | ||
| 179 | if ( $args{ReturnPath} ) { | ||
| 180 | $call_args{'ReturnPath'} = $args{ReturnPath}; | ||
| 181 | } | ||
| 182 | for ( my $i = 0 ; $i < @{ $args{To} } ; $i++ ) { | ||
| 183 | my $email = $args{To}->[$i]; | ||
| 184 | $call_args{ sprintf( 'Destination.ToAddresses.member.%d', $i + 1 ) } = | ||
| 185 | $email; | ||
| 186 | } | ||
| 187 | my $r = $self->call( 'SendEmail', \%call_args ); | ||
| 188 | } ## end sub send | ||
| 189 | |||
| 190 | sub verify_email { | ||
| 191 | my ( $self, $email ) = @_; | ||
| 192 | unless ($email) { | ||
| 193 | croak "verify_email(): usage error"; | ||
| 194 | } | ||
| 195 | return $self->call( 'VerifyEmailIdentity', { EmailAddress => $email } ); | ||
| 196 | } | ||
| 197 | *delete_domain = \&delete_identity; | ||
| 198 | *delete_email = \&delete_identity; | ||
| 199 | |||
| 200 | sub delete_identity { | ||
| 201 | my ( $self, $identity ) = @_; | ||
| 202 | unless ($identity) { | ||
| 203 | croak "delete_identity(): usage error"; | ||
| 204 | } | ||
| 205 | return $self->call( 'DeleteIdentity', { Identity => $identity } ); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub list_emails { | ||
| 209 | my $self = shift; | ||
| 210 | my %args = @_; | ||
| 211 | my %call_args = ( IdentityType => 'EmailAddress' ); | ||
| 212 | if ( $args{limit} ) { | ||
| 213 | $call_args{MaxItems} = $args{limit}; | ||
| 214 | } | ||
| 215 | if ( $args{offset} ) { | ||
| 216 | $call_args{NextToken} = $args{offset}; | ||
| 217 | } | ||
| 218 | my $r = $self->call( 'ListIdentities', \%call_args ); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub list_domains { | ||
| 222 | my $self = shift; | ||
| 223 | my %args = @_; | ||
| 224 | my %call_args = ( IdentityType => 'Domain' ); | ||
| 225 | if ( $args{limit} ) { | ||
| 226 | $call_args{MaxItems} = $args{limit}; | ||
| 227 | } | ||
| 228 | if ( $args{offset} ) { | ||
| 229 | $call_args{NextToken} = $args{offset}; | ||
| 230 | } | ||
| 231 | my $r = $self->call( 'ListIdentities', \%call_args ); | ||
| 232 | } | ||
| 233 | |||
| 234 | sub get_quota { | ||
| 235 | my $self = shift; | ||
| 236 | return $self->call('GetSendQuota'); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub get_statistics { | ||
| 240 | my $self = shift; | ||
| 241 | return $self->call('GetSendStatistics'); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub send_mime { | ||
| 245 | my $self = shift; | ||
| 246 | my $msg = $_[0] if ( @_ == 1 ); | ||
| 247 | if ( $msg && ref($msg) && $msg->isa("MIME::Entity") ) { | ||
| 248 | my $r = $self->call( 'SendRawEmail', | ||
| 249 | { 'RawMessage.Data' => encode_base64( $msg->stringify ) } ); | ||
| 250 | return $r; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | sub get_dkim_attributes { | ||
| 255 | my $self = shift; | ||
| 256 | my @identities = @_; | ||
| 257 | my %call_args = (); | ||
| 258 | for ( my $i = 0 ; $i < @identities ; $i++ ) { | ||
| 259 | my $id = $identities[$i]; | ||
| 260 | $call_args{ 'Identities.member.' . ( $i + 1 ) } = $id; | ||
| 261 | } | ||
| 262 | return $self->call( 'GetIdentityDkimAttributes', \%call_args ); | ||
| 263 | } | ||
| 264 | |||
| 265 | 1; | ||
| 266 | __END__ | ||