Skip to content

Instantly share code, notes, and snippets.

@aonemd
Created December 31, 2024 11:27
Show Gist options
  • Save aonemd/ddb999351e4571d4610a4ba2360c274f to your computer and use it in GitHub Desktop.
Save aonemd/ddb999351e4571d4610a4ba2360c274f to your computer and use it in GitHub Desktop.
A script to generate request signature for Pusher channels API
require 'digest'
require 'openssl'
app_id = 'app_id'
auth_key = 'key' # `key` in Pusher.com dashboard
secret_key = 'secret' # `secret` in Pusher.com dashboard
auth_timestamp = Time.now.to_i
auth_version = '1.0'
method = 'POST'
event = "my-event"
channel = "my-channel"
path = "/apps/#{ app_id }/events"
body_str = "{\"name\":\"#{event}\",\"channels\":[\"#{channel}\"],\"data\":\"{\\\"message\\\":\\\"hello from curl\\\"}\"}"
# Generate MD5 hash of the body
body_md5 = Digest::MD5.hexdigest(body_str)
# Create the string to sign
auth_signing = "#{method}\n#{path}\nauth_key=#{auth_key}&auth_timestamp=#{auth_timestamp}&auth_version=#{auth_version}&body_md5=#{body_md5}"
# Generate HMAC-SHA256 signature
auth_signature = OpenSSL::HMAC.hexdigest('sha256', secret_key, auth_signing)
puts auth_signature
puts `curl -H "Content-Type: application/json" -d '{\"name\":\"#{event}\",\"channels\":[\"#{channel}\"],\"data\":\"{\\\"message\\\":\\\"hello from curl\\\"}\"}' "http://api.pusherapp.com/apps/#{app_id}/events?auth_key=#{auth_key}&auth_timestamp=#{ auth_timestamp }&auth_version=1.0&body_md5=#{body_md5}&auth_signature=#{auth_signature}"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment