@meebon/meebon-crypto 1.3.0 → 1.5.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,4 +1,4 @@
|
|
|
1
|
-
import { RequestHandler } from 'express';
|
|
1
|
+
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
2
|
import { MeebonCryptoKeyPair } from '../types/global';
|
|
3
3
|
/**
|
|
4
4
|
* Middleware to encrypt response bodies if the request header 'x-require-encryption' is present.
|
|
@@ -38,6 +38,6 @@ declare function encryptionMiddleware({ publicKey }: Omit<MeebonCryptoKeyPair, "
|
|
|
38
38
|
* @param {string} param.privateKey - The private key used for decryption.
|
|
39
39
|
* @returns {RequestHandler} The middleware function.
|
|
40
40
|
*/
|
|
41
|
-
declare function decryptMiddleware({ privateKey }: Omit<MeebonCryptoKeyPair, "publicKey">):
|
|
41
|
+
declare function decryptMiddleware({ privateKey }: Omit<MeebonCryptoKeyPair, "publicKey">): (req: Request, res: Response, next: NextFunction) => void;
|
|
42
42
|
export { encryptionMiddleware, decryptMiddleware };
|
|
43
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.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// lib/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
MeebonCrypto: () => MeebonCrypto,
|
|
34
|
+
decryptMiddleware: () => decryptMiddleware,
|
|
35
|
+
encryptionMiddleware: () => encryptionMiddleware
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
|
|
39
|
+
// lib/MeebonCrypto.ts
|
|
40
|
+
var import_node_forge = __toESM(require("node-forge"), 1);
|
|
41
|
+
var MeebonCrypto = class _MeebonCrypto {
|
|
42
|
+
privateKeyPem;
|
|
43
|
+
publicKeyPem;
|
|
44
|
+
privateKey;
|
|
45
|
+
publicKey;
|
|
46
|
+
schema;
|
|
47
|
+
constructor({ privateKeyPem, publicKeyPem, schema }) {
|
|
48
|
+
this.privateKeyPem = privateKeyPem;
|
|
49
|
+
this.publicKeyPem = publicKeyPem;
|
|
50
|
+
this.privateKey = import_node_forge.default.pki.privateKeyFromPem(privateKeyPem);
|
|
51
|
+
this.publicKey = import_node_forge.default.pki.publicKeyFromPem(publicKeyPem);
|
|
52
|
+
this.schema = schema ?? "RSAES-PKCS1-V1_5";
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Initializes a new instance of MeebonCrypto.
|
|
56
|
+
* @param props - The crypto properties including keys and optional encryption scheme.
|
|
57
|
+
* @returns {MeebonCrypto} The initialized MeebonCrypto instance.
|
|
58
|
+
*/
|
|
59
|
+
static init(props) {
|
|
60
|
+
return new _MeebonCrypto(props);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Encrypts data using the instance's public key.
|
|
64
|
+
* @param {string} plainText - The data to encrypt.
|
|
65
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
66
|
+
*/
|
|
67
|
+
encrypt(plainText) {
|
|
68
|
+
const encryptedBytes = this.publicKey.encrypt(plainText, this.schema);
|
|
69
|
+
return import_node_forge.default.util.encode64(encryptedBytes);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Decrypts data using the instance's private key.
|
|
73
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
74
|
+
* @returns {string} The decrypted data.
|
|
75
|
+
*/
|
|
76
|
+
decrypt(encryptedData) {
|
|
77
|
+
const encryptedBytes = import_node_forge.default.util.decode64(encryptedData);
|
|
78
|
+
return this.privateKey.decrypt(encryptedBytes, this.schema);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Generates an RSA key pair with the specified key size.
|
|
82
|
+
*
|
|
83
|
+
* @param {number} [length=3072] - The length of the RSA key in bits (defaults to 3072).
|
|
84
|
+
* @returns {MeebonCryptoKeyPair} An object containing the generated public and private keys in PEM format.
|
|
85
|
+
*/
|
|
86
|
+
static generateKeyPair(length = 3072) {
|
|
87
|
+
const keyPair = import_node_forge.default.pki.rsa.generateKeyPair({ bits: length, e: 65537 });
|
|
88
|
+
return {
|
|
89
|
+
publicKey: import_node_forge.default.pki.publicKeyToPem(keyPair.publicKey),
|
|
90
|
+
privateKey: import_node_forge.default.pki.privateKeyToPem(keyPair.privateKey)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Encrypts data with a provided public key.
|
|
95
|
+
* @param {string} data - The data to encrypt.
|
|
96
|
+
* @param {string} publicKey - The public key in PEM format.
|
|
97
|
+
* @param {string} [scheme='RSA-OAEP'] - The encryption scheme to use (defaults to 'RSA-OAEP').
|
|
98
|
+
* @returns {string} The Base64-encoded encrypted data.
|
|
99
|
+
*/
|
|
100
|
+
static encryptData(data, publicKey, scheme = "RSA-OAEP") {
|
|
101
|
+
const publicKeyObj = import_node_forge.default.pki.publicKeyFromPem(publicKey);
|
|
102
|
+
const encryptedBytes = publicKeyObj.encrypt(data, scheme);
|
|
103
|
+
return import_node_forge.default.util.encode64(encryptedBytes);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Decrypts data with a provided private key.
|
|
107
|
+
* @param {string} encryptedData - The Base64-encoded encrypted data.
|
|
108
|
+
* @param {string} privateKey - The private key in PEM format.
|
|
109
|
+
* @param {string} [scheme='RSA-OAEP'] - The decryption scheme to use (defaults to 'RSA-OAEP').
|
|
110
|
+
* @returns {string} The decrypted data.
|
|
111
|
+
*/
|
|
112
|
+
static decryptData(encryptedData, privateKey, scheme = "RSA-OAEP") {
|
|
113
|
+
const privateKeyObj = import_node_forge.default.pki.privateKeyFromPem(privateKey);
|
|
114
|
+
const decodedBytes = import_node_forge.default.util.decode64(encryptedData);
|
|
115
|
+
return privateKeyObj.decrypt(decodedBytes, scheme);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// lib/ExpressEncryptionMiddleware.ts
|
|
120
|
+
function encryptionMiddleware({ publicKey }) {
|
|
121
|
+
return (req, res, next) => {
|
|
122
|
+
const originalSend = res.send.bind(res);
|
|
123
|
+
const originalJson = res.json.bind(res);
|
|
124
|
+
res.send = function(body) {
|
|
125
|
+
let bodyString = typeof body === "object" ? JSON.stringify(body) : body.toString();
|
|
126
|
+
const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
|
|
127
|
+
res.set("Content-Type", "text/plain");
|
|
128
|
+
return originalSend(encryptedBody);
|
|
129
|
+
};
|
|
130
|
+
res.json = function(body) {
|
|
131
|
+
const bodyString = JSON.stringify(body);
|
|
132
|
+
const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
|
|
133
|
+
res.set("Content-Type", "text/plain");
|
|
134
|
+
return originalSend(encryptedBody);
|
|
135
|
+
};
|
|
136
|
+
next();
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function decryptMiddleware({ privateKey }) {
|
|
140
|
+
return (req, res, next) => {
|
|
141
|
+
if (typeof req.body === "string") {
|
|
142
|
+
try {
|
|
143
|
+
const decryptedText = MeebonCrypto.decryptData(req.body, privateKey);
|
|
144
|
+
req.body = JSON.parse(decryptedText);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
res.status(400).json({ error: "Invalid encrypted data" });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
next();
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
153
|
+
0 && (module.exports = {
|
|
154
|
+
MeebonCrypto,
|
|
155
|
+
decryptMiddleware,
|
|
156
|
+
encryptionMiddleware
|
|
157
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -84,31 +84,23 @@ function encryptionMiddleware({ publicKey }) {
|
|
|
84
84
|
const originalSend = res.send.bind(res);
|
|
85
85
|
const originalJson = res.json.bind(res);
|
|
86
86
|
res.send = function(body) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return originalSend(encryptedBody);
|
|
92
|
-
} else {
|
|
93
|
-
return originalSend(body);
|
|
94
|
-
}
|
|
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);
|
|
95
91
|
};
|
|
96
92
|
res.json = function(body) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return originalSend(encryptedBody);
|
|
102
|
-
} else {
|
|
103
|
-
return originalJson(body);
|
|
104
|
-
}
|
|
93
|
+
const bodyString = JSON.stringify(body);
|
|
94
|
+
const encryptedBody = MeebonCrypto.encryptData(bodyString, publicKey);
|
|
95
|
+
res.set("Content-Type", "text/plain");
|
|
96
|
+
return originalSend(encryptedBody);
|
|
105
97
|
};
|
|
106
98
|
next();
|
|
107
99
|
};
|
|
108
100
|
}
|
|
109
101
|
function decryptMiddleware({ privateKey }) {
|
|
110
102
|
return (req, res, next) => {
|
|
111
|
-
if (
|
|
103
|
+
if (typeof req.body === "string") {
|
|
112
104
|
try {
|
|
113
105
|
const decryptedText = MeebonCrypto.decryptData(req.body, privateKey);
|
|
114
106
|
req.body = JSON.parse(decryptedText);
|
package/package.json
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meebon/meebon-crypto",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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
|
-
"main": "dist/index.
|
|
8
|
+
"main": "dist/index.cjs",
|
|
9
|
+
"module": "dist/index.js",
|
|
9
10
|
"type": "module",
|
|
10
|
-
"
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
11
19
|
"directories": {
|
|
12
20
|
"lib": "lib",
|
|
13
21
|
"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
|
+
}
|