UNPKG

prisma-encrypter

Version:

Lightweight encryption solution for Prisma

130 lines 7.04 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrismaEncrypter = void 0; const class_validator_1 = require("class-validator"); const tsyringe_1 = require("tsyringe"); const actions_1 = require("../ressources/actions"); const crypter_1 = require("./crypter"); const errors_1 = require("../ressources/errors"); const utils_1 = require("./utils"); let PrismaEncrypter = class PrismaEncrypter { constructor() { this.models = []; this.crypter = { encrypt: crypter_1.encrypt, decrypt: crypter_1.decrypt, }; this.rawEncrypt = (value, key, iv, algorithm) => { var _a; return (0, crypter_1.encrypt)(value, key || ((_a = this.global) === null || _a === void 0 ? void 0 : _a.key), iv, algorithm); }; this.rawDecrypt = (value, key, iv, algorithm) => { var _a; return (0, crypter_1.decrypt)(value, key || ((_a = this.global) === null || _a === void 0 ? void 0 : _a.key), iv, algorithm); }; } setOptions(options) { if (typeof options === 'string') { return this.setOptions({ global: { key: options, }, }); } const errors = (0, class_validator_1.validateSync)(options); if (errors.length > 0) throw errors; this.models = options.models; this.global = options.global; } addModels(models) { const errors = (0, class_validator_1.validateSync)(models); if (errors.length > 0) throw errors; this.models = this.models.filter((e) => !models.some((m) => m.model === e.model)); this.models.push(...models); } getIVKey(model, data) { var _a, _b; const field = (_a = model === null || model === void 0 ? void 0 : model.local) === null || _a === void 0 ? void 0 : _a.ivField; if (!field) return ((_b = model === null || model === void 0 ? void 0 : model.local) === null || _b === void 0 ? void 0 : _b.iv) || this.global.iv; if (!(field in data)) throw new Error(errors_1.EEncrypterErrors.MissingIVField); return data[field]; } transformData(method, model, data) { var _a, _b, _c, _d, _e, _f, _g; if (!data) return data; if (Array.isArray(data)) return data.map((d) => this.transformData(method, model, d)); const ivKey = model ? this.getIVKey(model, data) : this.global.iv; for (const field in data) { if (Array.isArray(data[field]) || (0, utils_1.isObject)(data[field])) { model = this.models.find((m) => field.includes(m.model.toLowerCase())); data[field] = model ? this.transformData(method, model, data[field]) : data[field]; continue; } else if (((_a = this.models) === null || _a === void 0 ? void 0 : _a.length) && !model) continue; if (typeof data[field] == 'string' && (!(model === null || model === void 0 ? void 0 : model.fields.length) || (model === null || model === void 0 ? void 0 : model.fields.includes(field)))) { try { data[field] = this.crypter[method](data[field], ((_b = model === null || model === void 0 ? void 0 : model.local) === null || _b === void 0 ? void 0 : _b.key) || ((_c = this.global) === null || _c === void 0 ? void 0 : _c.key), ivKey, ((_d = model === null || model === void 0 ? void 0 : model.local) === null || _d === void 0 ? void 0 : _d.algorithm) || ((_e = this.global) === null || _e === void 0 ? void 0 : _e.algorithm)); } catch (err) { console.error(method, field, data[field], (err === null || err === void 0 ? void 0 : err.message) || err); } } } if (method == 'decrypt' && ((_f = model === null || model === void 0 ? void 0 : model.local) === null || _f === void 0 ? void 0 : _f.ivField) && ((_g = model === null || model === void 0 ? void 0 : model.local) === null || _g === void 0 ? void 0 : _g.stripIvField)) delete data[model.local.ivField]; return data; } middleware(params, next) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; if (!((_a = this.global) === null || _a === void 0 ? void 0 : _a.key) && !((_b = this.models) === null || _b === void 0 ? void 0 : _b.length)) return next(params); const model = ((_c = this.models) === null || _c === void 0 ? void 0 : _c.length) ? this.models.find((m) => m.model === params.model) : null; if (actions_1.EMethodsToEncrypt[params.action]) { if (params === null || params === void 0 ? void 0 : params.args) for (const arg in params.args) { params.args[arg] = this.transformData('encrypt', model, params.args[arg]); } } const result = yield next(params); if (actions_1.EMethodsToDecrypt[params.action] && result) { try { let decrypted = this.transformData('decrypt', model, result); return decrypted; } catch (err) { console.error((err === null || err === void 0 ? void 0 : err.message) || err); } } return result; }); } }; exports.PrismaEncrypter = PrismaEncrypter; exports.PrismaEncrypter = PrismaEncrypter = __decorate([ (0, tsyringe_1.singleton)() ], PrismaEncrypter); //# sourceMappingURL=handler.js.map