@meebon/meebon-crypto 1.2.21 → 1.3.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
1
  import { RequestHandler } from 'express';
2
2
  import { MeebonCryptoKeyPair } from '../types/global';
3
3
  /**
4
- * Middleware for encrypting incoming request data and decrypting outgoing response data.
4
+ * Middleware to encrypt response bodies if the request header 'x-require-encryption' is present.
5
5
  *
6
- * @param {MeebonCryptoKeyPair} keys - An object containing the privateKey, publicKey, and forRequest flag.
7
- * @returns {RequestHandler} The Express middleware function.
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 EncryptionMiddleware({ privateKey, publicKey, forRequest }: MeebonCryptoKeyPair): RequestHandler;
10
- export { EncryptionMiddleware };
41
+ declare function decryptMiddleware({ privateKey }: Omit<MeebonCryptoKeyPair, "publicKey">): RequestHandler;
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,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"}
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;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,iBAAS,oBAAoB,CAAC,EAAE,SAAS,EAAE,EAAE,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,GAAG,cAAc,CA0BpG;AAID;;;;;;;;;;;;GAYG;AACH,iBAAS,iBAAiB,CAAC,EAAE,UAAU,EAAE,EAAE,IAAI,CAAC,mBAAmB,EAAE,WAAW,CAAC,GAAG,cAAc,CAYjG;AAED,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import forge from 'node-forge';
2
- import { MeebonCryptoGenerateKeyPair, MeebonCryptoProps } from '../types/global';
2
+ import { MeebonCryptoKeyPair, MeebonCryptoProps } from '../types/global';
3
3
  export declare class MeebonCrypto {
4
4
  protected privateKeyPem: string;
5
5
  protected publicKeyPem: string;
@@ -29,9 +29,9 @@ export declare class MeebonCrypto {
29
29
  * Generates an RSA key pair with the specified key size.
30
30
  *
31
31
  * @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
32
- * @returns {MeebonCryptoGenerateKeyPair} An object containing the generated public and private keys in PEM format.
32
+ * @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
33
33
  */
34
- static generateKeyPair(length?: number): MeebonCryptoGenerateKeyPair;
34
+ static generateKeyPair(length?: number): MeebonCryptoKeyPair;
35
35
  /**
36
36
  * Encrypts data with a provided public key.
37
37
  * @param {string} data - The data to encrypt.
@@ -1 +1 @@
1
- {"version":3,"file":"MeebonCrypto.d.ts","sourceRoot":"","sources":["../lib/MeebonCrypto.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAE,2BAA2B,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEjF,qBAAa,YAAY;IACvB,SAAS,CAAC,aAAa,EAAE,MAAM,CAAC;IAChC,SAAS,CAAC,YAAY,EAAE,MAAM,CAAC;IAExB,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC;IACrC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC;IAE1C,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,gBAAgB,CAAC;IAEjD,OAAO;IASP;;;;OAIG;WACW,IAAI,CAAC,KAAK,EAAE,iBAAiB,GAAG,YAAY;IAI1D;;;;OAIG;IACI,OAAO,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM;IAKzC;;;;OAIG;IACI,OAAO,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM;IAK7C;;;;;OAKG;WACW,eAAe,CAAC,MAAM,GAAE,MAAa,GAAG,2BAA2B;IAQjF;;;;;;OAMG;WACW,WAAW,CACvB,I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AW,CACvB,aAAa,EAAE,MAAM,EACrB,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,gBAA6B,GAClD,MAAM;CAKV"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { MeebonCrypto } from "./MeebonCrypto";
2
- import { EncryptionMiddleware } from "./ExpressEncryptionMiddleware";
3
- export { EncryptionMiddleware, MeebonCrypto };
2
+ import { decryptMiddleware, encryptionMiddleware } from "./ExpressEncryptionMiddleware";
3
+ export { decryptMiddleware, encryptionMiddleware, MeebonCrypto };
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -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;AACrE,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,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
@@ -43,7 +43,7 @@ var MeebonCrypto = class _MeebonCrypto {
43
43
  * Generates an RSA key pair with the specified key size.
44
44
  *
45
45
  * @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
46
- * @returns {MeebonCryptoGenerateKeyPair} An object containing the generated public and private keys in PEM format.
46
+ * @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
47
47
  */
