postman导入json文件(包含签名)

postman导入json文件(包含签名)_第1张图片

{
	"info": {
		"_postman_id": "e2242e4c-69ba-4ce9-ad18-b052c1c24454",
		"name": "诺云放开接口",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "/Third/Tools/checkSign",
			"event": [
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							"\r",
							"var appKey = \"\";\r",
							"var appSecret = \"\";\r",
							"\r",
							"var md5 = calcMd5();\r",
							"var date = new Date().toGMTString();\r",
							"var timestamp =  new Date().getTime();\r",
							"var nonce = createUuid();\r",
							"var textToSign = \"\";\r",
							"var accept = \"*/*\";\r",
							"var contentType = \"\";\r",
							"\r",
							"console.log(\"request\" + JSON.stringify(request));\r",
							"if(request.headers[\"accept\"]){\r",
							"    accept = request.headers[\"accept\"];\r",
							"}\r",
							"if(request.headers[\"content-type\"]){\r",
							"    contentType = request.headers[\"content-type\"];\r",
							"}\r",
							"textToSign += request.method + \"\\n\";\r",
							"textToSign += accept + \"\\n\";\r",
							"textToSign += md5 + \"\\n\";\r",
							"textToSign += contentType + \"\\n\";\r",
							"textToSign += date + \"\\n\";\r",
							"var headers = headersToSign();\r",
							"var signatureHeaders;\r",
							"var sortedKeys = Array.from(headers.keys()).sort()\r",
							"for (var headerName of sortedKeys) {\r",
							"    textToSign += headerName + \":\" + headers.get(headerName) + \"\\n\";\r",
							"    signatureHeaders = signatureHeaders ? signatureHeaders + \",\" + headerName : headerName;\r",
							"}\r",
							"textToSign += urlToSign();\r",
							"console.log(\"textToSign\\n\" + textToSign);\r",
							"var hash = CryptoJS.HmacSHA256(textToSign, appSecret)\r",
							"console.log(\"hash:\" + hash)\r",
							"var signature = hash.toString(CryptoJS.enc.Base64)\r",
							"console.log(\"signature:\" + signature)\r",
							"pm.globals.set('AppKey', appKey);\r",
							"pm.globals.set('Md5', md5);\r",
							"pm.globals.set(\"Date\", date);\r",
							"pm.globals.set(\"Signature\", signature);\r",
							"pm.globals.set(\"SignatureHeaders\", signatureHeaders);\r",
							"pm.globals.set(\"Nonce\", nonce);\r",
							"pm.globals.set(\"Timestamp\", timestamp);\r",
							"\r",
							"function headersToSign() {\r",
							"    var headers = new Map();\r",
							"    for (var name in request.headers) {\r",
							"        //name = name.toLowerCase();\r",
							"        if (!name.startsWith('x-ca-')) {\r",
							"            continue;\r",
							"        } \r",
							"        if (name === \"x-ca-signature\" || name === \"x-ca-signature-headers\" || name == \"x-ca-key\" || name === 'x-ca-nonce' || name === 'x-ca-timestamp') {\r",
							"            continue;\r",
							"        }\r",
							"        var value = request.headers[name];\r",
							"        headers.set(name, value);\r",
							"    }\r",
							"    headers.set('x-ca-key', appKey);\r",
							"    headers.set('x-ca-nonce', nonce);\r",
							"    headers.set('x-ca-timestamp', timestamp)\r",
							"    return headers;\r",
							"}\r",
							"function urlToSign() {\r",
							"    var params = new Map();\r",
							"    var contentType = request.headers[\"content-type\"];\r",
							"    if (contentType && contentType.startsWith('application/x-www-form-urlencoded')) {\r",
							"       for(x in request.data){\r",
							"           params.set(x, request.data[x]);\r",
							"       }\r",
							"    }\r",
							"    var queryParam = pm.request.url.query.members;\r",
							"    console.log(\"request.url\" + JSON.stringify(pm.request.url))\r",
							"    for (let i in queryParam) {\r",
							"        params.set(queryParam[i].key, queryParam[i].value);\r",
							"    }\r",
							"    var sortedKeys = Array.from(params.keys())\r",
							"    sortedKeys.sort();\r",
							"    var url = \"\";\r",
							"    for(var k of pm.request.url.path){\r",
							"        url = url + \"/\" + k;\r",
							"    }\r",
							"    var qs;\r",
							"    for (var k of sortedKeys) {\r",
							"        var s = k + \"=\" + params.get(k);\r",
							"        qs = qs ? qs + \"&\" + s : s;\r",
							"        console.log(\"key=\" + k + \" value=\" + params.get(k));\r",
							"    }\r",
							"    return qs ? url + \"?\" + qs : url;\r",
							"}\r",
							"function calcMd5() {\r",
							"    var contentType = String(request.headers[\"content-type\"]);\r",
							"    console.log(\"data\" + JSON.stringify(request.data));\r",
							"    if (!JSON.stringify(request.data).startsWith('{}') && !contentType.startsWith('application/x-www-form-urlencoded')) {\r",
							"        var data = request.data;\r",
							"        var md5 = CryptoJS.MD5(data);\r",
							"        var md5String = md5.toString(CryptoJS.enc.Base64);\r",
							"        console.log(\"data:\" + data + \"\\nmd5:\" + md5String);\r",
							"        return md5String;\r",
							"    } else {\r",
							"        return \"\";\r",
							"    }\r",
							"}\r",
							"function createUuid() {\r",
							"    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {\r",
							"        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);\r",
							"        return v.toString(16);\r",
							"    });\r",
							"}"
						],
						"type": "text/javascript"
					}
				}
			],
			"protocolProfileBehavior": {
				"disabledSystemHeaders": {
					"accept": true
				}
			},
			"request": {
				"method": "POST",
				"header": [
					{
						"key": "Accept",
						"value": "application/json; charset=utf-8",
						"type": "text"
					},
					{
						"key": "Content-Type",
						"value": "application/x-www-form-urlencoded; charset=utf-8",
						"type": "text"
					},
					{
						"key": "Date",
						"value": "{{Date}}",
						"type": "text"
					},
					{
						"key": "Content-MD5",
						"value": "{{Md5}}",
						"type": "text"
					},
					{
						"key": "X-Ca-Nonce",
						"value": "{{Nonce}}",
						"type": "text"
					},
					{
						"key": "X-Ca-Key",
						"value": "{{AppKey}}",
						"type": "text"
					},
					{
						"key": "X-Ca-Signature",
						"value": "{{Signature}}",
						"type": "text"
					},
					{
						"key": "X-Ca-SignatureMethod",
						"value": "HmacSHA256",
						"type": "text"
					},
					{
						"key": "X-Ca-Signature-Headers",
						"value": "{{SignatureHeaders}}",
						"type": "text"
					},
					{
						"key": "X-Ca-Timestamp",
						"value": "{{Timestamp}}",
						"type": "text"
					}
				],
				"body": {
					"mode": "urlencoded",
					"urlencoded": [
						{
							"key": "username",
							"value": "xiaoming",
							"type": "text"
						},
						{
							"key": "password",
							"value": "123456789",
							"type": "text"
						}
					]
				},
				"url": {
					"raw": "https://gateway.nuoyunlive.com/Third/Tools/checkSign",
					"protocol": "https",
					"host": [
						"gateway",
						"nuoyunlive",
						"com"
					],
					"path": [
						"Third",
						"Tools",
						"checkSign"
					]
				}
			},
			"response": []
		}
	]
}

