Sha256: 408f5c1a3caf4e761d580eb7fd3a98a1c9213c93b9a46ad987eaeb674a32db4e

Contents?: true

Size: 930 Bytes

Versions: 7

Compression:

Stored size: 930 Bytes

Contents

class DiscoApp::WebhookService

  # Return true iff the provided hmac_to_verify matches that calculated from the
  # given data and secret.
  def self.valid_hmac?(body, secret, hmac_to_verify)
    ActiveSupport::SecurityUtils.secure_compare(calculated_hmac(body, secret), hmac_to_verify.to_s)
  end

  # Calculate the HMAC for the given data and secret.
  def self.calculated_hmac(body, secret)
    digest = OpenSSL::Digest.new('sha256')
    Base64.encode64(OpenSSL::HMAC.digest(digest, secret, body)).strip
  end

  # Try to find a job class for the given webhook topic.
  def self.find_job_class(topic)
    # First try to find a top-level matching job class.
    "#{topic}_job".tr('/', '_').classify.constantize
  rescue NameError
    # If that fails, try to find a DiscoApp:: prefixed job class.
    begin
      %(DiscoApp::#{"#{topic}_job".tr('/', '_').classify}).constantize
    rescue NameError
      nil
    end
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
disco_app-0.17.0 app/services/disco_app/webhook_service.rb
disco_app-0.18.0 app/services/disco_app/webhook_service.rb
disco_app-0.18.2 app/services/disco_app/webhook_service.rb
disco_app-0.18.3 app/services/disco_app/webhook_service.rb
disco_app-0.18.6 app/services/disco_app/webhook_service.rb
disco_app-0.18.4 app/services/disco_app/webhook_service.rb
disco_app-0.18.1 app/services/disco_app/webhook_service.rb