prisma-field-encryption
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
import { CloakKeychain, ParsedCloakKey } from '@47ng/cloak'; | ||
import type { Configuration, MiddlewareParams } from './types'; | ||
import type { Configuration, FieldsConfiguration, MiddlewareParams } from './types'; | ||
export interface EncryptionConfiguration { | ||
@@ -11,5 +11,5 @@ encryptOnWrite: boolean; | ||
} | ||
export declare function configureEncryption<Models extends string, Actions extends string>(params: MiddlewareParams<Models, Actions>, config: Configuration<Models>): EncryptionConfiguration; | ||
export declare function configureKeys<Models extends string>(config: Configuration<Models>): KeysConfiguration; | ||
export declare function encryptOnWrite<Models extends string>(data: any, keys: KeysConfiguration, config: Configuration<Models>, model?: Models): any; | ||
export declare function configureEncryption(params: MiddlewareParams, fields: FieldsConfiguration): EncryptionConfiguration; | ||
export declare function configureKeys(config: Configuration): KeysConfiguration; | ||
export declare function encryptOnWrite(data: any, keys: KeysConfiguration, fields: FieldsConfiguration, model?: string): any; | ||
export declare function decryptOnRead(data: any, keys: KeysConfiguration): any; |
@@ -14,3 +14,3 @@ "use strict"; | ||
}); | ||
function configureEncryption(params, config) { | ||
function configureEncryption(params, fields) { | ||
if (!params.model) { | ||
@@ -26,3 +26,3 @@ // Model is not available for raw SQL & execute. | ||
const model = String(params.model); | ||
const isModelEnabled = Object.entries(config.fields).some(([key, value]) => key.split('.')[0] === model && value === true); | ||
const isModelEnabled = Object.entries(fields).some(([key, value]) => key.split('.')[0] === model && value === true); | ||
const isWriteOperation = [ | ||
@@ -63,3 +63,3 @@ 'create', | ||
const lowercaseFirstLetter = (input) => input[0].toLowerCase() + input.slice(1); | ||
function encryptOnWrite(data, keys, config, model) { | ||
function encryptOnWrite(data, keys, fields, model) { | ||
// Deep-clone the input to avoid mutating it. | ||
@@ -70,3 +70,3 @@ // (eg: if reusing objects across queries) | ||
// to match the `include` behaviour in Prisma queries. | ||
const encryptedFieldPaths = Object.entries(config.fields) | ||
const encryptedFieldPaths = Object.entries(fields) | ||
.filter(([, value]) => value === true) | ||
@@ -73,0 +73,0 @@ .flatMap(([key]) => { |
import type { Configuration, Middleware } from './types'; | ||
export declare function fieldEncryptionMiddleware<Models extends string, Actions extends string>(config: Configuration<Models>): Middleware<Models, Actions>; | ||
export declare function fieldEncryptionMiddleware(config?: Configuration): Middleware; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fieldEncryptionMiddleware = void 0; | ||
const dmmf_1 = require("./dmmf"); | ||
const encryption_1 = require("./encryption"); | ||
function fieldEncryptionMiddleware(config) { | ||
function fieldEncryptionMiddleware(config = {}) { | ||
// This will throw if the encryption key is missing | ||
// or if anything is invalid. | ||
const keys = (0, encryption_1.configureKeys)(config); | ||
const fields = (0, dmmf_1.configureFields)(); | ||
return async (params, next) => { | ||
const encryptionConfig = (0, encryption_1.configureEncryption)(params, config); | ||
const encryptionConfig = (0, encryption_1.configureEncryption)(params, fields); | ||
const logger = process.env.PRISMA_FIELD_ENCRYPTION_LOG === 'false' | ||
@@ -26,2 +28,3 @@ ? { | ||
context: { | ||
fields, | ||
...keys, | ||
@@ -33,3 +36,3 @@ ...encryptionConfig | ||
if (encryptionConfig.encryptOnWrite) { | ||
const data = (0, encryption_1.encryptOnWrite)(params.args.data, keys, config, params.model); | ||
const data = (0, encryption_1.encryptOnWrite)(params.args.data, keys, fields, params.model && String(params.model)); | ||
params.args.data = data; | ||
@@ -36,0 +39,0 @@ } |
@@ -1,13 +0,8 @@ | ||
export interface MiddlewareParams<Models, Actions> { | ||
model?: Models; | ||
action: Actions; | ||
args: any; | ||
dataPath: string[]; | ||
runInTransaction: boolean; | ||
} | ||
export interface Configuration<Models extends string> { | ||
fields: Record<`${Models}.${string}`, true>; | ||
import { Prisma } from '@prisma/client'; | ||
export declare type MiddlewareParams = Prisma.MiddlewareParams; | ||
export declare type Middleware = Prisma.Middleware; | ||
export interface Configuration { | ||
encryptionKey?: string; | ||
decryptionKeys?: string[]; | ||
} | ||
export declare type Middleware<Models, Actions> = (params: MiddlewareParams<Models, Actions>, next: (params: MiddlewareParams<Models, Actions>) => Promise<any>) => Promise<any>; | ||
export declare type FieldsConfiguration = Record<string, boolean>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// export type Middleware<Models, Actions> = ( | ||
// params: MiddlewareParams<Models, Actions>, | ||
// next: (params: MiddlewareParams<Models, Actions>) => Promise<any> | ||
// ) => Promise<any> |
{ | ||
"name": "prisma-field-encryption", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"description": "Transparent field-level encryption at rest for Prisma", | ||
@@ -39,2 +39,5 @@ "main": "dist/index.js", | ||
}, | ||
"peerDependencies": { | ||
"@prisma/client": "^3.5.0" | ||
}, | ||
"devDependencies": { | ||
@@ -48,3 +51,2 @@ "@commitlint/config-conventional": "^15.0.0", | ||
"jest": "^27.3.1", | ||
"nodemon": "^2.0.15", | ||
"npm-run-all": "^4.1.5", | ||
@@ -56,5 +58,8 @@ "ts-jest": "^27.0.7", | ||
"jest": { | ||
"verbose": true, | ||
"preset": "ts-jest/presets/js-with-ts", | ||
"verbose": true, | ||
"testEnvironment": "node" | ||
"testEnvironment": "node", | ||
"testMatch": [ | ||
"<rootDir>/src/**/*.test.[jt]s?(x)" | ||
] | ||
}, | ||
@@ -61,0 +66,0 @@ "prettier": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15953
11
14
263
0
4