Skip to content

Commit

Permalink
Merge pull request #71 from basz/encryption-predicate
Browse files Browse the repository at this point in the history
Encryption predicate
  • Loading branch information
generalpiston authored Apr 26, 2024
2 parents 6fbb444 + c22b2d7 commit c8ea1ac
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function encrypt<T extends ObjectLiteral>(entity: any): any {
let options: ExtendedColumnOptions = columnMetadata.options;
let encrypt = options.encrypt;
if (
encrypt &&
encrypt && !(encrypt?.encryptionPredicate && !encrypt?.encryptionPredicate(entity)) &&
mode === 'regular' &&
(encrypt.looseMatching || entity.constructor === target)
) {
Expand Down Expand Up @@ -43,8 +43,8 @@ export function decrypt<T extends ObjectLiteral>(entity: any): any {
let options: ExtendedColumnOptions = columnMetadata.options;
let encrypt = options.encrypt;
if (
encrypt &&
mode === 'regular' &&
encrypt && !(encrypt?.encryptionPredicate && !encrypt?.encryptionPredicate(entity)) &&
mode === "regular" &&
(encrypt.looseMatching || entity.constructor === target)
) {
if (entity[propertyName]) {
Expand Down
1 change: 1 addition & 0 deletions src/options/EncryptionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface EncryptionOptions {
iv?: string; //// For testing mainly.
authTagLength?: number;
looseMatching?: boolean;
encryptionPredicate?: (entity: any) => boolean;
}
46 changes: 46 additions & 0 deletions test/encryption-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from "chai";
import { encrypt, decrypt } from "../src/entity";
import { getConnection } from "./utils";
import ColumnOptionsEntity4 from "./entities/ColumnOptionsEntity4";

describe("Column Options - Encryption Predicate", function () {
this.timeout(10000);

before(async function () {
await getConnection();
});

it("should encrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = true;
result.secret = "test";
encrypt(result);
expect(result.secret).to.equal(
"/1rBkZBCSx2I+UGe+UmuVhKzmHsDDv0EvRtMBFiaE3A="
);
});

it("should not encrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = false;
result.secret = "test";
encrypt(result);
expect(result.secret).to.equal("test");
});

it("should decrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = true;
result.secret = "/1rBkZBCSx2I+UGe+UmuVhKzmHsDDv0EvRtMBFiaE3A=";
decrypt(result);
expect(result.secret).to.equal("test");
});

it("should not decrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = false;
result.secret = "test";
decrypt(result);
expect(result.secret).to.equal("test");
});
});
25 changes: 25 additions & 0 deletions test/entities/ColumnOptionsEntity4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { ExtendedColumnOptions } from "../../src/options";

@Entity()
export default class ColumnOptionsEntity4 extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: "boolean" })
enablePredicate: boolean;

@Column(<ExtendedColumnOptions>{
type: "varchar",
nullable: false,
encrypt: {
key: "e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61",
algorithm: "aes-256-cbc",
ivLength: 16,
iv: "ff5ac19190424b1d88f9419ef949ae56",
encryptionPredicate: (entity: ColumnOptionsEntity4) =>
entity.enablePredicate,
},
})
secret: string;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"declaration": true,
"downlevelIteration": true
},
"include": ["src"],
"include": ["src", "test"],
"exclude": ["tmp", "temp", "lib", "node_modules"]
}

0 comments on commit c8ea1ac

Please sign in to comment.