mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add Mandrill support
This commit is contained in:
parent
6b7eac5c51
commit
3984460424
3 changed files with 91 additions and 0 deletions
|
@ -0,0 +1,59 @@
|
|||
class ActionMailbox::Ingresses::Mandrill::InboundEmailsController < ActionMailbox::BaseController
|
||||
before_action :ensure_authenticated
|
||||
|
||||
def create
|
||||
raw_emails.each { |raw_email| ActionMailbox::InboundEmail.create_and_extract_message_id! raw_email }
|
||||
head :ok
|
||||
rescue JSON::ParserError => error
|
||||
log.error error.message
|
||||
head :unprocessable_entity
|
||||
end
|
||||
|
||||
private
|
||||
def raw_emails
|
||||
events.lazy.
|
||||
select { |event| event["event"] == "inbound" }.
|
||||
collect { |event| event.dig("msg", "raw_msg") }.
|
||||
collect { |message| StringIO.new message }
|
||||
end
|
||||
|
||||
def events
|
||||
JSON.parse params.require(:mandrill_events)
|
||||
end
|
||||
|
||||
|
||||
def ensure_authenticated
|
||||
head :unauthorized unless authenticated?
|
||||
end
|
||||
|
||||
def authenticated?
|
||||
Authenticator.new(request).authenticated?
|
||||
end
|
||||
|
||||
class Authenticator
|
||||
cattr_accessor :key
|
||||
|
||||
attr_reader :request
|
||||
|
||||
def initialize(request)
|
||||
@request = request
|
||||
end
|
||||
|
||||
def authenticated?
|
||||
ActiveSupport::SecurityUtils.secure_compare given_signature, expected_signature
|
||||
end
|
||||
|
||||
private
|
||||
def given_signature
|
||||
request.headers["X-Mandrill-Signature"]
|
||||
end
|
||||
|
||||
def expected_signature
|
||||
Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, key, message)).strip
|
||||
end
|
||||
|
||||
def message
|
||||
[ request.original_url, request.POST.sort ].flatten.join
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,6 +5,7 @@ Rails.application.routes.draw do
|
|||
post "/amazon/inbound_emails" => "action_mailbox/ingresses/amazon/inbound_emails#create", as: :rails_amazon_inbound_emails
|
||||
post "/postfix/inbound_emails" => "action_mailbox/ingresses/postfix/inbound_emails#create", as: :rails_postfix_inbound_emails
|
||||
post "/sendgrid/inbound_emails" => "action_mailbox/ingresses/sendgrid/inbound_emails#create", as: :rails_sendgrid_inbound_emails
|
||||
post "/mandrill/inbound_emails" => "action_mailbox/ingresses/mandrill/inbound_emails#create", as: :rails_mandrill_inbound_emails
|
||||
|
||||
# Mailgun requires that the webhook's URL end in 'mime' for it to receive the raw contents of emails.
|
||||
post "/mailgun/inbound_emails/mime" => "action_mailbox/ingresses/mailgun/inbound_emails#create", as: :rails_mailgun_inbound_emails
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
require "test_helper"
|
||||
|
||||
ActionMailbox::Ingresses::Mandrill::InboundEmailsController::Authenticator.key = "1l9Qf7lutEf7h73VXfBwhw"
|
||||
|
||||
class ActionMailbox::Ingresses::Mandrill::InboundEmailsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
@events = JSON.generate([{ event: "inbound", msg: { raw_msg: file_fixture("../files/welcome.eml").read } }])
|
||||
end
|
||||
|
||||
test "receiving an inbound email from Mandrill" do
|
||||
assert_difference -> { ActionMailbox::InboundEmail.count }, +1 do
|
||||
post rails_mandrill_inbound_emails_url,
|
||||
headers: { "X-Mandrill-Signature" => "gldscd2tAb/G+DmpiLcwukkLrC4=" }, params: { mandrill_events: @events }
|
||||
end
|
||||
|
||||
assert_response :ok
|
||||
|
||||
inbound_email = ActionMailbox::InboundEmail.last
|
||||
assert_equal file_fixture("../files/welcome.eml").read, inbound_email.raw_email.download
|
||||
assert_equal "0CB459E0-0336-41DA-BC88-E6E28C697DDB@37signals.com", inbound_email.message_id
|
||||
end
|
||||
|
||||
test "rejecting a forged inbound email from Mandrill" do
|
||||
assert_no_difference -> { ActionMailbox::InboundEmail.count } do
|
||||
post rails_mandrill_inbound_emails_url,
|
||||
headers: { "X-Mandrill-Signature" => "forged" }, params: { mandrill_events: @events }
|
||||
end
|
||||
|
||||
assert_response :unauthorized
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue