Skip to content

Commit

Permalink
✨ Add support to query encrypted data
Browse files Browse the repository at this point in the history
  • Loading branch information
SAMIBETTAYEB committed Jun 18, 2021
1 parent e7c121b commit 001a9d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 24 additions & 1 deletion lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,17 @@ PostgreSQL.prototype.buildWhere = function(model, where) {
return whereClause;
};

PostgreSQL.prototype.getEncryptionFields = function(modelDefinition) {
if(modelDefinition
&& modelDefinition.settings
&& modelDefinition.settings.mixins
&& modelDefinition.settings.mixins.Encryption
&& modelDefinition.settings.mixins.Encryption.fields){
return modelDefinition.settings.mixins.Encryption.fields
}
return []
}

/**
* @private
* @param model
Expand All @@ -591,6 +602,7 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
const self = this;
const props = self.getModelDefinition(model).properties;

const encryptedFields = this.getEncryptionFields(this.getModelDefinition(model))
const whereStmts = [];
for (const key in where) {
const stmt = new ParameterizedSQL('', []);
Expand Down Expand Up @@ -631,7 +643,18 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
}
// eslint-disable one-var
let expression = where[key];
const columnName = self.columnEscaped(model, key);
let columnName = self.columnEscaped(model, key);
if(encryptedFields.includes(key)){
columnName = `convert_from(
decrypt_iv(
DECODE(${key},'hex')::bytea,
decode('${process.env.ENCRYPTION_HEX_KEY}','hex')::bytea,
decode('${process.env.ENCRYPTION_HEX_IV}','hex')::bytea,
'aes'
),
'utf8'
)`
}
// eslint-enable one-var
if (expression === null || expression === undefined) {
stmt.merge(columnName + ' IS NULL');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-connector-postgresql",
"version": "3.9.1",
"version": "3.10.0",
"description": "Loopback PostgreSQL Connector",
"engines": {
"node": ">=8"
Expand Down

0 comments on commit 001a9d9

Please sign in to comment.