@meebon/meebon-crypto 1.2.15 → 1.2.16
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.
|
@@ -3,9 +3,9 @@ import { MeebonCryptoKeyPair } from '../types/global';
|
|
|
3
3
|
/**
|
|
4
4
|
* Middleware for encrypting incoming request data and decrypting outgoing response data.
|
|
5
5
|
*
|
|
6
|
-
* @param {MeebonCryptoKeyPair} keys - An object containing the privateKey
|
|
6
|
+
* @param {MeebonCryptoKeyPair} keys - An object containing the privateKey, publicKey, and forRequest flag.
|
|
7
7
|
* @returns {RequestHandler} The Express middleware function.
|
|
8
8
|
*/
|
|
9
|
-
declare function EncryptionMiddleware({ privateKey, publicKey }: MeebonCryptoKeyPair): RequestHandler;
|
|
9
|
+
declare function EncryptionMiddleware({ privateKey, publicKey, forRequest }: MeebonCryptoKeyPair): RequestHandler;
|
|
10
10
|
export { EncryptionMiddleware };
|
|
11
11
|
//# sourceMappingURL=ExpressEncryptionMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;GAKG;AACH,iBAAS,oBAAoB,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,EAAE,mBAAmB,GAAG,cAAc,
|
|
1
|
+
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;GAKG;AACH,iBAAS,oBAAoB,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,mBAAmB,GAAG,cAAc,CAsCxG;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -79,24 +79,23 @@ var MeebonCrypto = class _MeebonCrypto {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
// lib/ExpressEncryptionMiddleware.ts
|
|
82
|
-
function EncryptionMiddleware({ privateKey, publicKey }) {
|
|
82
|
+
function EncryptionMiddleware({ privateKey, publicKey, forRequest }) {
|
|
83
83
|
return (req, res, next) => {
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
84
|
+
if (forRequest) {
|
|
85
|
+
if (req.body && req.body.data) {
|
|
86
|
+
try {
|
|
87
87
|
const plainText = JSON.stringify(req.body.data);
|
|
88
88
|
const encrypted = MeebonCrypto.encryptData(plainText, publicKey);
|
|
89
89
|
req.body.data = encrypted;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
next(new Error(error?.message || "Encryption failed."));
|
|
90
92
|
}
|
|
91
|
-
} catch (error) {
|
|
92
|
-
return next(new Error(error?.message || "Encryption failed."));
|
|
93
93
|
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (body) {
|
|
94
|
+
} else {
|
|
95
|
+
const originalSend = res.send.bind(res);
|
|
96
|
+
res.send = (body) => {
|
|
97
|
+
try {
|
|
98
|
+
if (typeof body === "string" && body) {
|
|
100
99
|
const decrypted = MeebonCrypto.decryptData(body, privateKey);
|
|
101
100
|
try {
|
|
102
101
|
body = JSON.parse(decrypted);
|
|
@@ -104,13 +103,13 @@ function EncryptionMiddleware({ privateKey, publicKey }) {
|
|
|
104
103
|
body = decrypted;
|
|
105
104
|
}
|
|
106
105
|
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
next(new Error(error?.message || "Decryption failed."));
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
};
|
|
113
|
-
return next();
|
|
109
|
+
return originalSend(body);
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
next();
|
|
114
113
|
};
|
|
115
114
|
}
|
|
116
115
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meebon/meebon-crypto",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.16",
|
|
4
4
|
"description": "",
|
|
5
5
|
"author": "kajalanS <103587022+kajalanS@users.noreply.github.com>",
|
|
6
6
|
"homepage": "https://github.com/KsoftmHub/meebon-crypto?tab=readme-ov-file#getting-started",
|
package/types/global.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { pki } from "node-forge";
|
|
2
2
|
|
|
3
3
|
export interface MeebonCryptoProps {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
privateKeyPem: string;
|
|
5
|
+
publicKeyPem: string;
|
|
6
|
+
schema?: pki.rsa.EncryptionScheme;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface MeebonCryptoKeyPair {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
privateKey: string;
|
|
11
|
+
publicKey: string;
|
|
12
|
+
forRequest?: boolean;
|
|
13
|
+
}
|