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