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

@@ -13,7 +13,8 @@ type AppConfig = {
postgresUsername: string,
postgresPassword: string,
postgresEnableSsl: boolean,
baseUrl?: string
baseUrl?: string,
encryptionKey: string
}
const appConfig: AppConfig = {
@@ -28,6 +29,7 @@ const appConfig: AppConfig = {
postgresUsername: process.env.POSTGRES_USERNAME || 'automatish_development_user',
postgresPassword: process.env.POSTGRES_PASSWORD,
postgresEnableSsl: process.env.POSTGRES_ENABLE_SSL === 'true' ? true : false,
encryptionKey: process.env.ENCRYPTION_KEY
}
const baseUrl = `${appConfig.protocol}://${appConfig.host}:${appConfig.port}`;

View File

@@ -19,6 +19,7 @@ const testConnectionResolver = async (params: Params, req: RequestWithCurrentUse
const isStillVerified = await appInstance.authenticationClient.isStillVerified();
connection = await connection.$query().patchAndFetch({
data: connection.data,
verified: isStillVerified
})

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;