Class: DiscoApp::WebhookService

Inherits:
Object
  • Object
show all
Defined in:
app/services/disco_app/webhook_service.rb

Class Method Summary collapse

Class Method Details

.calculated_hmac(body, secret) ⇒ Object

Calculate the HMAC for the given data and secret.



10
11
12
13
# File 'app/services/disco_app/webhook_service.rb', line 10

def self.calculated_hmac(body, secret)
  digest = OpenSSL::Digest.new('sha256')
  Base64.encode64(OpenSSL::HMAC.digest(digest, secret, body)).strip
end

.find_job_class(topic) ⇒ Object

Try to find a job class for the given webhook topic.



16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/disco_app/webhook_service.rb', line 16

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

.valid_hmac?(body, secret, hmac_to_verify) ⇒ Boolean

Return true iff the provided hmac_to_verify matches that calculated from the given data and secret.

Returns:

  • (Boolean)


5
6
7
# File 'app/services/disco_app/webhook_service.rb', line 5

def self.valid_hmac?(body, secret, hmac_to_verify)
  ActiveSupport::SecurityUtils.secure_compare(calculated_hmac(body, secret), hmac_to_verify.to_s)
end