Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/square/utilities/webhooks_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ def self.is_valid_webhook_event_signature(request_body, signature_header, signat
raise 'signature_key is null or empty' if signature_key.nil? || signature_key.empty?
raise 'notification_url is null or empty' if notification_url.nil? || notification_url.empty?

# Perform UTF-8 encoding to bytes
payload_bytes = "#{notification_url}#{request_body}".force_encoding('utf-8')
signature_key_bytes = signature_key.force_encoding('utf-8')
# Prepare the message as it was signed by the sender
message = "#{notification_url}#{request_body}"

# Compute the hash value
hmac = OpenSSL::HMAC.digest('sha256', signature_key_bytes, payload_bytes)
hmac = OpenSSL::HMAC.digest('sha256', signature_key, message)

# Compare the computed hash vs the value in the signature header
hash_base64 = Base64.strict_encode64(hmac)
Expand Down
2 changes: 2 additions & 0 deletions test/webhooks/test_webhooks_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../../lib/square/utilities/webhooks_helper.rb'
require 'minitest/autorun'
require 'minitest/hell'
Expand Down