Class: AmazonPay::IpnHandler
- Inherits:
-
Object
- Object
- AmazonPay::IpnHandler
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#proxy_addr ⇒ Object
Returns the value of attribute proxy_addr.
-
#proxy_pass ⇒ Object
Returns the value of attribute proxy_pass.
-
#proxy_port ⇒ Object
Returns the value of attribute proxy_port.
-
#proxy_user ⇒ Object
Returns the value of attribute proxy_user.
Instance Method Summary collapse
-
#authentic? ⇒ Boolean
This method will authenticate the ipn message sent from Amazon.
- #environment ⇒ Object
-
#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
constructor
A new instance of IpnHandler.
- #message ⇒ Object
- #message_id ⇒ Object
- #message_timestamp ⇒ Object
- #notification_data ⇒ Object
- #notification_type ⇒ Object
- #parse_from(json) ⇒ Object
- #seller_id ⇒ Object
- #signature ⇒ Object
- #signature_version ⇒ Object
- #signing_cert_url ⇒ Object
- #timestamp ⇒ Object
- #topic_arn ⇒ Object
- #type ⇒ Object
- #unsubscribe_url ⇒ Object
- #version ⇒ Object
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.
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
37 38 39 |
# File 'lib/amazon_pay/ipn_handler.rb', line 37 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
37 38 39 |
# File 'lib/amazon_pay/ipn_handler.rb', line 37 def headers @headers end |
#proxy_addr ⇒ Object
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_pass ⇒ Object
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_port ⇒ Object
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_user ⇒ Object
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.
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. end |
#environment ⇒ Object
137 138 139 |
# File 'lib/amazon_pay/ipn_handler.rb', line 137 def environment parse_from(@raw['Message'])['ReleaseEnvironment'] end |
#message ⇒ Object
105 106 107 |
# File 'lib/amazon_pay/ipn_handler.rb', line 105 def @raw['Message'] end |
#message_id ⇒ Object
97 98 99 |
# File 'lib/amazon_pay/ipn_handler.rb', line 97 def @raw['MessageId'] end |
#message_timestamp ⇒ Object
149 150 151 |
# File 'lib/amazon_pay/ipn_handler.rb', line 149 def parse_from(@raw['Message'])['Timestamp'] end |
#notification_data ⇒ Object
145 146 147 |
# File 'lib/amazon_pay/ipn_handler.rb', line 145 def notification_data parse_from(@raw['Message'])['NotificationData'] end |
#notification_type ⇒ Object
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_id ⇒ Object
133 134 135 |
# File 'lib/amazon_pay/ipn_handler.rb', line 133 def seller_id parse_from(@raw['Message'])['SellerId'] end |
#signature ⇒ Object
113 114 115 |
# File 'lib/amazon_pay/ipn_handler.rb', line 113 def signature @raw['Signature'] end |
#signature_version ⇒ Object
117 118 119 |
# File 'lib/amazon_pay/ipn_handler.rb', line 117 def signature_version @raw['SignatureVersion'] end |
#signing_cert_url ⇒ Object
121 122 123 |
# File 'lib/amazon_pay/ipn_handler.rb', line 121 def signing_cert_url @raw['SigningCertURL'] end |
#timestamp ⇒ Object
109 110 111 |
# File 'lib/amazon_pay/ipn_handler.rb', line 109 def @raw['Timestamp'] end |
#topic_arn ⇒ Object
101 102 103 |
# File 'lib/amazon_pay/ipn_handler.rb', line 101 def topic_arn @raw['TopicArn'] end |
#type ⇒ Object
93 94 95 |
# File 'lib/amazon_pay/ipn_handler.rb', line 93 def type @raw['Type'] end |
#unsubscribe_url ⇒ Object
125 126 127 |
# File 'lib/amazon_pay/ipn_handler.rb', line 125 def unsubscribe_url @raw['UnsubscribeURL'] end |
#version ⇒ Object
141 142 143 |
# File 'lib/amazon_pay/ipn_handler.rb', line 141 def version parse_from(@raw['Message'])['Version'] end |