Created
September 8, 2017 19:51
-
-
Save danquinney/6a9ebf33294818c929e18c731e50bfb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sirportly | |
module Modules | |
module MailHandlers | |
class Postal | |
# Allows an incoming AppMail messaage to be verified and turned into a ruby object | |
class PayloadProcessor | |
class InvalidSignatureError < StandardError; end | |
attr_reader :request | |
# @param [Rack::Request] request Incoming web request containing an email to be processed | |
def initialize(request) | |
@request = request | |
end | |
# Verify the payload of the request using the signature in the X-AppMail-Signature HTTP header and the AppMail | |
# public key store in APPMAIL_PUBLIC_KEY | |
def valid_signature? | |
BasicSSL.verify(Sirportly.config.postal_payload_verification_key, signature, raw_payload) | |
end | |
# Parse the payload in the message | |
# | |
# @return [Hash] hash representation of the payload | |
def payload | |
raise InvalidSignatureError unless valid_signature? | |
JSON.parse(raw_payload, :symbolize_names => true) | |
end | |
private | |
def raw_payload | |
@raw_payload ||= request.body.read | |
end | |
def signature | |
request.headers['X-Postal-Signature'] | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment