-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Encryption predicate #71
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ export function encrypt<T extends ObjectLiteral>(entity: any): any { | |
let { propertyName, mode, target } = columnMetadata; | ||
let options: ExtendedColumnOptions = columnMetadata.options; | ||
let encrypt = options.encrypt; | ||
if (encrypt?.encryptionPredicate && !encrypt?.encryptionPredicate(entity)) { | ||
continue; | ||
} | ||
if ( | ||
encrypt && | ||
mode === 'regular' && | ||
|
@@ -42,6 +45,9 @@ export function decrypt<T extends ObjectLiteral>(entity: any): any { | |
let { propertyName, mode, target } = columnMetadata; | ||
let options: ExtendedColumnOptions = columnMetadata.options; | ||
let encrypt = options.encrypt; | ||
if (encrypt?.encryptionPredicate && !encrypt?.encryptionPredicate(entity)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you merge this with |
||
continue; | ||
} | ||
if ( | ||
encrypt && | ||
mode === 'regular' && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ export interface EncryptionOptions { | |
iv?: string; //// For testing mainly. | ||
authTagLength?: number; | ||
looseMatching?: boolean; | ||
encryptionPredicate?: (entity: any) => boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I (respectfully) think we should not do that... Because the term Unless you disagree with this of course I'm happy to make the change. |
||
} |
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"); | ||
}); | ||
}); |
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you merge this with
let encrypt = options.encrypt
above or with the condition below?