class Clearbit::Webhook

Public Class Methods

clearbit_key() click to toggle source
# File lib/clearbit/webhook.rb, line 7
def self.clearbit_key
  Clearbit.key!
end
generate_signature(key, body) click to toggle source
# File lib/clearbit/webhook.rb, line 25
def self.generate_signature(key, body)
  signed_body = body
  signed_body = JSON.dump(signed_body) unless signed_body.is_a?(String)
  'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), key, signed_body)
end
new(env, key = nil) click to toggle source
# File lib/clearbit/webhook.rb, line 31
def initialize(env, key = nil)
  request = Rack::Request.new(env)

  request.body.rewind

  signature = request.env['HTTP_X_REQUEST_SIGNATURE']
  body      = request.body.read

  self.class.valid!(signature, body, key)

  merge!(JSON.parse(body))
end
valid!(signature, body, key = nil) click to toggle source
# File lib/clearbit/webhook.rb, line 21
def self.valid!(signature, body, key = nil)
  valid?(signature, body, key) ? true : raise(Errors::InvalidWebhookSignature.new)
end
valid?(request_signature, body, key = nil) click to toggle source
# File lib/clearbit/webhook.rb, line 11
def self.valid?(request_signature, body, key = nil)
  return false unless request_signature && body

  # The global Clearbit.key can be overriden for multi-tenant apps using multiple Clearbit keys
  key = (key || clearbit_key).gsub(/\A(pk|sk)_/, '')

  signature = generate_signature(key, body)
  Rack::Utils.secure_compare(request_signature, signature)
end