Sha256: db0b942e9144fea33a42cc9e83dfe63317b26601b771a2e0fed0dc4f71dfc203

Contents?: true

Size: 864 Bytes

Versions: 16

Compression:

Stored size: 864 Bytes

Contents

require 'base64'
require 'openssl'

module Spree
  module Webhooks
    class EventSignature
      def initialize(event, payload)
        @event = event
        @payload = payload
      end

      # Generates a base64-encoded HMAC SHA256 signature for the payload of the event.
      #
      # By using the stringified payload, the signature is made tamper-proof as any
      # alterations of the data during transit will lead to an incorrect signature
      # comparison by the client.
      #
      # @return [String] The computed signature
      def computed_signature
        @computed_signature ||=
          Base64.strict_encode64(
            OpenSSL::HMAC.digest('sha256', @event.subscriber.secret_key, payload)
          )
      end

      private

      def payload
        @payload.is_a?(String) ? @payload : @payload.to_json
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
spree_api-4.10.1 app/models/spree/webhooks/event_signature.rb
spree_api-4.10.0 app/models/spree/webhooks/event_signature.rb
spree_api-4.9.0 app/models/spree/webhooks/event_signature.rb
spree_api-4.8.3 app/models/spree/webhooks/event_signature.rb
spree_api-4.8.2 app/models/spree/webhooks/event_signature.rb
spree_api-4.7.3 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.6 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.5 app/models/spree/webhooks/event_signature.rb
spree_api-4.7.2 app/models/spree/webhooks/event_signature.rb
spree_api-4.7.1 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.4 app/models/spree/webhooks/event_signature.rb
spree_api-4.7.0 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.3 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.2 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.1 app/models/spree/webhooks/event_signature.rb
spree_api-4.6.0 app/models/spree/webhooks/event_signature.rb