File Coverage

blib/lib/Signer/AWSv4/SES.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Signer::AWSv4::SES;
2 1     1   70383 use Moo;
  1         12254  
  1         5  
3             extends 'Signer::AWSv4';
4 1     1   2119 use Types::Standard qw/Str/;
  1         77991  
  1         10  
5              
6 1     1   1315 use JSON::MaybeXS qw//;
  1         5765  
  1         25  
7 1     1   442 use MIME::Base64 qw//;
  1         615  
  1         425  
8              
9             has '+expires' => (default => 0);
10             #has '+region' => (default => 'us-east-1');
11             has '+service' => (default => 'ses');
12             has '+method' => (default => '');
13             has '+uri' => (default => '/');
14             has '+date' => (default => '11111111');
15              
16             has smtp_user => (is => 'ro', isa => Str, default => sub {
17             my $self = shift;
18             return $self->access_key;
19             });
20             has smtp_password => (is => 'ro', isa => Str, lazy => 1, builder => '_build_password_v4');
21             has smtp_password_v2 => (is => 'ro', isa => Str, lazy => 1, builder => '_build_password_v2');
22              
23             has smtp_endpoint => (is => 'ro', isa => Str, default => sub {
24             my $self = shift;
25             sprintf 'email-smtp.%s.amazonaws.com', $self->region;
26             });
27              
28             has '+signing_key' => (default => sub {
29             my $self = shift;
30             my $signature = Digest::SHA::hmac_sha256($self->date, "AWS4" . $self->secret_key);
31             $signature = Digest::SHA::hmac_sha256($self->region, $signature);
32             $signature = Digest::SHA::hmac_sha256($self->service, $signature);
33             $signature = Digest::SHA::hmac_sha256('aws4_request', $signature);
34             $signature = Digest::SHA::hmac_sha256('SendRawEmail', $signature);
35             return $signature;
36             });
37              
38             sub _build_password_v2 {
39 1     1   747 my $self = shift;
40            
41 1         14 my $signature = Digest::SHA::hmac_sha256('SendRawEmail', $self->secret_key);
42 1         23 MIME::Base64::encode_base64("\x02" . $signature, '');
43             }
44              
45             sub _build_password_v4 {
46 2     2   2203 my $self = shift;
47              
48 2         37 MIME::Base64::encode_base64("\x04" . $self->signing_key, '');
49             }
50              
51             1;
52             ### main pod documentation begin ###
53              
54             =encoding UTF-8
55              
56             =head1 NAME
57              
58             Signer::AWSv4::SES - Generate passwords for sending email through SES SMTP servers with IAM credentials
59              
60             =head1 SYNOPSIS
61              
62             use Signer::AWSv4::SES;
63             $pass_gen = Signer::AWSv4::SES->new(
64             access_key => 'AKIAIOSFODNN7EXAMPLE',
65             secret_key => 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
66             region => 'us-east-1',
67             );
68             $pass_gen->smtp_password;
69              
70             =head1 DESCRIPTION
71              
72             Generate passwords for sending email through SES SMTP servers with IAM credentials.
73             The IAM user needs to have the ses:SendRawEmail IAM permission to be able to send mail.
74              
75             This module generates v4 signatures for SES, unlike lots of other examples around the
76             Internet, that use the old v2 signature scheme, although a fallback for obtaining a v2
77             password is still there, just in case you want to use it.
78              
79             =head1 Request Attributes
80              
81             This module requires C, C and C passed to the constructor
82              
83             =head1 Signature Attributes
84              
85             =head2 smtp_user
86              
87             This has to be used as the user for SMTP authentication to the SES SMTP endpoint
88              
89             =head2 smtp_password
90              
91             This has to be used as the password the SMTP authentication
92              
93             =head2 smtp_password_v2
94              
95             This is the password in Version 2 format (not recommended). It is not dependant on the
96             region, so you can basically pass any value to it if you're only interested in the v2
97             signature.
98              
99             =head1 Extra Attributes
100              
101             =head2 smtp_endpoint
102              
103             This calculates the name of the SMTP endpoint in funtion of the region.
104              
105             =head1 SEE ALSO
106              
107             L
108              
109             =head1 BUGS and SOURCE
110              
111             The source code is located here: L
112              
113             Please report bugs to: L
114              
115             =head1 AUTHOR
116              
117             Jose Luis Martinez
118             pplusdomain@gmail.com
119              
120             =head1 COPYRIGHT and LICENSE
121              
122             Copyright (c) 2018 by Jose Luis Martinez
123              
124             This code is distributed under the Apache 2 License. The full text of the license can be found in the LICENSE file included with this module.
125              
126             =cut