Skip to content

Instantly share code, notes, and snippets.

@tlemburg
Created October 2, 2020 18:49
Show Gist options
  • Save tlemburg/fe8242de603deae05b42730244d0ac58 to your computer and use it in GitHub Desktop.
Save tlemburg/fe8242de603deae05b42730244d0ac58 to your computer and use it in GitHub Desktop.
Cognito webhook receiver v2
require 'sinatra'
require 'json'
require 'openssl'
require 'base64'
WEBHOOK_KEY = 'webhook_sandbox_key_19a08a7f7f181eadc45a188f815aa4f0'
WEBHOOK_SECRET = '43e042f238733fc892d9bbb06b564a023999d116a7d345c51c48611ba81e4ba4'
get '/' do
'hello world'
end
post '/' do
'hellow owlrd'
end
post '/webhook' do
puts ""
puts 'Webhook received!'
puts ""
json = request.body.read
puts "JSON Hash From Body:"
puts JSON.parse(json)
puts ""
puts ""
puts "Date header:"
puts request.env["HTTP_DATE"]
puts "Digest header:"
puts request.env['HTTP_DIGEST']
puts "Authorization header:"
puts request.env["HTTP_AUTHORIZATION"]
puts ""
puts ""
puts "Verifying signature:"
puts "Signature in header:"
header_sig = request.env['HTTP_AUTHORIZATION'].split('signature="').last[0..-2]
puts header_sig
puts "Computed signature using secret:"
signing_string = [
"(request-target): post /webhook",
"date: #{request.env["HTTP_DATE"]}",
"digest: #{request.env["HTTP_DIGEST"]}"
].join("\n")
computed_sig = Base64.strict_encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::SHA256.new, WEBHOOK_SECRET, signing_string
)
)
puts computed_sig
if header_sig == computed_sig
puts "They match!"
else
puts "THEY DO NOT MATCH!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment