gc_rsa 1.3.2 → 1.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +10 -0
- package/README.md +2 -2
- package/package.json +3 -3
- package/src/adapter/fetch.js +7 -0
- package/src/adapter/fly.js +19 -0
- package/src/adapter/index.js +5 -0
- package/src/adapter/uni.js +23 -0
- package/src/adapter/wx.js +23 -0
- package/src/index.js +56 -0
- package/src/lib/crypto.js +20 -0
- package/src/lib/obj.js +22 -0
- package/src/lib/signature.js +79 -0
- package/src/lib/sort.js +17 -0
- package/src/lib/transformHeaders.js +72 -0
- package/.babelrc +0 -4
- package/.eslintrc +0 -11
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gc_rsa",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"main": "
|
|
3
|
+
"version": "1.3.4",
|
|
4
|
+
"main": "src/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "webpack serve --mode development --open",
|
|
@@ -27,4 +27,4 @@
|
|
|
27
27
|
"flyio": "^0.6.14"
|
|
28
28
|
},
|
|
29
29
|
"sideEffects": false
|
|
30
|
-
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import fly from "flyio";
|
|
2
|
+
|
|
3
|
+
export default (request, responseCallback) => {
|
|
4
|
+
fly
|
|
5
|
+
.request(request.url, request.data, request)
|
|
6
|
+
.then(function(d) {
|
|
7
|
+
responseCallback({
|
|
8
|
+
statusCode: d.engine.status,
|
|
9
|
+
responseText: d.engine.responseText,
|
|
10
|
+
statusMessage: d.engine.statusText
|
|
11
|
+
});
|
|
12
|
+
})
|
|
13
|
+
.catch(function(err) {
|
|
14
|
+
responseCallback({
|
|
15
|
+
statusCode: err.status,
|
|
16
|
+
statusMessage: err.message
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default (req, responseCallback) => {
|
|
2
|
+
uni.request({
|
|
3
|
+
method: req.method,
|
|
4
|
+
url: req.url,
|
|
5
|
+
header: req.headers,
|
|
6
|
+
dataType: req.dataType || "json",
|
|
7
|
+
data: req.body || {},
|
|
8
|
+
success(res) {
|
|
9
|
+
responseCallback({
|
|
10
|
+
statusCode: res.statusCode,
|
|
11
|
+
responseText: res.data,
|
|
12
|
+
headers: res.header,
|
|
13
|
+
statusMessage: res.errMsg
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
fail(res) {
|
|
17
|
+
responseCallback({
|
|
18
|
+
statusCode: res.statusCode || 0,
|
|
19
|
+
statusMessage: res.errMsg
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default (req, responseCallback) => {
|
|
2
|
+
wx.request({
|
|
3
|
+
method: req.method,
|
|
4
|
+
url: req.url,
|
|
5
|
+
header: req.headers,
|
|
6
|
+
dataType: req.dataType || "text",
|
|
7
|
+
data: req.body || {},
|
|
8
|
+
success(res) {
|
|
9
|
+
responseCallback({
|
|
10
|
+
statusCode: res.statusCode,
|
|
11
|
+
responseText: res.data,
|
|
12
|
+
headers: res.header,
|
|
13
|
+
statusMessage: res.errMsg
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
fail(res) {
|
|
17
|
+
responseCallback({
|
|
18
|
+
statusCode: res.statusCode || 0,
|
|
19
|
+
statusMessage: res.errMsg
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import fly from "flyio";
|
|
2
|
+
import EngineWrapper from "flyio/dist/npm/engine-wrapper";
|
|
3
|
+
import signature from "./lib/signature";
|
|
4
|
+
import adapter from "./adapter";
|
|
5
|
+
import { isFunction } from "lodash-es";
|
|
6
|
+
|
|
7
|
+
// 主函数
|
|
8
|
+
const rsa = (
|
|
9
|
+
options = {
|
|
10
|
+
appkey: "25396816",
|
|
11
|
+
appsecret: "ba09305bef13bf8c17ace9987c66326f",
|
|
12
|
+
engineType: "fly", //底层请求库
|
|
13
|
+
adapter: "fetch" //请求适配器
|
|
14
|
+
}
|
|
15
|
+
) => {
|
|
16
|
+
fly.engine = XMLHttpRequest;
|
|
17
|
+
|
|
18
|
+
var engine = EngineWrapper((request, responseCallback) => {
|
|
19
|
+
try {
|
|
20
|
+
let flag = false;
|
|
21
|
+
if (options.exclude) {
|
|
22
|
+
if (isFunction(options.exclude)) {
|
|
23
|
+
flag = options.exclude(request);
|
|
24
|
+
console.log(flag);
|
|
25
|
+
} else {
|
|
26
|
+
for (let index = 0; index < options.exclude.length; index++) {
|
|
27
|
+
const element = options.exclude[index];
|
|
28
|
+
if (request.url.indexOf(element) > -1) {
|
|
29
|
+
flag = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const ad = `${options.adapter ? options.adapter : "fetch"}Adapter`;
|
|
35
|
+
adapter[ad](
|
|
36
|
+
flag || request.headers.noSign ? request : signature(request, options),
|
|
37
|
+
responseCallback
|
|
38
|
+
);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.warn(error);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
//覆盖默认
|
|
44
|
+
console.log(options.engineType);
|
|
45
|
+
if (options.engineType == "fly") {
|
|
46
|
+
console.log(fly.engine);
|
|
47
|
+
fly.engine = engine;
|
|
48
|
+
} else {
|
|
49
|
+
XMLHttpRequest = engine;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
if (typeof window !== "undefined") {
|
|
53
|
+
console.log("window");
|
|
54
|
+
window.gc_rsa = rsa;
|
|
55
|
+
}
|
|
56
|
+
export default rsa;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// import { createHmac, createHash } from "crypto-browserify";
|
|
2
|
+
|
|
3
|
+
import createHmac from "crypto-js/hmac-sha256";
|
|
4
|
+
import Base64 from "crypto-js/enc-base64";
|
|
5
|
+
import createMd5 from "crypto-js/md5";
|
|
6
|
+
import { isEmpty } from "lodash-es";
|
|
7
|
+
|
|
8
|
+
const sign = (str, secret) => {
|
|
9
|
+
// return createHmac("sha256", secret).update(str, "utf8").digest("base64");
|
|
10
|
+
return Base64.stringify(createHmac(str, secret));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const md5Content = data => {
|
|
14
|
+
return !isEmpty(data)
|
|
15
|
+
? Base64.stringify(createMd5(data))
|
|
16
|
+
: //createHash("md5").update(data, "utf-8").digest("base64")
|
|
17
|
+
"";
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export default { sign, md5Content };
|
package/src/lib/obj.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { mapKeys, isEmpty, includes } from "lodash-es";
|
|
2
|
+
|
|
3
|
+
export const concat = obj => {
|
|
4
|
+
let str = "";
|
|
5
|
+
mapKeys(obj, (value, key) => {
|
|
6
|
+
if (!!key) {
|
|
7
|
+
if (isEmpty(value)) {
|
|
8
|
+
str += "&" + key;
|
|
9
|
+
} else {
|
|
10
|
+
str += "&" + key + "=" + value;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
return str;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const concatURL = (pathname, query) => {
|
|
18
|
+
return isEmpty(query)
|
|
19
|
+
? pathname
|
|
20
|
+
: (includes(pathname, "?") ? pathname : pathname + "?") +
|
|
21
|
+
query.substring(1);
|
|
22
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// import stringify from "qs/lib/stringify";
|
|
2
|
+
import { concat, concatURL } from "./obj";
|
|
3
|
+
import parse from "qs/lib/parse";
|
|
4
|
+
import { isObject, isString, isEmpty, now, random } from "lodash-es";
|
|
5
|
+
import { parse as urlParseLib } from "url";
|
|
6
|
+
import crypto from "./crypto";
|
|
7
|
+
import sort from "./sort";
|
|
8
|
+
import transformHeaders from "./transformHeaders";
|
|
9
|
+
|
|
10
|
+
const FormHeader = "application/x-www-form-urlencoded";
|
|
11
|
+
const signature = (request, { appkey, appsecret, engineType }) => {
|
|
12
|
+
try {
|
|
13
|
+
if (appkey && appsecret) {
|
|
14
|
+
const { method, headers, url } = request;
|
|
15
|
+
const { Accept } = headers;
|
|
16
|
+
const urlParsed = urlParseLib(url, true),
|
|
17
|
+
queryObject = urlParsed.query;
|
|
18
|
+
|
|
19
|
+
let contentMD5 = "",
|
|
20
|
+
query = queryObject;
|
|
21
|
+
//如果没有content-type 默认加上application/json
|
|
22
|
+
if (!headers["Content-Type"]) {
|
|
23
|
+
headers["Content-Type"] = "application/json";
|
|
24
|
+
}
|
|
25
|
+
//如果没有 Accept 默认加上 */*
|
|
26
|
+
if (!headers["Accept"]) {
|
|
27
|
+
headers["Accept"] = "*/*";
|
|
28
|
+
}
|
|
29
|
+
//如果不是Form提交 对content签名
|
|
30
|
+
if (request.body && headers["Content-Type"].indexOf(FormHeader) === -1) {
|
|
31
|
+
contentMD5 = crypto.md5Content(request.body);
|
|
32
|
+
headers["Content-MD5"] = contentMD5;
|
|
33
|
+
} else {
|
|
34
|
+
//如果是Form表单提交
|
|
35
|
+
let formObject = request.body;
|
|
36
|
+
let obj;
|
|
37
|
+
if (isObject(formObject)) {
|
|
38
|
+
obj = sort(formObject);
|
|
39
|
+
}
|
|
40
|
+
if (isString(formObject)) {
|
|
41
|
+
const obj = parse(formObject);
|
|
42
|
+
console.log("objobj", obj);
|
|
43
|
+
if (isEmpty(query)) {
|
|
44
|
+
query = obj;
|
|
45
|
+
} else {
|
|
46
|
+
query = Object.assign({}, query, obj);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const urlPath = concatURL(urlParsed.pathname, concat(sort(query)));
|
|
51
|
+
|
|
52
|
+
const ContentType = headers["Content-Type"]
|
|
53
|
+
? headers["Content-Type"]
|
|
54
|
+
: "";
|
|
55
|
+
headers["X-Gw-Key"] = appkey;
|
|
56
|
+
headers["X-Gw-Timestamp"] = now();
|
|
57
|
+
headers["X-Gw-Nonce"] = random(1000000000000, 9999999999999);
|
|
58
|
+
const stringToSign = `${method}\n${
|
|
59
|
+
Accept ? Accept : "*/*"
|
|
60
|
+
}\n${contentMD5}\n${ContentType}\n\n${
|
|
61
|
+
transformHeaders(headers).transformHeader
|
|
62
|
+
}${decodeURIComponent(urlPath)}`;
|
|
63
|
+
console.log("==============", urlPath, "======================");
|
|
64
|
+
console.log(stringToSign);
|
|
65
|
+
console.log("==============", urlPath, "======================");
|
|
66
|
+
headers["X-Gw-Signature"] = crypto.sign(stringToSign, appsecret);
|
|
67
|
+
headers["X-Gw-Signature-Headers"] =
|
|
68
|
+
transformHeaders(headers).transformHeaderKeys;
|
|
69
|
+
|
|
70
|
+
return request;
|
|
71
|
+
} else {
|
|
72
|
+
console.warn("检查appkey和appsecret");
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.log("error", error);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export default signature;
|
package/src/lib/sort.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { map, isEmpty, isArray, sortBy } from "lodash-es";
|
|
2
|
+
export default obj => {
|
|
3
|
+
const sortArr = sortBy(Object.keys(obj));
|
|
4
|
+
let returnObect = {};
|
|
5
|
+
map(sortArr, key => {
|
|
6
|
+
if (isEmpty(obj[key])) {
|
|
7
|
+
returnObect[key] = null;
|
|
8
|
+
} else {
|
|
9
|
+
if (isArray(obj[key])) {
|
|
10
|
+
returnObect[key] = obj[key][0];
|
|
11
|
+
} else {
|
|
12
|
+
returnObect[key] = obj[key];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
return returnObect;
|
|
17
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
keys,
|
|
3
|
+
includes,
|
|
4
|
+
toLower,
|
|
5
|
+
map,
|
|
6
|
+
mapValues,
|
|
7
|
+
compact,
|
|
8
|
+
sortBy,
|
|
9
|
+
forEach
|
|
10
|
+
} from "lodash-es";
|
|
11
|
+
|
|
12
|
+
// just add here the lodash functions you want to support
|
|
13
|
+
const chainableFunctions = {
|
|
14
|
+
map,
|
|
15
|
+
keys,
|
|
16
|
+
compact,
|
|
17
|
+
sortBy,
|
|
18
|
+
forEach,
|
|
19
|
+
};
|
|
20
|
+
export const chain = input => {
|
|
21
|
+
let value = input;
|
|
22
|
+
let obj = mapValues(chainableFunctions, f => (...args) => {
|
|
23
|
+
// lodash always puts input as the first argument
|
|
24
|
+
value = f(value, ...args);
|
|
25
|
+
return wrapper;
|
|
26
|
+
});
|
|
27
|
+
const wrapper = Object.assign(obj, {
|
|
28
|
+
value: () => value
|
|
29
|
+
});
|
|
30
|
+
return wrapper;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default obj => {
|
|
34
|
+
//不加入签名 需要排除的数组
|
|
35
|
+
const excludes = [
|
|
36
|
+
"X-Gw-Signature",
|
|
37
|
+
"X-Gw-Signature-headers",
|
|
38
|
+
"Accept",
|
|
39
|
+
"Content-MD5",
|
|
40
|
+
"Content-Type",
|
|
41
|
+
"Date"
|
|
42
|
+
];
|
|
43
|
+
let transformHeader = "",
|
|
44
|
+
transformHeaderKeys = "",
|
|
45
|
+
first = true;
|
|
46
|
+
|
|
47
|
+
chain(obj)
|
|
48
|
+
.keys(obj)
|
|
49
|
+
.map(value => {
|
|
50
|
+
return (
|
|
51
|
+
//排除上面的已经存在的各种header
|
|
52
|
+
!includes(excludes, value) && {
|
|
53
|
+
value: obj[value],
|
|
54
|
+
key: toLower(value)
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
})
|
|
58
|
+
.compact()
|
|
59
|
+
.sortBy(value => {
|
|
60
|
+
return value.key;
|
|
61
|
+
})
|
|
62
|
+
.forEach(item => {
|
|
63
|
+
transformHeader += `${item["key"]}:${item["value"]}\n`;
|
|
64
|
+
if (first) {
|
|
65
|
+
transformHeaderKeys += item["key"];
|
|
66
|
+
} else {
|
|
67
|
+
transformHeaderKeys += "," + item["key"];
|
|
68
|
+
}
|
|
69
|
+
first = false;
|
|
70
|
+
});
|
|
71
|
+
return { transformHeader, transformHeaderKeys };
|
|
72
|
+
};
|
package/.babelrc
DELETED