| 1 | package Net::Amazon::SNS::Signature; | ||||||||||
| 2 | 1 | $Net::Amazon::SNS::Signature::VERSION = '0.003'; | |||||||||
| 3 | 1 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Crypt::OpenSSL::X509; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Net::Amazon::SNS::Signature | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | For the verification of Amazon SNS messages | ||||||||||
| 18 | |||||||||||
| 19 | =head1 USAGE | ||||||||||
| 20 | |||||||||||
| 21 | # Will download the signature certificate from SigningCertURL attribute of $message_ref | ||||||||||
| 22 | # use LWP::UserAgent | ||||||||||
| 23 | my $sns_signature = Net::Amazon::SNS::Signature->new(); | ||||||||||
| 24 | if ( $sns_signature->verify( $message_ref ) ){ ... } | ||||||||||
| 25 | |||||||||||
| 26 | # Will automatically download the certificate using your own user_agent ( supports ->get returns HTTP::Response ) | ||||||||||
| 27 | my $sns_signature = Net::Amazon::SNS::Signature->new( user_agent => $my_user_agent ); | ||||||||||
| 28 | if ( $sns_signature->verify( $message_ref ) ){ ... } | ||||||||||
| 29 | |||||||||||
| 30 | # Provide the certificate yourself | ||||||||||
| 31 | my $sns_signature = Net::Amazon::SNS::Signature->new() | ||||||||||
| 32 | if ( $sns_signature->verify({ message => $message_ref, certificate => $x509_cert }) ) { ... } | ||||||||||
| 33 | |||||||||||
| 34 | =head2 verify | ||||||||||
| 35 | |||||||||||
| 36 | Call to verify the message, C<$message> is required as first parameter, C<$cert> is | ||||||||||
| 37 | optional and should be a raw x509 certificate as downloaded from Amazon. | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | sub verify { | ||||||||||
| 42 | 1 | ▶ | my ( $self, $message, $cert ) = @_; | ||||||||
|
Subroutine
verify: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 43 | |||||||||||
| 44 | my $signature = MIME::Base64::decode_base64($message->{Signature}) | ||||||||||
| 45 | 1 | ▶ | or carp( "Signature is a required attribute of message" ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 1 | my $string = $self->build_sign_string( $message ); | |||||||||
| 47 | my $public_key = $cert ? $self->_key_from_cert( $cert ) : | ||||||||||
| 48 | 1 | ▶ | $self->_public_key_from_url( $message->{SigningCertURL} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 1 | my $rsa = Crypt::OpenSSL::RSA->new_public_key( $public_key ); | |||||||||
| 51 | 1 | return $rsa->verify($string, $signature); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | =head2 build_sign_string | ||||||||||
| 55 | |||||||||||
| 56 | Given a C<$message_ref> will return a formatted string ready to be signed. | ||||||||||
| 57 | |||||||||||
| 58 | Usage: | ||||||||||
| 59 | |||||||||||
| 60 | my $sign_string = $this->build_sign_string({ | ||||||||||
| 61 | Message => 'Hello', | ||||||||||
| 62 | MessageId => '12345', | ||||||||||
| 63 | Subject => 'I am a message', | ||||||||||
| 64 | Timestamp => '2016-01-20T14:37:01Z', | ||||||||||
| 65 | TopicArn => 'xyz123', | ||||||||||
| 66 | Type => 'Notification' | ||||||||||
| 67 | }); | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | sub build_sign_string { | ||||||||||
| 72 | 1 | ▶ | my ( $self, $message ) = @_; | ||||||||
|
Subroutine
build_sign_string: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||
| 73 | |||||||||||
| 74 | 1 | ▶ | my @keys = ( qw/Message MessageId/, ( defined($message->{Subject}) ? 'Subject' : () ), qw/Timestamp TopicArn Type/ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 1 | ▶ | defined($message->{$_}) or carp( sprintf( "%s is required", $_ ) ) for @keys; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 1 | return join( "\n", ( map { ( $_, $message->{$_} ) } @keys ), "" ); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub new { | ||||||||||
| 80 | 1 | ▶ | my ( $class, $args_ref ) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 81 | return bless { | ||||||||||
| 82 | 1 | ▶ | defined($args_ref->{user_agent}) ? ( user_agent => $args_ref->{user_agent} ) : () | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | }, $class; | ||||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | sub _public_key_from_url { | ||||||||||
| 87 | 0 | ▶ | my ( $self, $url ) = @_; | ||||||||
|
Subroutine
_public_key_from_url: not called CC=1 SCAR=6.9
| |||||||||||
| 88 | 0 | my $response = $self->user_agent->get( $url ); | |||||||||
| 89 | 0 | my $content = $response->decoded_content; | |||||||||
| 90 | 0 | return $self->_key_from_cert( $content ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub _key_from_cert { | ||||||||||
| 94 | 1 | ▶ | my ( $self, $cert ) = @_; | ||||||||
|
Subroutine
_key_from_cert: called CC=1 SCAR=0.0
| |||||||||||
| 95 | 1 | my $x509 = Crypt::OpenSSL::X509->new_from_string( | |||||||||
| 96 | $cert, Crypt::OpenSSL::X509::FORMAT_PEM | ||||||||||
| 97 | ); | ||||||||||
| 98 | 1 | return $x509->pubkey; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | sub user_agent { | ||||||||||
| 102 | 0 | ▶ | my ( $self ) = @_; | ||||||||
|
Subroutine
user_agent: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 103 | 0 | ▶ | unless ( defined( $self->{user_agent} ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 0 | $self->{user_agent} = LWP::UserAgent->new(); | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | return $self->{user_agent}; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 1 | 1; | |||||||||