Skip to content

Instantly share code, notes, and snippets.

@chimame
Created January 27, 2015 03:25
Show Gist options
  • Save chimame/ef195ec328e608023abd to your computer and use it in GitHub Desktop.
Save chimame/ef195ec328e608023abd to your computer and use it in GitHub Desktop.
Twilio Request Validation for Rails
class TwilioBaseController < ActionController::Base
before_filter :authenticate_twilio_request
private
def authenticate_twilio_request
twilio_sig = request.headers['HTTP_X_TWILIO_SIGNATURE']
render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text and return if twilio_sig.blank?
twilio_validator = Twilio::Util::RequestValidator.new("YOUR TWILIO AUTH TOKEN")
params_for_auth = (request.post? ? env['rack.request.form_hash'] : {})
uri_for_auth = request.original_url
is_valid_twilio_req = twilio_validator.validate(uri_for_auth, params_for_auth, twilio_sig)
unless is_valid_twilio_req
render :xml => (Twilio::TwiML::Response.new {|r| r.Hangup}).text
false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment