如何要求PHP.serialize能够验证Webhook?(Ruby on Rails 5)

use*_*den 4 php ruby ruby-on-rails webhooks

我正在使用Ruby on Rails 5和ruby -v 2.5.3。我正在尝试验证一个Webhook,示例显示:

require 'base64'
require 'php_serialize'
require 'openssl'


public_key = '-----BEGIN PUBLIC KEY-----
MIICIjANBgkqh...'

# 'data' represents all of the POST fields sent with the request.
# Get the p_signature parameter & base64 decode it.
signature = Base64.decode64(data['p_signature'])

# Remove the p_signature parameter
data.delete('p_signature')

# Ensure all the data fields are strings
data.each {|key, value|data[key] = String(value)}

# Sort the data
data_sorted = data.sort_by{|key, value| key}

# and serialize the fields
# serialization library is available here: https://github.com/jqr/php-serialize
data_serialized = PHP.serialize(data_sorted, true)

# verify the data
digest    = OpenSSL::Digest::SHA1.new
pub_key   = OpenSSL::PKey::RSA.new(public_key).public_key
verified  = pub_key.verify(digest, signature, data_serialized)

if verified
    puts "Yay! Signature is valid!"
else
    puts "The signature is invalid!"
end
Run Code Online (Sandbox Code Playgroud)

我的问题是php.serialize,我尝试使用了gem:https : //github.com/jqr/php-serialize,但是它不支持ruby -v 2.5.3。(例如由于:https : //github.com/jqr/php-serialize/issues/16

我如何在Rails应用程序中要求“ php_serialize”?

Con*_*nor 6

似乎Fixnum不推荐使用的警告已在此处的PR中修复。最新的1.2版落后于master,并且没有几处更改。

如果您担心警告,一种选择是您可以通过gemfile中的ref获取最新信息。

如何从git仓库安装gems

   gem 'php-serialize', git: 'https://github.com/jqr/php-serialize.git', ref: '31dde87'
Run Code Online (Sandbox Code Playgroud)

除此之外,在一些快速测试中,我没有发现PHP-Serialize gem有多大错误。您的代码段是否存在任何特定问题?您能否提供其他详细信息/错误?