Skip to content

Commit 1199248

Browse files
committed
✨ Add support to query encrypted data
Signed-off-by: SAMI BETTAYEB <[email protected]>
1 parent 2994e4e commit 1199248

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/postgresql.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
588588
return whereClause;
589589
};
590590

591+
PostgreSQL.prototype.getEncryptionFields = function(modelDefinition) {
592+
if(modelDefinition
593+
&& modelDefinition.settings
594+
&& modelDefinition.settings.mixins
595+
&& modelDefinition.settings.mixins.Encryption
596+
&& modelDefinition.settings.mixins.Encryption.fields){
597+
return modelDefinition.settings.mixins.Encryption.fields
598+
}
599+
return []
600+
}
601+
591602
/**
592603
* @private
593604
* @param model
@@ -606,6 +617,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
606617
const self = this;
607618
const props = self.getModelDefinition(model).properties;
608619

620+
const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model))
609621
const whereStmts = [];
610622
for (const key in where) {
611623
const stmt = new ParameterizedSQL('', []);
@@ -646,7 +658,18 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
646658
}
647659
// eslint-disable one-var
648660
let expression = where[key];
649-
const columnName = self.columnEscaped(model, key);
661+
let columnName = self.columnEscaped(model, key);
662+
if(encryptedFields.includes(key)){
663+
columnName = `convert_from(
664+
decrypt_iv(
665+
DECODE(${key},'hex')::bytea,
666+
decode('${process.env.ENCRYPTION_HEX_KEY}','hex')::bytea,
667+
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
668+
'aes'
669+
),
670+
'utf8'
671+
)`
672+
}
650673
// eslint-enable one-var
651674
if (expression === null || expression === undefined) {
652675
stmt.merge(columnName + ' IS NULL');

0 commit comments

Comments
 (0)