php - 如何要求 PHP.serialize 能够验证 webhook?(Ruby on Rails 5)
问题描述
我正在使用 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
我的问题是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”?
解决方案
看起来 Fixnum deprecated 警告已在此处的 PR 中修复。最新版本 1.2 落后于 master 并且不包含一些更改。
如果您担心警告,一种选择是您可以通过 gemfile 中的 ref 获取最新的。
gem 'php-serialize', git: 'https://github.com/jqr/php-serialize.git', ref: '31dde87'
除此之外,我在一些快速测试中没有看到 PHP-Serialize gem 有什么问题。您对代码片段有任何具体问题吗?您能否提供其他详细信息/错误?
推荐阅读
- docker - l2 cni插件与l3插件有什么区别?
- wpf - 禁用时更改 RadioButton 文本的不透明度
- apache-kafka-streams - Kafka Streams DSL 谓词流分离
- api - Laravel 5.7:API 请求与否?
- python - 与使用 Anaconda Prompt 运行相比,从 Visual Studio 启动 Python 文件的工作方式不同
- kdb - 使用递归求解 q 中的 Euler 18
- mysql - SQL Query group by,有最小值,不选择最小值?
- machine-learning - 在矩阵分解中设置电影评分限制
- java - 在将对象添加到数组时动态增加数组长度
- c# - MVC 模型在德国日期更改日期和月份