@bee.js/node 0.0.96 → 0.0.97
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/beehive.js +56 -1
- package/package.json +2 -2
package/beehive.js
CHANGED
|
@@ -4,6 +4,7 @@ const beeORM = require("./lib/ORM/beeORM");
|
|
|
4
4
|
const beeDBA = require("./lib/DBA/beeDBA");
|
|
5
5
|
const beeJWT = require("./lib/JWT/beeJWT");
|
|
6
6
|
const beeTools = require("./tools/beeTools");
|
|
7
|
+
const crypto = require("crypto");
|
|
7
8
|
|
|
8
9
|
module.exports = function hive(req = {}, res = {}, model = null) {
|
|
9
10
|
model =
|
|
@@ -468,7 +469,28 @@ module.exports = function hive(req = {}, res = {}, model = null) {
|
|
|
468
469
|
},
|
|
469
470
|
|
|
470
471
|
response: function (sendData = data, action = null, status) {
|
|
471
|
-
|
|
472
|
+
let responseData = sendData;
|
|
473
|
+
let e2eeConfig = null;
|
|
474
|
+
|
|
475
|
+
if (
|
|
476
|
+
sendData &&
|
|
477
|
+
typeof sendData === "object" &&
|
|
478
|
+
!Array.isArray(sendData) &&
|
|
479
|
+
sendData.e2ee
|
|
480
|
+
) {
|
|
481
|
+
const hasDataOverride = Object.prototype.hasOwnProperty.call(
|
|
482
|
+
sendData,
|
|
483
|
+
"data"
|
|
484
|
+
);
|
|
485
|
+
const isConfigOnly = Object.keys(sendData).length === 1;
|
|
486
|
+
|
|
487
|
+
if (hasDataOverride || isConfigOnly) {
|
|
488
|
+
e2eeConfig = sendData.e2ee;
|
|
489
|
+
responseData = hasDataOverride ? sendData.data : data;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const out = { data: responseData, counters, action, error };
|
|
472
494
|
|
|
473
495
|
if (res.headersSent)
|
|
474
496
|
return console.error("ERROR beejs: headers already sent");
|
|
@@ -477,6 +499,39 @@ module.exports = function hive(req = {}, res = {}, model = null) {
|
|
|
477
499
|
|
|
478
500
|
if (global.configs.debug) out.debug = debug;
|
|
479
501
|
|
|
502
|
+
if (e2eeConfig) {
|
|
503
|
+
try {
|
|
504
|
+
const algorithm =
|
|
505
|
+
e2eeConfig.algorithm || e2eeConfig.alghoritimo || "aes-256-gcm";
|
|
506
|
+
const key =
|
|
507
|
+
typeof e2eeConfig.key === "string" ? e2eeConfig.key : "";
|
|
508
|
+
|
|
509
|
+
if (key) {
|
|
510
|
+
const iv = crypto.randomBytes(12);
|
|
511
|
+
const hashKey =
|
|
512
|
+
/^[a-f0-9]{64}$/i.test(key)
|
|
513
|
+
? Buffer.from(key, "hex")
|
|
514
|
+
: crypto.createHash("sha256").update(key).digest();
|
|
515
|
+
const cipher = crypto.createCipheriv(algorithm, hashKey, iv);
|
|
516
|
+
const jsonData = JSON.stringify(out.data ?? null);
|
|
517
|
+
const encryptedBuffer = Buffer.concat([
|
|
518
|
+
cipher.update(jsonData, "utf8"),
|
|
519
|
+
cipher.final(),
|
|
520
|
+
]);
|
|
521
|
+
const tag = cipher.getAuthTag();
|
|
522
|
+
out.data = {
|
|
523
|
+
e2ee: {
|
|
524
|
+
encrypted: encryptedBuffer.toString("base64"),
|
|
525
|
+
iv: iv.toString("base64"),
|
|
526
|
+
tag: tag.toString("base64"),
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
} catch (e) {
|
|
531
|
+
console.error("ERROR beejs: e2ee encryption failed", e);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
480
535
|
if (status) res.status(status).send(out);
|
|
481
536
|
else
|
|
482
537
|
switch (req.method) {
|
package/package.json
CHANGED