Class: OpenLoam::WebhookDeliveryJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/open_loam/webhook_delivery_job.rb

Overview

Delivers one event to one endpoint. Inherits ActiveJob::Base rather than the host app's ApplicationJob: the gem must not depend on a class the app owns and may have configured for its own retries.

Signing: the receiver recomputes HMAC-SHA256 of the exact body with the endpoint's secret and compares it to X-OpenLoam-Signature. Body building and signing are class methods so both sides — and the tests — can call them without a network.

Constant Summary collapse

TIMEOUT_SECONDS =
5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.body_for(event_name, payload, tenant_id) ⇒ Object



22
23
24
# File 'app/jobs/open_loam/webhook_delivery_job.rb', line 22

def self.body_for(event_name, payload, tenant_id)
  JSON.generate(event: event_name, payload: payload, tenant_id: tenant_id)
end

.signature(secret, body) ⇒ Object



26
27
28
# File 'app/jobs/open_loam/webhook_delivery_job.rb', line 26

def self.signature(secret, body)
  "sha256=#{OpenSSL::HMAC.hexdigest('SHA256', secret.to_s, body)}"
end

Instance Method Details

#perform(tenant_id, endpoint_id, event_name, payload) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/jobs/open_loam/webhook_delivery_job.rb', line 30

def perform(tenant_id, endpoint_id, event_name, payload)
  tenant = OpenLoam::Tenant.find_by(id: tenant_id)
  return if tenant.nil?

  OpenLoam.as_tenant(tenant) do
    endpoint = OpenLoam::WebhookEndpoint.find_by(id: endpoint_id)
    # The endpoint may have been deleted or switched off between enqueue
    # and delivery — that is an answer, not an error.
    next if endpoint.nil? || !endpoint.active?

    deliver(endpoint, self.class.body_for(event_name, payload, tenant_id), event_name)
  end
end