Skip to content

Instantly share code, notes, and snippets.

@pjg
Forked from joost/webhooks_controller.rb
Created February 4, 2021 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjg/2cedf1619e09ab1c6d6455572f0f75e9 to your computer and use it in GitHub Desktop.
Save pjg/2cedf1619e09ab1c6d6455572f0f75e9 to your computer and use it in GitHub Desktop.
Mandrill API Webhook signature verification. This shows how you could verify a Mandrill signature in a Rails Controller.
class WebhooksController < ActionController::Base
WEBHOOK_KEY = "some_key" # You could also use an API request to lookup the key
before_filter :verify_request_signature
# See: http://help.mandrill.com/entries/23704122-Authenticating-webhook-requests
def verify_request_signature
signed_data = request.url
post_params = request.request_parameters.dup # POST parameters
signed_data += request.request_parameters.sort.join
signature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha1',WEBHOOK_KEY,signed_data))
logger.debug("our: #{signature}, mandrill: #{request.headers['X-Mandrill-Signature']}")
# Do something here.. compare them..
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment