@meebon/meebon-crypto 1.2.22 → 1.4.0
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.
|
@@ -1,11 +1,43 @@
|
|
|
1
|
-
import { RequestHandler } from 'express';
|
|
1
|
+
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
2
|
import { MeebonCryptoKeyPair } from '../types/global';
|
|
3
3
|
/**
|
|
4
|
-
* Middleware
|
|
4
|
+
* Middleware to encrypt response bodies if the request header 'x-require-encryption' is present.
|
|
5
5
|
*
|
|
6
|
-
* @param {
|
|
7
|
-
* @
|
|
6
|
+
* @param {Object} param - An object containing the public key.
|
|
7
|
+
* @param {string} param.publicKey - The public key used for encryption.
|
|
8
|
+
* @returns {RequestHandler} - The middleware function to handle encryption of response bodies.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import express from 'express';
|
|
12
|
+
* import { encryptionMiddleware } from './path/to/ExpressEncryptionMiddleware';
|
|
13
|
+
*
|
|
14
|
+
* const app = express();
|
|
15
|
+
* const publicKey = 'your-public-key';
|
|
16
|
+
*
|
|
17
|
+
* app.use(encryptionMiddleware({ publicKey }));
|
|
18
|
+
*
|
|
19
|
+
* app.get('/data', (req, res) => {
|
|
20
|
+
* res.json({ message: 'Hello, world!' });
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* app.listen(3000, () => {
|
|
24
|
+
* console.log('Server is running on port 3000');
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
27
|
+
declare function encryptionMiddleware({ publicKey }: Omit<MeebonCryptoKeyPair, "privateKey">): RequestHandler;
|
|
28
|
+
/**
|
|
29
|
+
* Middleware to decrypt the request body if it is encrypted.
|
|
30
|
+
*
|
|
31
|
+
* This middleware checks for the presence of the 'x-encrypted' header and
|
|
32
|
+
* attempts to decrypt the request body using the provided private key.
|
|
33
|
+
* If decryption is successful, the decrypted data is parsed as JSON and
|
|
34
|
+
* assigned to `req.body`. If decryption fails, a 400 status code with an
|
|
35
|
+
* error message is returned.
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} param - The parameter object.
|
|
38
|
+
* @param {string} param.privateKey - The private key used for decryption.
|
|
39
|
+
* @returns {RequestHandler} The middleware function.
|
|
8
40
|
*/
|
|
9
|
-
declare function
|
|
10
|
-
export {
|
|
41
|
+
declare function decryptMiddleware({ privateKey }: Omit<MeebonCryptoKeyPair, "publicKey">): (req: Request, res: Response, next: NextFunction) => void;
|
|
42
|
+
export { encryptionMiddleware, decryptMiddleware };
|
|
11
43
|
//# sourceMappingURL=ExpressEncryptionMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAAqC,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEzF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,iBAAS,oBAAoB,CAAC,EAAE,SAAS,EAAE,EAAE,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,GAAG,cAAc,CAkBpG;AAID;;;;;;;;;;;;GAYG;AACH,iBAAS,iBAAiB,CAAC,EAAE,UAAU,EAAE,EAAE,IAAI,CAAC,mBAAmB,EAAE,WAAW,CAAC,IACvE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,UAWxD;AAED,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { MeebonCrypto } from "./MeebonCrypto";
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
2
|
+
import { decryptMiddleware, encryptionMiddleware } from "./ExpressEncryptionMiddleware";
|
|
3
|
+
export { decryptMiddleware, encryptionMiddleware, MeebonCrypto };
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACxF,OAAO,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,YAAY,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -79,37 +79,40 @@ var MeebonCrypto = class _MeebonCrypto {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
// lib/ExpressEncryptionMiddleware.ts
|
|
82
|
-
function
|
|
82
|
+
function encryptionMiddleware({ publicKey }) {
|
|
83
83
|
return (req, res, next) => {
|
|
84
|
-
if (req.body && req.body.data) {
|
|
85
|
-
try {
|
|
86
|
-
const encrypted = MeebonCrypto.decryptData(req.body.data, privateKey);
|
|
87
|
-
req.body.data = JSON.parse(encrypted);
|
|
88
|
-
} catch (error) {
|
|
89
|
-
next(new Error(error?.message || "Encryption failed."));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
84
|
const originalSend = res.send.bind(res);
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
const originalJson = res.json.bind(res);
|
|
86
|
+
res.send = function(body) {
|
|
87
|
+
let bodyString = typeof body === "object" ? JSON.stringify(body) : body.toString();
|
|
88
|
+
const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
|
|
89
|
+
res.set("Content-Type", "text/plain");
|
|
90
|
+
return originalSend(encryptedBody);
|
|
91
|
+
};
|
|
92
|
+
res.json = function(body) {
|
|
93
|
+
const bodyString = JSON.stringify(body);
|
|
94
|
+
const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
|
|
95
|
+
res.set("Content-Type", "text/plain");
|
|
96
|
+
return originalSend(encryptedBody);
|
|
97
|
+
};
|
|
98
|
+
next();
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function decryptMiddleware({ privateKey }) {
|
|
102
|
+
return (req, res, next) => {
|
|
103
|
+
if (typeof req.body === "string") {
|
|
95
104
|
try {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
} catch {
|
|
101
|
-
encryptData.data = decrypted;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
} catch (error) {
|
|
105
|
-
next(new Error(error?.message || "Decryption failed."));
|
|
105
|
+
const decryptedText = MeebonCrypto.decryptData(req.body, privateKey);
|
|
106
|
+
req.body = JSON.parse(decryptedText);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
res.status(400).json({ error: "Invalid encrypted data" });
|
|
106
109
|
}
|
|
107
|
-
|
|
108
|
-
};
|
|
110
|
+
}
|
|
109
111
|
next();
|
|
110
112
|
};
|
|
111
113
|
}
|
|
112
114
|
export {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
MeebonCrypto,
|
|
116
|
+
decryptMiddleware,
|
|
117
|
+
encryptionMiddleware
|
|
115
118
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meebon/meebon-crypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
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",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"type": "module",
|
|
10
|
-
"types": "
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
11
|
"directories": {
|
|
12
12
|
"lib": "lib",
|
|
13
13
|
"tests": "tests",
|
package/types/global.d.ts
CHANGED
|
@@ -10,3 +10,11 @@ export interface MeebonCryptoKeyPair {
|
|
|
10
10
|
privateKey: string;
|
|
11
11
|
publicKey: string;
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
export interface IDecryptRequest extends Request {
|
|
15
|
+
decrypt: (data: any) => any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IEncryptResponse extends Response {
|
|
19
|
+
encrypt: (data: any) => any;
|
|
20
|
+
}
|