diff options
author | George Claghorn <george@basecamp.com> | 2018-12-24 15:16:22 -0500 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2018-12-25 21:32:35 -0500 |
commit | a5b2fff64ca0c1fa7be5124f40a251d991c10a85 (patch) | |
tree | 33a79841402b7151e52d9ad3949ce54f320c10aa /actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb | |
parent | 4298df00ae6219b9b5b7c40f281d4fa4d66f4383 (diff) | |
parent | dcddff1d2d0c695318670686a27429a76f20ae03 (diff) | |
download | rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.tar.gz rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.tar.bz2 rails-a5b2fff64ca0c1fa7be5124f40a251d991c10a85.zip |
Import Action Mailbox
Diffstat (limited to 'actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb')
-rw-r--r-- | actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb b/actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb new file mode 100644 index 0000000000..2a8ec375c7 --- /dev/null +++ b/actionmailbox/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# Ingests inbound emails from Amazon's Simple Email Service (SES). +# +# Requires the full RFC 822 message in the +content+ parameter. Authenticates requests by validating their signatures. +# +# Returns: +# +# - <tt>204 No Content</tt> if an inbound email is successfully recorded and enqueued for routing to the appropriate mailbox +# - <tt>401 Unauthorized</tt> if the request's signature could not be validated +# - <tt>404 Not Found</tt> if Action Mailbox is not configured to accept inbound emails from SES +# - <tt>422 Unprocessable Entity</tt> if the request is missing the required +content+ parameter +# - <tt>500 Server Error</tt> if one of the Active Record database, the Active Storage service, or +# the Active Job backend is misconfigured or unavailable +# +# == Usage +# +# 1. Install the {aws-sdk-sns}[https://rubygems.org/gems/aws-sdk-sns] gem: +# +# # Gemfile +# gem "aws-sdk-sns", ">= 1.9.0", require: false +# +# 2. Tell Action Mailbox to accept emails from SES: +# +# # config/environments/production.rb +# config.action_mailbox.ingress = :amazon +# +# 3. {Configure SES}[https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-notifications.html] +# to deliver emails to your application via POST requests to +/rails/action_mailbox/amazon/inbound_emails+. +# If your application lived at <tt>https://example.com</tt>, you would specify the fully-qualified URL +# <tt>https://example.com/rails/action_mailbox/amazon/inbound_emails</tt>. +class ActionMailbox::Ingresses::Amazon::InboundEmailsController < ActionMailbox::BaseController + before_action :authenticate + + cattr_accessor :verifier + + def self.prepare + self.verifier ||= begin + require "aws-sdk-sns/message_verifier" + Aws::SNS::MessageVerifier.new + end + end + + def create + ActionMailbox::InboundEmail.create_and_extract_message_id! params.require(:content) + end + + private + def authenticate + head :unauthorized unless verifier.authentic?(request.body) + end +end |