cloud-web-corejs 1.1.0-dev.87 → 1.1.0-dev.89

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cloud-web-corejs",
3
3
  "private": false,
4
- "version": "1.1.0-dev.87",
4
+ "version": "1.1.0-dev.89",
5
5
  "scripts": {
6
6
  "build:ace-resources": "node build/build-ace-resources.js",
7
7
  "build:tinymce-resources": "node build/build-tinymce-resources.js",
@@ -3,7 +3,7 @@ import vue from "vue";
3
3
  import excelImport from "./index.vue";
4
4
  import exportDialog from "./exportDialog.vue";
5
5
  import { getBdEnv } from "@base/api/user";
6
- import { encrypt } from "@base/utils/aes";
6
+ import { encrypt, loadAesKeys } from "@base/utils/aes";
7
7
  import {
8
8
  getExportArchiveType,
9
9
  processArchiveExport,
@@ -88,8 +88,11 @@ moudule.install = function (Vue) {
88
88
  if (options.plaintext) {
89
89
  reqData.plaintext = options.plaintext;
90
90
  }
91
+ loadAesKeys().catch(function () {});
92
+ const plain = JSON.stringify(reqData);
93
+ const cipher = encrypt(plain);
91
94
  let data = {
92
- datas: encrypt(JSON.stringify(reqData)),
95
+ datas: cipher === null ? plain : cipher,
93
96
  };
94
97
 
95
98
  that.$http({
@@ -9,6 +9,7 @@ import {
9
9
  getLogicParamValue,
10
10
  } from "../../api/user";
11
11
  import { getToken, setToken, clearToken } from "../../utils/auth";
12
+ import { loadAesKeys, clearAesKeys } from "../../utils/aes";
12
13
  import { clearBaseDictsCache } from "../../utils/baseDictsCache";
13
14
  import router, { resetRouter } from "@base/router";
14
15
  import { unmountAllMicroApps } from "@base/microRouter";
@@ -142,6 +143,7 @@ actions = {
142
143
  let token = opts.token;
143
144
  commit("SET_TOKEN", token);
144
145
  configUtil.setToken(token);
146
+ loadAesKeys(token).catch(function () {});
145
147
  sessionStorage.setItem(
146
148
  "easyweb",
147
149
  JSON.stringify({
@@ -156,6 +158,8 @@ actions = {
156
158
 
157
159
  // get user info
158
160
  getInfo2({ commit, state, dispatch }) {
161
+ // 刷新页面或主应用注入 token 时不会再走登录,这里补拉一次密钥。失败不提示、不阻塞。
162
+ loadAesKeys().catch(function () {});
159
163
  return new Promise((resolve, reject) => {
160
164
  configUtil
161
165
  .getInfo({
@@ -320,6 +324,7 @@ actions = {
320
324
  // remove token
321
325
  resetToken({ commit, state, dispatch }) {
322
326
  return new Promise((resolve) => {
327
+ clearAesKeys();
323
328
  commit("SET_TOKEN", "");
324
329
  commit("SET_ROLES", []);
325
330
  commit("SET_NICK_NAME", "");
package/src/utils/aes.js CHANGED
@@ -1,21 +1,142 @@
1
+ import request from "./request.js";
1
2
  import CryptoJS from "crypto-js";
3
+ import prefixConfig from "@/prefixConfig.js";
4
+ import { getToken } from "./auth.js";
2
5
 
3
- const secretKey = "2025202620272028"; // 密钥必须是16个字节
4
- const ivKey = "2022202320242025"; // 向量必须是16个字节
6
+ // 密钥只存在内存里,跟当前 token 绑定。退出登录或 token 变化后作废。
7
+ // 接口 404、objx 为空、解密失败时记成「本次不加密」,请求继续发原始内容。
8
+ let keys = null;
9
+ let pending = null;
10
+ let unavailableToken = null;
11
+ let generation = 0;
12
+
13
+ function currentKeys() {
14
+ const token = getToken();
15
+ if (!keys || !token || keys.token !== token || !keys.k || !keys.iv)
16
+ return null;
17
+ return keys;
18
+ }
19
+
20
+ export function setAesKeys(k, iv, token) {
21
+ const boundToken = token == null ? getToken() : token;
22
+ if (!k || !iv || !boundToken) {
23
+ keys = null;
24
+ return;
25
+ }
26
+ keys = { k: k, iv: iv, token: boundToken };
27
+ unavailableToken = null;
28
+ }
29
+
30
+ export function clearAesKeys() {
31
+ generation += 1;
32
+ keys = null;
33
+ pending = null;
34
+ unavailableToken = null;
35
+ }
36
+
37
+ function decryptKok(objx, token) {
38
+ try {
39
+ const material = String(token || "").substring(0, 16);
40
+ if (material.length < 16) return null;
41
+ const plain = CryptoJS.AES.decrypt(
42
+ objx,
43
+ CryptoJS.enc.Utf8.parse(material),
44
+ {
45
+ iv: CryptoJS.enc.Utf8.parse(material),
46
+ }
47
+ ).toString(CryptoJS.enc.Utf8);
48
+ const sep = plain.indexOf(":");
49
+ if (sep <= 0) return null;
50
+ const k = plain.slice(0, sep);
51
+ const iv = plain.slice(sep + 1);
52
+ if (!k || !iv) return null;
53
+ return { k: k, iv: iv };
54
+ } catch (e) {
55
+ return null;
56
+ }
57
+ }
58
+
59
+ function requestKok(token) {
60
+ const prefix = String(prefixConfig.USER_PREFIX || "/user").replace(/\/$/, "");
61
+ // 走统一请求。失败不弹窗;fetchAesKey: false 避免本请求再进去取一次密钥。
62
+ return request({
63
+ url: prefix + "/common/kok",
64
+ method: "post",
65
+ timeout: 10000,
66
+ isLoading: false,
67
+ modal: false,
68
+ failMsg: false,
69
+ errorMsg: false,
70
+ fetchAesKey: false,
71
+ filterConfig: { enabled: false },
72
+ })
73
+ .then(function (data) {
74
+ if (!data || typeof data !== "object") return null;
75
+ if (data.type && data.type !== "success") return null;
76
+ if (!data.objx) return null;
77
+ return decryptKok(data.objx, token);
78
+ })
79
+ .catch(function () {
80
+ return null;
81
+ });
82
+ }
83
+
84
+ // 登录成功或刷新后调用。同一 token 只请求一次,进行中的调用共用同一个 Promise。
85
+ export function loadAesKeys(token) {
86
+ const current = token || getToken();
87
+ if (!current) return Promise.resolve(null);
88
+ if (keys && keys.token === current && keys.k && keys.iv) {
89
+ return Promise.resolve(keys);
90
+ }
91
+ if (unavailableToken === current) return Promise.resolve(null);
92
+ if (pending && pending.token === current) return pending.promise;
93
+
94
+ const gen = generation;
95
+ const promise = requestKok(current)
96
+ .then(function (result) {
97
+ if (gen !== generation || getToken() !== current) return null;
98
+ if (!result) {
99
+ unavailableToken = current;
100
+ if (keys && keys.token === current) keys = null;
101
+ return null;
102
+ }
103
+ keys = { k: result.k, iv: result.iv, token: current };
104
+ unavailableToken = null;
105
+ return keys;
106
+ })
107
+ .catch(function () {
108
+ if (gen !== generation) return null;
109
+ unavailableToken = current;
110
+ return null;
111
+ })
112
+ .finally(function () {
113
+ if (pending && pending.promise === promise) pending = null;
114
+ });
115
+ pending = { token: current, promise: promise };
116
+ return promise;
117
+ }
5
118
 
6
- // 加密函数
7
119
  export function encrypt(text) {
8
- return CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(secretKey), {
9
- iv: CryptoJS.enc.Utf8.parse(ivKey),
120
+ const key = currentKeys();
121
+ if (!key) return null;
122
+ return CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(key.k), {
123
+ iv: CryptoJS.enc.Utf8.parse(key.iv),
10
124
  }).toString();
11
125
  }
12
126
 
13
- // 解密函数
14
127
  export function decrypt(ciphertext) {
15
- const bytes = CryptoJS.AES.decrypt(
16
- ciphertext,
17
- CryptoJS.enc.Utf8.parse(secretKey),
18
- { iv: CryptoJS.enc.Utf8.parse(ivKey) }
19
- );
20
- return bytes.toString(CryptoJS.enc.Utf8);
128
+ const key = currentKeys();
129
+ if (!key || ciphertext == null || ciphertext === "") return ciphertext;
130
+ try {
131
+ const bytes = CryptoJS.AES.decrypt(
132
+ ciphertext,
133
+ CryptoJS.enc.Utf8.parse(key.k),
134
+ {
135
+ iv: CryptoJS.enc.Utf8.parse(key.iv),
136
+ }
137
+ );
138
+ return bytes.toString(CryptoJS.enc.Utf8);
139
+ } catch (e) {
140
+ return ciphertext;
141
+ }
21
142
  }
@@ -8,7 +8,7 @@ import ErroreBox from "../components/errorMsg/index.js";
8
8
  import PopupManager from "element-ui/src/utils/popup/popup-manager.js";
9
9
  import settingConfig from "@/settings.js";
10
10
  import { encode } from "js-base64";
11
- import { encrypt } from "@base/utils/aes";
11
+ import { encrypt, loadAesKeys } from "@base/utils/aes";
12
12
  import { handleRequestNetworkError } from "@base/utils/runtimeRecovery";
13
13
 
14
14
  let configUtil = {
@@ -95,14 +95,23 @@ function aesPlainText(data, useEmptyObject) {
95
95
  }
96
96
 
97
97
  function wrapAesBody(data, useEmptyObject) {
98
+ const cipher = encrypt(aesPlainText(data, useEmptyObject));
99
+ if (!cipher) return null;
98
100
  return {
99
- _aeenboda: encrypt(aesPlainText(data, useEmptyObject)),
101
+ _aeenboda: cipher,
100
102
  };
101
103
  }
102
104
 
105
+ function applyAesBody(config, useEmptyObject) {
106
+ const wrapped = wrapAesBody(config.data, useEmptyObject);
107
+ if (!wrapped) return false;
108
+ config.data = wrapped;
109
+ return true;
110
+ }
111
+
103
112
  // request interceptor
104
113
  service.interceptors.request.use(
105
- (config) => {
114
+ async (config) => {
106
115
  if (config.useToken !== false) {
107
116
  let token = getToken();
108
117
  if (token) {
@@ -123,30 +132,33 @@ service.interceptors.request.use(
123
132
  );
124
133
  }
125
134
  const canEncryptBody = !isBinaryRequestBody(config.data);
126
- if (
127
- settingConfig.requestAesEnabled === true &&
128
- canEncryptBody &&
129
- isOwnBackendRequest(config)
130
- ) {
131
- config.data = wrapAesBody(config.data, true);
135
+ // fetchAesKey: false 强制本请求不取密钥,也不加密。
136
+ const skipAesKey = config.fetchAesKey === false;
137
+ const wantGlobalAes
138
+ = !skipAesKey
139
+ && settingConfig.requestAesEnabled === true
140
+ && canEncryptBody
141
+ && isOwnBackendRequest(config);
142
+ const isFormUrlEncoded
143
+ = config.headers["Content-Type"] === "application/x-www-form-urlencoded"
144
+ || config.headers["Content-Type"]
145
+ === "application/x-www-form-urlencoded;charset=UTF-8";
146
+ const wantListedAes
147
+ = !skipAesKey
148
+ && canEncryptBody
149
+ && settingConfig.base64Urls
150
+ && settingConfig.base64Urls.includes(config.url);
151
+ const wantFlagAes = !skipAesKey && canEncryptBody && config.aes;
152
+ // 密钥在后台取。没取到或接口 404/报错时不等待、不提示,直接发原始请求体。
153
+ if (wantGlobalAes || wantListedAes || wantFlagAes) {
154
+ loadAesKeys().catch(function () {});
155
+ }
156
+ if (wantGlobalAes && applyAesBody(config, true)) {
132
157
  config.headers["Content-Type"] = "application/json;charset=UTF-8";
133
- } else if (
134
- (config.headers["Content-Type"] === "application/x-www-form-urlencoded" ||
135
- config.headers["Content-Type"] ===
136
- "application/x-www-form-urlencoded;charset=UTF-8") &&
137
- typeof config.data !== "string" &&
138
- config.data
139
- ) {
158
+ } else if (isFormUrlEncoded && typeof config.data !== "string" && config.data) {
140
159
  config.data = qs.stringify(config.data);
141
- } else if (
142
- canEncryptBody &&
143
- settingConfig.base64Urls &&
144
- settingConfig.base64Urls.includes(config.url)
145
- ) {
146
- // config.data = {"_baenboda": encode(JSON.stringify(config.data))};
147
- config.data = wrapAesBody(config.data);
148
- } else if (canEncryptBody && config.aes) {
149
- config.data = wrapAesBody(config.data);
160
+ } else if (wantListedAes || wantFlagAes) {
161
+ applyAesBody(config);
150
162
  }
151
163
 
152
164
  if (config.modal !== false && config.loadingTarget) {