我编写了一个接口,用python向内部audible api发出请求。每个API请求都需要使用RSA SHA256签名。
现在,我想用Postman测试API的端点,并使用pre request脚本函数。但是我对javascript并不是很坚定。也许有人能帮我把下面的python函数翻译成Postman脚本:
def sign_request(
request: httpx.Request, adp_token: str, private_key: str
) -> httpx.Request:
"""
Helper function who creates a signed requests for authentication.
:param request: The request to be signed
:param adp_token: the token is obtained after register as device
:param private_key: the rsa key obtained after register as device
:returns: The signed request
"""
method = request.method
path = request.url.path
query = request.url.query
body = request.content.decode("utf-8")
date = datetime.utcnow().isoformat("T") + "Z"
if query:
path += f"?{query}"
data = f"{method}\n{path}\n{date}\n{body}\n{adp_token}"
key = rsa.PrivateKey.load_pkcs1(private_key.encode())
cipher = rsa.pkcs1.sign(data.encode(), key, "SHA-256")
signed_encoded = base64.b64encode(cipher)
signed_header = {
"x-adp-token": adp_token,
"x-adp-alg": "SHA256withRSA:1.0",
"x-adp-signature": f"{signed_encoded.decode()}:{date}"
}
request.headers.update(signed_header)
return request
我发现了如何获取请求方法和正文。我可以使用pm.request.url.getPathWithQuery()
获取路径和查询。为了将头部添加到请求中,我使用了pm.request.headers.add
。
但我不知道如何获得等格式的日期时间,连接字符串和签名的数据。
发布于 2020-11-01 00:32:56
在Postman中实现这一点的问题是,您只能使用sandbox中提供的那些包。因此,您可以使用crypto-js作为加密操作的惟一包助手。
var CryptoJS = require("crypto-js");
var moment = require("moment");
signRequest(pm.request, "yourAdpToken", "yourPrivateKey")
function signRequest(request, adpToken, privateKey) {
const method = request.method;
const path = request.url.getPathWithQuery();
const body = request.body.raw;
const date = moment.utc().format();
const data = `${method}\n${path}\n${date}\n${body}\n${adpToken}`
const hash = CryptoJS.HmacSHA256(data, privateKey);
const signedEncoded = CryptoJS.enc.Base64.stringify(hash);
pm.request.headers.add({
key: 'x-adp-token',
value: adpToken
});
pm.request.headers.add({
key: 'x-adp-alg',
value: 'SHA256withRSA:1.0'
});
pm.request.headers.add({
key: 'x-adp-signature',
value: `${CryptoJS.enc.Base64.parse(signedEncoded)}:${date}`
});
}
将上述代码添加到Pre-request脚本中会将您想要的标头添加到请求中,如下所示:
您可能需要更改编码部分,请检查encode options
https://stackoverflow.com/questions/64619771
复制相似问题