@meebon/meebon-crypto 1.2.2 → 1.2.3

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,8 +1,11 @@
1
- import { Request, Response, NextFunction } from 'express';
1
+ import { RequestHandler } from 'express';
2
2
  import { MeebonCryptoKeyPair } from '../types/global';
3
3
  /**
4
- * Middleware Function for Encryption and Decryption
4
+ * Middleware for encrypting incoming request data and decrypting outgoing response data.
5
+ *
6
+ * @param {MeebonCryptoKeyPair} keys - An object containing the privateKey and publicKey in PEM format.
7
+ * @returns {RequestHandler} The Express middleware function.
5
8
  */
6
- declare const encryptionMiddleware: ({ privateKey, publicKey, }: MeebonCryptoKeyPair) => (req: Request, res: Response, next: NextFunction) => void;
9
+ declare const encryptionMiddleware: ({ privateKey, publicKey }: MeebonCryptoKeyPair) => RequestHandler;
7
10
  export default encryptionMiddleware;
8
11
  //# sourceMappingURL=ExpressEncryptionMiddleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ExpressEncryptionMiddleware.d.ts","sourceRoot":"","sources":["../lib/ExpressEncryptionMiddleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAA;AACzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;GAEG;AACH,QAAA,MAAM,oBAAoB,+BAAgC,mBAAmB,WAC9D,OAAO,OAAO,QAAQ,QAAQ,YAAY,SAyBxD,CAAC;AAEF,eAAe,oBAAoB,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,QAAA,MAAM,oBAAoB,8BAA+B,mBAAmB,KAAG,cAkC9E,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { MeebonCrypto } from './MeebonCrypto';
2
- export declare const EncryptionMiddleware: ({ privateKey, publicKey, }: import("../types/global").MeebonCryptoKeyPair) => (req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => void;
2
+ export declare const EncryptionMiddleware: ({ privateKey, publicKey }: import("../types/global").MeebonCryptoKeyPair) => import("express").RequestHandler;
3
3
  export default 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;AAE9C,eAAO,MAAM,oBAAoB,gMAAuB,CAAC;AACzD,eAAe,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,eAAO,MAAM,oBAAoB,gHAAuB,CAAC;AACzD,eAAe,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -81,25 +81,30 @@ var MeebonCrypto = class _MeebonCrypto {
81
81
  // lib/ExpressEncryptionMiddleware.ts
82
82
  var encryptionMiddleware = ({ privateKey, publicKey }) => {
83
83
  return (req, res, next) => {
84
- if (req.body.data) {
84
+ if (req.body && req.body.data) {
85
85
  try {
86
- const encrypted = MeebonCrypto.encryptData(req.body.dataToEncrypt, publicKey);
86
+ const plainText = JSON.stringify(req.body.data);
87
+ const encrypted = MeebonCrypto.encryptData(plainText, publicKey);
87
88
  req.body.data = encrypted;
88
89
  } catch (error) {
89
- throw new Error(error?.message ?? error);
90
+ return next(new Error(error?.message || "Encryption failed."));
90
91
  }
91
92
  }
92
- const originalSend = res.send;
93
- res.send = function(body) {
93
+ const originalSend = res.send.bind(res);
94
+ res.send = (body) => {
94
95
  try {
95
- if (body && body.encryptedData) {
96
- const decrypted = MeebonCrypto.decryptData(body.encryptedData, privateKey);
97
- body.decryptedData = decrypted;
96
+ if (typeof body === "string") {
97
+ const decrypted = MeebonCrypto.decryptData(body, privateKey);
98
+ try {
99
+ body = JSON.parse(decrypted);
100
+ } catch {
101
+ body = decrypted;
102
+ }
98
103
  }
99
- return originalSend.call(this, body);
100
104
  } catch (error) {
101
- throw new Error(error?.message ?? error);
105
+ throw new Error(error?.message ?? "Decryption failed");
102
106
  }
107
+ return originalSend(body);
103
108
  };
104
109
  return next();
105
110
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meebon/meebon-crypto",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
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",