postman_script.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const appId = ""; // 应用id配置
  2. const appPrivateKey = ""; //应用私钥配置
  3. const alipayPublicKey = ""; //支付宝公钥配置
  4. init(appId, appPrivateKey, alipayPublicKey);
  5. signRequest();
  6. verifyRespnose();
  7. function verifyRespnose() {
  8. if (pm.response) {
  9. check(pm.collectionVariables.get("alipay_public_key"));
  10. }
  11. }
  12. function signRequest() {
  13. if (!pm.response) {
  14. pm.request.headers.add({key: 'alipay-request-id', value: pm.variables.replaceIn('{{$randomUUID}}')});
  15. addAuthHeader(pm.collectionVariables.get("app_id"),pm.collectionVariables.get("app_private_key"),pm.collectionVariables.get("app_cert_sn") );
  16. }
  17. }
  18. function init(appId, privateKey, alipayPublicKey, certSN) {
  19. pm.collectionVariables.set("app_id", appId);
  20. pm.collectionVariables.set("app_private_key", "-----BEGIN PRIVATE KEY----- " + privateKey + " -----END PRIVATE KEY-----");
  21. pm.collectionVariables.set("alipay_public_key", "-----BEGIN PUBLIC KEY----- " + alipayPublicKey + " -----END PUBLIC KEY-----");
  22. pm.collectionVariables.set("app_cert_sn", certSN);
  23. // 加载类库
  24. if (!pm.globals.has("pmlib_code")) {
  25. pm.sendRequest("https://joolfe.github.io/postman-util-lib/dist/bundle.js", (err, res) => {
  26. if (!err) {
  27. pm.globals.set("pmlib_code", res.text())
  28. eval( pm.globals.get('pmlib_code') );
  29. }
  30. });
  31. sleep(5000)
  32. } else {
  33. eval( pm.globals.get('pmlib_code') );
  34. }
  35. }
  36. // 添加认证参数
  37. function addAuthHeader(appId, privateKey, sn) {
  38. const timestamp = new Date().getTime();
  39. var authString = genAuthString(timestamp, appId,sn, pm.variables.replaceIn('{{$randomUUID}}'), 120);
  40. var method = pm.request.method;
  41. var url = pm.request.url.getPathWithQuery();
  42. var body = pm.request.body;
  43. if (pm.request.body.mode == 'formdata') {
  44. body = pm.request.body.formdata.get("data")
  45. pm.request.body.formdata.get("data").type='application/json'
  46. }
  47. var signContent = authString + '\n' + method + '\n' + url + '\n' + body + '\n';
  48. var appAuthToken = pm.request.headers.get("alipay-app-auth-token");
  49. if (appAuthToken) {
  50. signContent += appAuthToken + '\n';
  51. }
  52. const sha256withRSA = new pmlib.rs.KJUR.crypto.Signature({"alg":"SHA256withRSA"});
  53. sha256withRSA.init(privateKey);
  54. sha256withRSA.updateString(signContent);
  55. const sign = pmlib.rs.hextob64(sha256withRSA.sign());
  56. pm.request.headers.add({key: 'authorization', value: "ALIPAY-SHA256withRSA " + authString + ",sign=" + sign })
  57. }
  58. function check(publicKey) {
  59. const timestamp = pm.response.headers.get("alipay-timestamp");
  60. const nonce = pm.response.headers.get("alipay-nonce");
  61. const sign = pm.response.headers.get("alipay-signature");
  62. const body = pm.response.text();
  63. var signContent = timestamp + '\n' + nonce + '\n' + body + '\n';
  64. const sha256withRSA = new pmlib.rs.KJUR.crypto.Signature({"alg":"SHA256withRSA"});
  65. sha256withRSA.init(publicKey);
  66. sha256withRSA.updateString(signContent);
  67. var verified = sha256withRSA.verify(pmlib.rs.b64tohex(sign));
  68. console.log("响应验签结果:" + verified);
  69. pm.expect(verified).to.be.true;
  70. }
  71. function genAuthString(timestamp, appId, sn, nonce, nonceExpireSeconds) {
  72. var authString = "app_id="+appId + ",timestamp=" +timestamp + ",nonce=" + nonce + ",expired_seconds=" + nonceExpireSeconds ;
  73. return authString;
  74. }