Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example generate signature #913

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/stripe/webhook.rb
Expand Up @@ -24,10 +24,14 @@ def self.construct_event(payload, sig_header, secret,
module Signature
EXPECTED_SCHEME = "v1"

def self.compute_signature(payload, secret)
# Compute a signature for a given payload, header and secret
def self.compute_signature(payload, header, secret)
timestamp, _signatures =
get_timestamp_and_signatures(header, EXPECTED_SCHEME)

signed_payload = "#{timestamp}.#{payload}"
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, payload)
end
private_class_method :compute_signature

# Extracts the timestamp and the signature(s) with the desired scheme
# from the header
Expand Down Expand Up @@ -67,8 +71,8 @@ def self.verify_header(payload, header, secret, tolerance: nil)
)
end

signed_payload = "#{timestamp}.#{payload}"
expected_sig = compute_signature(signed_payload, secret)

expected_sig = compute_signature(payload, header, secret)
unless signatures.any? { |s| Util.secure_compare(expected_sig, s) }
raise SignatureVerificationError.new(
"No signatures found matching the expected signature for payload",
Expand All @@ -85,6 +89,7 @@ def self.verify_header(payload, header, secret, tolerance: nil)

true
end

end
end
end