ExTwilioWebhook

ExTwilioWebhook is a simple library that validates Twilio webhooks in Plug applications. It has been developed for internal use at virtualQ GmbH, but it may come in handy for someone else.

Installation

Add :ex_twilio_webhook to project dependencies in mix.exs.

def deps do
  [
    {:ex_twilio_webhook, github: "moroz/ex_twilio_webhook"}
  ]
end

In order to validate a webhook, the library needs access to both the parsed params and to the unprocessed raw body of the request. However, by default, Plug.Parsers discards the raw body when it parses the params. If your application is using Phoenix, modify the call to Plug.Parsers in your application's endpoint module to add the :body_reader option:

plug Plug.Parsers,
  parsers: [:urlencoded, :json],
  pass: ["*/*"],
  json_decoder: Phoenix.json_library(),
  body_reader: {ExTwilioWebhook.BodyReader, :read_body, []}

Add the ExTwilioWebhook.Plug to the endpoint after Plug.Parsers:

plug ExTwilioWebhook.Plug,
  at: ~r"^/twilio/",
  secret: fn account_sid ->
    do_some_logic(account_sid)
  end,
  public_host: {System, :get_env, ["PUBLIC_HOST"]}

Configuration options: