Sha256: 6ae38c45150389a3b01c278fdb85f8e1f684baacb2b3fa3fa01ed5efca6a8cbe

Contents?: true

Size: 1.19 KB

Versions: 16

Compression:

Stored size: 1.19 KB

Contents

module Braintree
  class WebhookNotificationGateway # :nodoc:
    def initialize(gateway)
      @gateway = gateway
      @config = gateway.config
    end

    def parse(signature_string, payload)
      _verify_signature(signature_string, payload)
      attributes = Xml.hash_from_xml(Base64.decode64(payload))
      WebhookNotification._new(@gateway, attributes[:notification])
    end

    def verify(challenge)
      digest = Braintree::Digest.hexdigest(@config.private_key, challenge)
      "#{@config.public_key}|#{digest}"
    end

    def _matching_signature_pair(signature_string)
      signature_pairs = signature_string.split("&")
      valid_pairs = signature_pairs.select { |pair| pair.include?("|") }.map { |pair| pair.split("|") }

      valid_pairs.detect do |public_key, signature|
        public_key == @config.public_key
      end
    end

    def _verify_signature(signature, payload)
      public_key, signature = _matching_signature_pair(signature)
      payload_signature = Braintree::Digest.hexdigest(@config.private_key, payload)

      raise InvalidSignature if public_key.nil?
      raise InvalidSignature unless Braintree::Digest.secure_compare(signature, payload_signature)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
braintree-2.30.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.29.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.28.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.27.1 lib/braintree/webhook_notification_gateway.rb
braintree-2.27.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.26.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.25.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.24.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.23.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.22.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.21.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.20.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.19.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.18.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.17.0 lib/braintree/webhook_notification_gateway.rb
braintree-2.16.0 lib/braintree/webhook_notification_gateway.rb