【发布时间】:2019-03-09 10:51:18
【问题描述】:
我有一个 Rails 应用程序,它提供了一个由 python 脚本使用的 JSON API。安全性很重要,我一直在使用 HMAC 来做到这一点。 rails 应用程序和 python 脚本都知道密钥和他们用它加密的签名是 URL 和请求的正文。
我的问题是请求的签名不会每次都改变。如果它被拦截,那么攻击者可以发送具有相同摘要的完全相同的请求,我认为它会进行身份验证,尽管攻击者不知道密钥。
所以我认为我需要在签名中包含请求的时间戳之类的东西 - 问题是我不知道如何在 python 和 ruby 中得到它。
这是我的python代码:
import hmac
import hashlib
import requests
fetch_path = url_base + '/phone_messages/pending'
fetch_body = '{}'
fetch_signature = fetch_path + ':' + fetch_body
fetch_hmac = hmac.new(api_key.encode('utf-8'), fetch_signature.encode('utf-8'), haslib.sha1).hexdigest()
这是我的红宝石代码:
signature = "#{request.url}:#{request.body.to_json.to_s}"
hmac_digest = OpenSSL::HMAC.hexdigest('sha1', secret_key, signature)
【问题讨论】:
-
有一个错字,缺少
fetch_body。您能否确认,{request.body}在您的测试用例中发生了变化? -
@stovfl 抱歉,我会改正错字
标签: python ruby-on-rails http python-requests hmac