feat: Encrypt data column of connections (#105)

This commit is contained in:
Ömer Faruk Aydın
2021-11-28 22:08:35 +01:00
committed by GitHub
parent 69bea588e4
commit f949eca3c4
6 changed files with 44 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
import { QueryContext, ModelOptions } from 'objection';
import { AES, enc } from 'crypto-js';
import Base from './base'
import User from './user'
import appConfig from '../config/app';
class Connection extends Base {
id!: number
@@ -34,6 +37,30 @@ class Connection extends Base {
},
}
})
encryptData() {
this.data = AES.encrypt(JSON.stringify(this.data), appConfig.encryptionKey).toString();
}
decryptData() {
this.data = JSON.parse(AES.decrypt(this.data, appConfig.encryptionKey).toString(enc.Utf8));
}
// TODO: Make another abstraction like beforeSave instead of using
// beforeInsert and beforeUpdate separately for the same operation.
async $beforeInsert(queryContext: QueryContext) {
await super.$beforeInsert(queryContext);
this.encryptData();
}
async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext) {
await super.$beforeUpdate(opt, queryContext);
this.encryptData();
}
async $afterFind(queryContext: QueryContext) {
this.decryptData();
}
}
export default Connection;