postman导入json文件(包含签名)_第2张图片
postman导入json文件(包含签名)_第3张图片
postman导入json文件(包含签名)_第4张图片
postman导入json文件(包含签名)_第5张图片


var appKey = "";
var appSecret = "";

var md5 = calcMd5();
var date = new Date().toGMTString();
var timestamp =  new Date().getTime();
var nonce = createUuid();
var textToSign = "";
var accept = "*/*";
var contentType = "";

console.log("request" + JSON.stringify(request));
if(request.headers["accept"]){
    accept = request.headers["accept"];
}
if(request.headers["content-type"]){
    contentType = request.headers["content-type"];
}
textToSign += request.method + "\n";
textToSign += accept + "\n";
textToSign += md5 + "\n";
textToSign += contentType + "\n";
textToSign += date + "\n";
var headers = headersToSign();
var signatureHeaders;
var sortedKeys = Array.from(headers.keys()).sort()
for (var headerName of sortedKeys) {
    textToSign += headerName + ":" + headers.get(headerName) + "\n";
    signatureHeaders = signatureHeaders ? signatureHeaders + "," + headerName : headerName;
}
textToSign += urlToSign();
console.log("textToSign\n" + textToSign);
var hash = CryptoJS.HmacSHA256(textToSign, appSecret)
console.log("hash:" + hash)
var signature = hash.toString(CryptoJS.enc.Base64)
console.log("signature:" + signature)
pm.globals.set('AppKey', appKey);
pm.globals.set('Md5', md5);
pm.globals.set("Date", date);
pm.globals.set("Signature", signature);
pm.globals.set("SignatureHeaders", signatureHeaders);
pm.globals.set("Nonce", nonce);
pm.globals.set("Timestamp", timestamp);

function headersToSign() {
    var headers = new Map();
    for (var name in request.headers) {
        //name = name.toLowerCase();
        if (!name.startsWith('x-ca-')) {
            continue;
        } 
        if (name === "x-ca-signature" || name === "x-ca-signature-headers" || name == "x-ca-key" || name === 'x-ca-nonce' || name === 'x-ca-timestamp') {
            continue;
        }
        var value = request.headers[name];
        headers.set(name, value);
    }
    headers.set('x-ca-key', appKey);
    headers.set('x-ca-nonce', nonce);
    headers.set('x-ca-timestamp', timestamp)
    return headers;
}
function urlToSign() {
    var params = new Map();
    var contentType = request.headers["content-type"];
    if (contentType && contentType.startsWith('application/x-www-form-urlencoded')) {
       for(x in request.data){
           params.set(x, request.data[x]);
       }
    }
    var queryParam = pm.request.url.query.members;
    console.log("request.url" + JSON.stringify(pm.request.url))
    for (let i in queryParam) {
        params.set(queryParam[i].key, queryParam[i].value);
    }
    var sortedKeys = Array.from(params.keys())
    sortedKeys.sort();
    var url = "";
    for(var k of pm.request.url.path){
        url = url + "/" + k;
    }
    var qs;
    for (var k of sortedKeys) {
        var s = k + "=" + params.get(k);
        qs = qs ? qs + "&" + s : s;
        console.log("key=" + k + " value=" + params.get(k));
    }
    return qs ? url + "?" + qs : url;
}
function calcMd5() {
    var contentType = String(request.headers["content-type"]);
    console.log("data" + JSON.stringify(request.data));
    if (!JSON.stringify(request.data).startsWith('{}') && !contentType.startsWith('application/x-www-form-urlencoded')) {
        var data = request.data;
        var md5 = CryptoJS.MD5(data);
        var md5String = md5.toString(CryptoJS.enc.Base64);
        console.log("data:" + data + "\nmd5:" + md5String);
        return md5String;
    } else {
        return "";
    }
}
function createUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}

你可能感兴趣的:(postman)