Skip to content

Instantly share code, notes, and snippets.

@cowholio4
Last active December 28, 2017 05:16
Show Gist options
  • Save cowholio4/e1761a7eba3849deb3482a51638a427e to your computer and use it in GitHub Desktop.
Save cowholio4/e1761a7eba3849deb3482a51638a427e to your computer and use it in GitHub Desktop.
Example Seclytics webhooks controller for Ruby on Rails.
class Webhooks::SeclyticsController < ApplicationController
skip_before_filter :require_login, :strict_transport_security
def create
# check if verification
hook_secret = request.headers['X-Hook-Secret']
if hook_secret == Settings.seclytics_webhook_shared_secret
response.headers['X-Hook-Secret'] = hook_secret
return render plain: "OK", status: 201
end
hook_signature = request.headers['X-Hook-Signature']
if hook_signature.present?
# check if signature valid
calculated_signature = OpenSSL::HMAC.hexdigest('sha1', Settings.seclytics_access_token, request.raw_post)
if calculated_signature != hook_signature
logger.info("Hook Signature: " + hook_signature)
logger.info("Bad Signautre:" + calculated_signature )
return render plain: 'FORBIDDEN', status: :forbidden
end
if process_webhook
# process the content
return render plain: "", status: 204
else
return render plain: "COULD NOT PROCESS", status: 500
end
end
render plain: 'FORBIDDEN', status: :forbidden
end
def process_webhook
if not params.has_key? :payload
return false
end
# all the events are in the payload each has a type and a value
# in this example we are simply storing all the IPs as Spam
ips = params[:payload].keep_if{|x| x['type'] == 'ip'}.map{|x| IPAddr.new(x['value']).to_i}.uniq
ips.each do |i|
SpamIp.where(ip: i).first_or_create
end
return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment