Skip to content

Instantly share code, notes, and snippets.

@remy727
Created January 30, 2024 18:32
Show Gist options
  • Save remy727/9f207fb2cfa604d325b2850a708c1701 to your computer and use it in GitHub Desktop.
Save remy727/9f207fb2cfa604d325b2850a708c1701 to your computer and use it in GitHub Desktop.
App Proxy Verification
module AppProxyVerification
extend ActiveSupport::Concern
included do
skip_before_action :verify_authenticity_token, raise: false
before_action :verify_request
end
private
def verify_request
query_parameters = Rack::Utils.parse_query(request.query_string)
return head(:unauthorized) unless hmac_valid?(query_parameters)
end
def hmac_valid?(query_parameters)
signature = query_parameters.delete("signature")
sorted_params = query_parameters.collect{ |k, v| "#{k}=#{Array(v).join(',')}" }.sort.join
ActiveSupport::SecurityUtils.secure_compare(
signature,
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), ENV['SHOPIFY_API_SECRET'], sorted_params)
)
end
end
class Api::ReviewsController < ApplicationController
include AppProxyVerification
def create
# do your work
render(json: { success: true }, status: 200)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment