Sha256: 7ae9f95bdc4e2cf2ac03fb52480ab788b1154ef2637299b45c9dbaf22ec564e9

Contents?: true

Size: 1.17 KB

Versions: 13

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module Onfido
  class Webhook < Resource
    def create(url:, **payload)
      payload[:url] = url
      post(path: 'webhooks', payload: payload)
    end

    def find(webhooks_id)
      get(path: "webhooks/#{webhooks_id}")
    end

    def all
      get(path: 'webhooks')
    end

    def destroy(webhook_id)
      delete(path: "webhooks/#{webhook_id}")
    end

    # As well as being a normal resource, Onfido::Webhook also supports
    # verifying the authenticity of a webhook by comparing the signature on the
    # request to one computed from the body
    def self.valid?(request_body, request_signature, token)
      if [request_body, request_signature, token].any?(&:nil?)
        raise ArgumentError, 'A request body, request signature and token ' \
                             'must be provided'
      end

      computed_signature = generate_signature(request_body, token)
      Rack::Utils.secure_compare(request_signature, computed_signature)
    end

    def self.generate_signature(request_body, token)
      OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), token, request_body)
    end
    private_class_method :generate_signature
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
onfido-2.9.0 lib/onfido/resources/webhook.rb
onfido-2.8.0 lib/onfido/resources/webhook.rb
onfido-2.7.0 lib/onfido/resources/webhook.rb
onfido-2.6.0 lib/onfido/resources/webhook.rb
onfido-2.5.0 lib/onfido/resources/webhook.rb
onfido-2.4.0 lib/onfido/resources/webhook.rb
onfido-2.3.0 lib/onfido/resources/webhook.rb
onfido-2.2.0 lib/onfido/resources/webhook.rb
onfido-2.1.1 lib/onfido/resources/webhook.rb
onfido-2.1.0 lib/onfido/resources/webhook.rb
onfido-2.0.2 lib/onfido/resources/webhook.rb
onfido-2.0.1 lib/onfido/resources/webhook.rb
onfido-2.0.0 lib/onfido/resources/webhook.rb