Class: Rack::TwilioWebhookAuthentication
- Inherits:
-
Object
- Object
- Rack::TwilioWebhookAuthentication
- Defined in:
- lib/rack/twilio_webhook_authentication.rb
Overview
Middleware that authenticates webhooks from Twilio using the request validator.
The middleware takes an auth token with which to set up the request validator and any number of paths. When a path matches the incoming request path, the request will be checked for authentication.
Example:
require 'rack' use Rack::TwilioWebhookAuthentication, ENV, //messages/
The above appends this middleware to the stack, using an auth token saved in the ENV and only against paths that match //messages/. If the request validates then it gets passed on to the action as normal. If the request doesn't validate then the middleware responds immediately with a 403 status.
Webhooks signed with a SharedKey (see the Webhooks Rules API) carry an X-Twilio-Webhooks-Signature-Settings header naming the signing algorithm, and are signed with the SharedKey secret instead of the auth token. Pass it with the shared_key_secret option:
use Rack::TwilioWebhookAuthentication, ENV, //voice/, shared_key_secret: ENV
Requests without that header are still validated with the auth token and HMAC-SHA1. A request with the header but no shared_key_secret configured gets a 500, as the middleware cannot validate it.
Constant Summary collapse
- FORM_URLENCODED_MEDIA_TYPE =
Rack's FORM_DATA_MEDIA_TYPES can be modified to taste, so we're slightly more conservative in what we consider form data.
Rack::MediaType.type('application/x-www-form-urlencoded')
- SIGNATURE_SETTINGS_ENV_KEY =
'HTTP_X_TWILIO_WEBHOOKS_SIGNATURE_SETTINGS'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, auth_token, *paths, shared_key_secret: nil, &auth_token_lookup) ⇒ TwilioWebhookAuthentication
constructor
A new instance of TwilioWebhookAuthentication.
Constructor Details
#initialize(app, auth_token, *paths, shared_key_secret: nil, &auth_token_lookup) ⇒ TwilioWebhookAuthentication
Returns a new instance of TwilioWebhookAuthentication.
41 42 43 44 45 46 47 |
# File 'lib/rack/twilio_webhook_authentication.rb', line 41 def initialize(app, auth_token, *paths, shared_key_secret: nil, &auth_token_lookup) @app = app @auth_token = auth_token @shared_key_secret = shared_key_secret define_singleton_method(:get_auth_token, auth_token_lookup) if block_given? @path_regex = Regexp.union(paths) end |
Instance Method Details
#call(env) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rack/twilio_webhook_authentication.rb', line 49 def call(env) return @app.call(env) unless protected_path?(env['PATH_INFO']) request = Rack::Request.new(env) original_url = request.url params = extract_params!(request) signature_settings = env[SIGNATURE_SETTINGS_ENV_KEY] secret = signature_settings ? @shared_key_secret : (@auth_token || get_auth_token(params['AccountSid'])) return missing_shared_key_secret_response if secret.nil? validator = Twilio::Security::RequestValidator.new(secret) signature = env['HTTP_X_TWILIO_SIGNATURE'] || '' algorithm = Twilio::Security::RequestValidator.signature_algorithm(signature_settings) if validator.validate(original_url, params, signature, algorithm) @app.call(env) else [ 403, { 'Content-Type' => 'text/plain' }, ['Twilio Request Validation Failed.'] ] end end |