blib/lib/AWS/SNS/Verify.pm

filter: Statementstmt 61.7% 50 / 81 Branchbran 42.8% 6 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 61.1% 11 / 18 Podpod 100% 5 / 5 totaltotal 61.9% 75 / 121 SCAR 28.0
CC
10
Cov
60%
generate_signature_string
20.0
fetch_certificate
6.0
verify
6.0
CRAP 16.5
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AWS::SNS::Verify;
4 1 $AWS::SNS::Verify::VERSION = '0.0103';
5 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Crypt::OpenSSL::X509;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 has body => (
15 is => 'ro',
16 required => 1,
17 );
18  
19 has message => (
20 is => 'ro',
21 lazy => 1,
22 default => sub {
23 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 return JSON::decode_json($self->body);
25 }
26 1 );
27  
28 has certificate_string => (
29 is => 'ro',
30 lazy => 1,
31 default => sub {
32 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 return $self->fetch_certificate;
34 }
35 1 );
36  
37 has certificate => (
38 is => 'ro',
39 lazy => 1,
40 default => sub {
41 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 return Crypt::OpenSSL::X509->new_from_string($self->certificate_string);
43 }
44 1 );
45  
46 1 has validate_signing_cert_url => (
47 is => 'ro',
48 lazy => 1,
49 default => 1,
50 );
51  
52 sub fetch_certificate {
53 0 ▶ my $self = shift;
Subroutine fetch_certificate: not called CC=2 SCAR=17.9
Pod documented
54 0 my $url = $self->valid_cert_url($self->message->{SigningCertURL});
55 0 my $response = HTTP::Tiny->new->get($url);
56 0 ▶ if ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 0 0
57 0 return $response->{content};
58 }
59 else {
60 0 ouch $response->{status}, $response->{reason}, $response;
61 }
62 }
63  
64 sub generate_signature_string {
65 0 ▶ my $self = shift;
Subroutine generate_signature_string: not called CC=4 SCAR=30.0
Pod documented
66 0 my $body = $self->message;
67 0 my @fields;
68 0 ▶ if ($body->{Type} eq 'Notification') {
Branch: 1 branch
BranchTrueFalseTotal
if ($body->{'Type'} eq 'Notification') { } 0 0 0
69 0 @fields = (qw(Message MessageId Subject Timestamp TopicArn Type)) ;
70 }
71 else {
72 0 @fields = (qw(Message MessageId SubscribeURL Timestamp Token TopicArn Type));
73 }
74 0 my @parts;
75 0 foreach my $field (@fields) {
76 0 ▶ if (exists $body->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $body->{$field}) 0 0 0
77 0 push @parts, $field;
78 0 push @parts, $body->{$field};
79 }
80 }
81 0 return join("\n", @parts)."\n";
82 }
83  
84 sub decode_signature {
85 0 ▶ my $self = shift;
Subroutine decode_signature: not called CC=1 SCAR=6.9
Pod documented
86 0 return decode_base64($self->message->{Signature});
87 }
88  
89 sub verify {
90 0 ▶ my $self = shift;
Subroutine verify: not called CC=2 SCAR=17.9
Pod documented
91 0 my $rsa = Crypt::OpenSSL::RSA->new_public_key($self->certificate->pubkey);
92 0 ▶ unless ($rsa->verify($self->generate_signature_string, $self->decode_signature)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsa->verify($self->generate_signature_string, $self->decode_signature)) 0 0 0
93 0 ouch 'Bad SNS Signature', 'Could not verify the SES message from its signature.', $self;
94 }
95 0 return 1;
96 }
97  
98 # See also:
99 # https://github.com/aws/aws-php-sns-message-validator/blob/master/src/MessageValidator.php#L22
100 sub valid_cert_url {
101 6 ▶ my $self = shift;
Subroutine valid_cert_url: called CC=5 SCAR=16.1
Pod documented
102 6 my ($url_string) = @_;
103 6 ▶ $url_string ||= '';
Condition100%2/2
$url_string ||= ""
l!l
51
MC/DC100%1/1
$url_string ||= ""
$url_string
Truth table100%2/2
A
A= $url_string
Aresult
11
00
104  
105 6 ▶ return $url_string unless $self->validate_signing_cert_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_signing_cert_url 1 5 6
106  
107 5 my $url = URI::URL->new($url_string);
108 5 ▶ unless ( $url->can('host') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url->can("host")) 2 3 5
109 2 ouch 'Bad SigningCertURL', "The SigningCertURL ($url_string) isn't a valid URL", $self;
110 }
111 3 my $host = $url->host;
112  
113 # Match all regional SNS endpoints, e.g.
114 # sns.<region>.amazonaws.com (AWS)
115 # sns.us-gov-west-1.amazonaws.com (AWS GovCloud)
116 # sns.cn-north-1.amazonaws.com.cn (AWS China)
117 3 my $dot = qr/\./;
118 3 my $region = qr/[a-zA-Z0-9-]+/;
119 3 ▶ unless ($host =~ /^ sns $dot $region $dot amazonaws $dot com(\.cn)? $/x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^ sns $dot $region $dot amazonaws $dot com(\.cn)? $/x) 1 2 3
120 1 ouch 'Bad SigningCertURL', "The SigningCertURL ($url_string) isn't an Amazon endpoint", $self;
121 }
122  
123 2 return $url_string;
124 }
125  
126  
127 =head1 NAME
128  
129 AWS::SNS::Verify - Verifies authenticity of SNS messages.
130  
131 =head1 VERSION
132  
133 version 0.0103
134  
135 =head1 SYNOPSIS
136  
137 my $body = request->body; # example fetch raw body from Dancer
138 my $sns = AWS::SNS::Verify->new(body => $body);
139 if ($sns->verify) {
140 return $sns->message;
141 }
142  
143 =head1 DESCRIPTION
144  
145 This module will parse a message from Amazon Simple Notification Service and validate its signature. This way you know the message came from AWS and not some third-party. More info here: L<http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.verify.signature.html>.
146  
147 =head1 METHODS
148  
149 =head2 new
150  
151 Constructor.
152  
153 =over
154  
155 =item body
156  
157 Required. JSON string posted by AWS SNS. Looks like:
158  
159 {
160 "Type" : "Notification",
161 "MessageId" : "a890c547-5d98-55e2-971d-8826fff56413",
162 "TopicArn" : "arn:aws:sns:us-east-1:041977924901:foo",
163 "Subject" : "test subject",
164 "Message" : "test message",
165 "Timestamp" : "2015-02-20T20:59:25.401Z",
166 "SignatureVersion" : "1",
167 "Signature" : "kzi3JBQz64uFAXG9ZuAwPI2gYW5tT7OF83oeHb8v0/XRPsy0keq2NHTCpQVRxCgPOJ/QUB2Yl/L29/W4hiHMo9+Ns0hrqyasgUfjq+XkVR1WDuYLtNaEA1vLnA0H9usSh3eVVlLhpYzoT4GUoGgstRVvFceW2QVF9EYUQyromlcbOVtVpKCEINAvGEEKJNGTXQQUkPUka3YMhHitgQg1WlFBmf+oweSYUEj8+RoguWsn6vluxD0VtIOGOml5jlUecfhDqnetF5pUVYMqCHPfHn6RBguiW+XD6XWsdKKxkjqo90a65Nlb72gPSRw6+sIEIgf4J39WFZK+FCpeSm0qAg==",
168 "SigningCertURL" : "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-d6d679a1d18e95c2f9ffcf11f4f9e198.pem",
169 "UnsubscribeURL" : "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:041977924901:foo:20b2d060-2a32-4506-9cb0-24b8b9e605e1",
170 "MessageAttributes" : {
171 "AWS.SNS.MOBILE.MPNS.Type" : {"Type":"String","Value":"token"},
172 "AWS.SNS.MOBILE.WNS.Type" : {"Type":"String","Value":"wns/badge"},
173 "AWS.SNS.MOBILE.MPNS.NotificationClass" : {"Type":"String","Value":"realtime"}
174 }
175 }
176  
177 =item certificate_string
178  
179 By default AWS::SNS::Verify will fetch the certificate string by issuing an HTTP GET request to C<SigningCertURL>. The SigningCertURL in the message must be a AWS SNS endpoint.
180  
181 If you wish to use a cached version, then pass it in.
182  
183 =item validate_signing_cert_url (default: true)
184  
185 If you're using a fake SNS server in your local test environment, the SigningCertURL won't be an AWS endpoint. If so, set validate_signing_cert_url to 0.
186  
187 Don't ever do this in any kind of Production environment.
188  
189 =back
190  
191 =head2 verify
192  
193 Returns a 1 on success, or die with an L<Ouch> on a failure.
194  
195 =head2 message
196  
197 Returns a hash reference of the decoded L<body> that was passed in to the constructor.
198  
199 =head2 certificate_string
200  
201 If you want to cache the certificate in a local cache, then get it using this method.
202  
203  
204 =head2 decode_signature
205  
206 You should never need to call this, it decodes the base64 signature.
207  
208 =head2 fetch_certificate
209  
210 You should never need to call this, it fetches the signing certificate.
211  
212  
213 =head2 generate_signature_string
214  
215 You should never need to call this, it generates the signature string required to verify the request.
216  
217 =head2 valid_cert_url
218  
219 You should never need to call this, it checks the validity of the certificate signing URL per L<https://github.com/aws/aws-php-sns-message-validator/blob/master/src/MessageValidator.php#L22>
220  
221 =head1 REQUIREMENTS
222  
223 Requires Perl 5.12 or higher and these modules:
224  
225 =over
226  
227 =item *
228  
229 Ouch
230  
231 =item *
232  
233 JSON
234  
235 =item *
236  
237 HTTP::Tiny
238  
239 =item *
240  
241 MIME::Base64
242  
243 =item *
244  
245 Moo
246  
247 =item *
248  
249 Crypt::OpenSSL::RSA
250  
251 =item *
252  
253 Crypt::OpenSSL::X509
254  
255 =back
256  
257 =head1 SUPPORT
258  
259 =over
260  
261 =item Repository
262  
263 L<http://github.com/rizen/AWS-SNS-Verify>
264  
265 =item Bug Reports
266  
267 L<http://github.com/rizen/AWS-SNS-Verify/issues>
268  
269 =back
270  
271  
272 =head1 AUTHOR
273  
274 JT Smith <jt_at_plainblack_dot_com>
275  
276 =head1 LEGAL
277  
278 AWS::SNS::Verify is Copyright 2015 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
279  
280 =cut
281  
282  
283 1 1;
↑ Summary