Created
January 27, 2015 03:25
-
-
Save chimame/ef195ec328e608023abd to your computer and use it in GitHub Desktop.
Twilio Request Validation for Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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