Sha256: af77fc6fbad0ebb80a9c015b911ccc140b03683bd363d0cebf17757d1c516193

Contents?: true

Size: 1.75 KB

Versions: 18

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require_relative 'webhook_event'

# ref: https://developer.intuit.com/app/developer/qbo/docs/develop/webhooks/managing-webhooks-notifications#validating-the-notification
module LedgerSync
  module Ledgers
    module TestLedger
      class Webhook
        attr_reader :notifications,
                    :original_payload,
                    :payload

        def initialize(payload:)
          @original_payload = payload
          @payload = payload.is_a?(String) ? JSON.parse(payload) : payload

          event_notifications_payload = @payload['eventNotifications']
          raise 'Invalid payload: Could not find eventNotifications' unless event_notifications_payload.is_a?(Array)

          @notifications = []

          event_notifications_payload.each do |event_notification_payload|
            @notifications << WebhookNotification.new(
              payload: event_notification_payload,
              webhook: self
            )
          end
        end

        def events
          notifications.map(&:events)
        end

        def resources
          @resources ||= notifications.map(&:resources).flatten.compact
        end

        def valid?(signature:, verification_token:)
          self.class.valid?(
            payload: payload.to_json,
            signature: signature,
            verification_token: verification_token
          )
        end

        def self.valid?(payload:, signature:, verification_token:)
          raise 'Cannot verify non-String payload' unless payload.is_a?(String)

          digest = OpenSSL::Digest.new('sha256')
          hmac = OpenSSL::HMAC.digest(digest, verification_token, payload)
          base64 = Base64.encode64(hmac).strip
          base64 == signature
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ledger_sync-3.0.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.6.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.5.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.4.4 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.3.1 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.2.3 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.2.1 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.2.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.0.2 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.0.1 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.0.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.8.1 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.8.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.7.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.6.0 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.5.2 lib/ledger_sync/test/support/test_ledger/webhook.rb
ledger_sync-1.5.1 lib/ledger_sync/test/support/test_ledger/webhook.rb