48
48
  static generateKeyPair(length = 3072) {
49
49
  const keyPair = forge.pki.rsa.generateKeyPair({ bits: length, e: 65537 });
@@ -79,41 +79,48 @@ var MeebonCrypto = class _MeebonCrypto {
79
79
  };
80
80
 
81
81
  // lib/ExpressEncryptionMiddleware.ts
82
- function EncryptionMiddleware({ privateKey, publicKey, forRequest }) {
82
+ function encryptionMiddleware({ publicKey }) {
83
83
  return (req, res, next) => {
84
- if (forRequest) {
85
- if (req.body && req.body.data) {
86
- try {
87
- const plainText = JSON.stringify(req.body.data);
88
- const encrypted = MeebonCrypto.decryptData(plainText, privateKey);
89
- req.body.data = encrypted;
90
- } catch (error) {
91
- next(new Error(error?.message || "Encryption failed."));
92
- }
84
+ const originalSend = res.send.bind(res);
85
+ const originalJson = res.json.bind(res);
86
+ res.send = function(body) {
87
+ if (req.headers["x-require-encryption"]) {
88
+ let bodyString = typeof body === "object" ? JSON.stringify(body) : body.toString();
89
+ const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
90
+ res.set("Content-Type", "text/plain");
91
+ return originalSend(encryptedBody);
92
+ } else {
93
+ return originalSend(body);
94
+ }
95
+ };
96
+ res.json = function(body) {
97
+ if (req.headers["x-require-encryption"]) {
98
+ const bodyString = JSON.stringify(body);
99
+ const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
100
+ res.set("Content-Type", "text/plain");
101
+ return originalSend(encryptedBody);
102
+ } else {
103
+ return originalJson(body);
104
+ }
105
+ };
106
+ next();
107
+ };
108
+ }
109
+ function decryptMiddleware({ privateKey }) {
110
+ return (req, res, next) => {
111
+ if (req.headers["x-encrypted"] && typeof req.body === "string") {
112
+ try {
113
+ const decryptedText = MeebonCrypto.decryptData(req.body, privateKey);
114
+ req.body = JSON.parse(decryptedText);
115
+ } catch (err) {
116
+ res.status(400).json({ error: "Invalid encrypted data" });
93
117
  }
94
- } else {
95
- const originalSend = res.send.bind(res);
96
- res.send = (body) => {
97
- let encryptData = body;
98
- try {
99
- if (encryptData?.data) {
100
- const decrypted = MeebonCrypto.encryptData(encryptData.data, publicKey);
101
- try {
102
- encryptData.data = JSON.parse(decrypted);
103
- } catch {
104
- encryptData.data = decrypted;
105
- }
106
- }
107
- } catch (error) {
108
- next(new Error(error?.message || "Decryption failed."));
109
- }
110
- return originalSend(encryptData);
111
- };
112
118
  }
113
119
  next();
114
120
  };
115
121
  }
116
122
  export {
117
- EncryptionMiddleware,
118
- MeebonCrypto
123
+ MeebonCrypto,
124
+ decryptMiddleware,
125
+ encryptionMiddleware
119
126
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meebon/meebon-crypto",
3
- "version": "1.2.21",
3
+ "version": "1.3.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",
package/types/global.d.ts CHANGED
@@ -9,7 +9,4 @@ export interface MeebonCryptoProps {
9
9
  export interface MeebonCryptoKeyPair {
10
10
  privateKey: string;
11
11
  publicKey: string;
12
- forRequest?: boolean;
13
12
  }
14
-
15
- export type MeebonCryptoGenerateKeyPair = Omit<MeebonCryptoKeyPair, "forRequest">;