我的文件夹是:
app/
- -controllers/
- -shopify_app/
- -webhooks_controller.rb
lib/
- -shopify_app/
- -controller_concerns/
- -webhook_verification.rb
webhook_verification.rb 代码是:
module ShopifyApp
module WebhookVerification
extend ActiveSupport::Concern
included do
skip_before_action :verify_authenticity_token, raise: false
before_action :verify_request
end
private
def verify_request
data = request.raw_post
return head :unauthorized unless hmac_valid?(data)
end
def hmac_valid?(data)
secret = ShopifyApp.configuration.secret
digest = OpenSSL::Digest.new('sha256')
ActiveSupport::SecurityUtils.secure_compare(
shopify_hmac,
Base64.encode64(OpenSSL::HMAC.digest(digest, secret, data)).strip
)
end
def shop_domain
request.headers['HTTP_X_SHOPIFY_SHOP_DOMAIN']
end
def shopify_hmac
request.headers['HTTP_X_SHOPIFY_HMAC_SHA256']
end
end
end
我想在 webhooks_controller.rb 中包含 webhook_verification.rb 执行“包含 ShopifyApp::WebhookVerification” ,但它不起作用。
如何加载 lib 文件夹?
注意:我试过在我的 config/application.rb 上写这个,但它似乎不起作用
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]