Class: AmazonPay::IpnHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_pay/ipn_handler.rb

Overview

AmazonPay Ipn Handler

This class authenticates an sns message sent from Amazon. It will validate the header, subject, and certificate. After validation there are many helper methods in place to extract information received from the ipn notification.

Constant Summary collapse

MSG_HEADER =
'Error - Header does not contain x-amz-sns-message-type header'.freeze
MSG_CERTIFICATE =
'Error - Unable to verify certificate subject issued by Amazon'.freeze
MSG_KEY =
'Error - Unable to verify public key with signature and signed string'.freeze
SIGNABLE_KEYS =
%w[
  Message
  MessageId
  Timestamp
  TopicArn
  Type
].freeze
COMMON_NAME =
'sns.amazonaws.com'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, body, proxy_addr: :ENV, proxy_port: nil, proxy_user: nil, proxy_pass: nil, log_enabled: false, log_file_name: nil, log_level: :DEBUG) ⇒ IpnHandler

Returns a new instance of IpnHandler.

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/amazon_pay/ipn_handler.rb', line 54

def initialize(
  headers,
  body,
  proxy_addr: :ENV,
  proxy_port: nil,
  proxy_user: nil,
  proxy_pass: nil,
  log_enabled: false,
  log_file_name: nil,
  log_level: :DEBUG
)

  @body = body
  @raw = parse_from(@body)
  @headers = headers
  @proxy_addr = proxy_addr
  @proxy_port = proxy_port
  @proxy_user = proxy_user
  @proxy_pass = proxy_pass

  @log_enabled = log_enabled
  @logger = AmazonPay::LogInitializer.new(log_file_name, log_level).create_logger if @log_enabled
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



37
38
39
# File 'lib/amazon_pay/ipn_handler.rb', line 37

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



37
38
39
# File 'lib/amazon_pay/ipn_handler.rb', line 37

def headers
  @headers
end

#proxy_addrObject

Returns the value of attribute proxy_addr.



41
42
43
# File 'lib/amazon_pay/ipn_handler.rb', line 41

def proxy_addr
  @proxy_addr
end

#proxy_passObject

Returns the value of attribute proxy_pass.



41
42
43
# File 'lib/amazon_pay/ipn_handler.rb', line 41

def proxy_pass
  @proxy_pass
end

#proxy_portObject

Returns the value of attribute proxy_port.



41
42
43
# File 'lib/amazon_pay/ipn_handler.rb', line 41

def proxy_port
  @proxy_port
end

#proxy_userObject

Returns the value of attribute proxy_user.



41
42
43
# File 'lib/amazon_pay/ipn_handler.rb', line 41

def proxy_user
  @proxy_user
end

Instance Method Details

#authentic?Boolean

This method will authenticate the ipn message sent from Amazon. It will return true if everything is verified. It will raise an error message if verification fails.

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/amazon_pay/ipn_handler.rb', line 81

def authentic?
  decoded_from_base64 = Base64.decode64(signature)
  validate_header
  validate_subject(certificate.subject)
  public_key = public_key_from(certificate)
  verify_public_key(public_key, decoded_from_base64, canonical_string)

  return true
rescue IpnWasNotAuthenticError => e
  raise e.message
end

#environmentObject



137
138
139
# File 'lib/amazon_pay/ipn_handler.rb', line 137

def environment
  parse_from(@raw['Message'])['ReleaseEnvironment']
end

#messageObject



105
106
107
# File 'lib/amazon_pay/ipn_handler.rb', line 105

def message
  @raw['Message']
end

#message_idObject



97
98
99
# File 'lib/amazon_pay/ipn_handler.rb', line 97

def message_id
  @raw['MessageId']
end

#message_timestampObject



149
150
151
# File 'lib/amazon_pay/ipn_handler.rb', line 149

def message_timestamp
  parse_from(@raw['Message'])['Timestamp']
end

#notification_dataObject



145
146
147
# File 'lib/amazon_pay/ipn_handler.rb', line 145

def notification_data
  parse_from(@raw['Message'])['NotificationData']
end

#notification_typeObject



129
130
131
# File 'lib/amazon_pay/ipn_handler.rb', line 129

def notification_type
  parse_from(@raw['Message'])['NotificationType']
end

#parse_from(json) ⇒ Object



153
154
155
# File 'lib/amazon_pay/ipn_handler.rb', line 153

def parse_from(json)
  JSON.parse(json)
end

#seller_idObject



133
134
135
# File 'lib/amazon_pay/ipn_handler.rb', line 133

def seller_id
  parse_from(@raw['Message'])['SellerId']
end

#signatureObject



113
114
115
# File 'lib/amazon_pay/ipn_handler.rb', line 113

def signature
  @raw['Signature']
end

#signature_versionObject



117
118
119
# File 'lib/amazon_pay/ipn_handler.rb', line 117

def signature_version
  @raw['SignatureVersion']
end

#signing_cert_urlObject



121
122
123
# File 'lib/amazon_pay/ipn_handler.rb', line 121

def signing_cert_url
  @raw['SigningCertURL']
end

#timestampObject



109
110
111
# File 'lib/amazon_pay/ipn_handler.rb', line 109

def timestamp
  @raw['Timestamp']
end

#topic_arnObject



101
102
103
# File 'lib/amazon_pay/ipn_handler.rb', line 101

def topic_arn
  @raw['TopicArn']
end

#typeObject



93
94
95
# File 'lib/amazon_pay/ipn_handler.rb', line 93

def type
  @raw['Type']
end

#unsubscribe_urlObject



125
126
127
# File 'lib/amazon_pay/ipn_handler.rb', line 125

def unsubscribe_url
  @raw['UnsubscribeURL']
end

#versionObject



141
142
143
# File 'lib/amazon_pay/ipn_handler.rb', line 141

def version
  parse_from(@raw['Message'])['Version']
end