@duongtrungnguyen/nestro
Version:
Service registry for Nest JS
96 lines • 3.97 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __decorateClass = (decorators, target, key, kind) => {
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
for (var i = decorators.length - 1, decorator; i >= 0; i--)
if (decorator = decorators[i])
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
if (kind && result) __defProp(target, key, result);
return result;
};
var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
var key_service_exports = {};
__export(key_service_exports, {
KeyService: () => KeyService
});
module.exports = __toCommonJS(key_service_exports);
var import_fs = require("fs");
var import_crypto = require("crypto");
var import_common = require("@nestjs/common");
var import_path = require("path");
var import_common2 = require("../../common");
var import_constants = require("../constants");
let KeyService = class {
constructor(options) {
this.options = options;
if (options.initKeys) this.ensureKeyPair();
}
ensureKeyPair() {
const keyDir = (0, import_path.dirname)(this.options.privateKeyPath);
if (!(0, import_fs.existsSync)(keyDir)) {
(0, import_common2.debugLog)(KeyService.name, "\u{1F4C2} Creating key directory:", keyDir);
(0, import_fs.mkdirSync)(keyDir, { recursive: true });
}
if ((0, import_fs.existsSync)(this.options.privateKeyPath) && (0, import_fs.existsSync)(this.options.publicKeyPath)) {
(0, import_common2.debugLog)(KeyService.name, "Key Pair already exists at:", this.options.privateKeyPath);
return;
}
const { privateKey, publicKey } = (0, import_crypto.generateKeyPairSync)("rsa", {
modulusLength: 2048,
publicKeyEncoding: { type: "spki", format: "pem" },
privateKeyEncoding: { type: "pkcs8", format: "pem" }
});
(0, import_fs.writeFileSync)(this.options.privateKeyPath, privateKey);
(0, import_fs.writeFileSync)(this.options.publicKeyPath, publicKey);
(0, import_common2.debugLog)(KeyService.name, "Key Pair generated successfully!");
}
getPrivateKey() {
return (0, import_fs.readFileSync)(this.options.privateKeyPath, "utf-8");
}
getPublicKey() {
return (0, import_fs.readFileSync)(this.options.publicKeyPath, "utf-8");
}
signData(data) {
const privateKey = this.getPrivateKey();
const signObj = (0, import_crypto.sign)("sha256", Buffer.from((0, import_common2.normalizeJson)(data)), {
key: privateKey,
padding: import_crypto.constants.RSA_PKCS1_PSS_PADDING
});
return signObj.toString("base64");
}
verifyData(data, signature, publicKey) {
return (0, import_crypto.verify)(
"sha256",
Buffer.from((0, import_common2.normalizeJson)(data)),
{
key: publicKey,
padding: import_crypto.constants.RSA_PKCS1_PSS_PADDING
},
Buffer.from(signature, "base64")
);
}
};
KeyService = __decorateClass([
(0, import_common.Injectable)(),
__decorateParam(0, (0, import_common.Inject)(import_constants.KEY_SERVICE_OPTIONS))
], KeyService);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
KeyService
});
//# sourceMappingURL=key.service.js.map