feat: Encrypt data column of connections (#105)
This commit is contained in:
@@ -9,3 +9,4 @@ POSTGRES_HOST=localhost
|
||||
POSTGRES_USERNAME=automatish_development_user
|
||||
POSTGRES_PASSWORD=
|
||||
POSTGRES_ENABLE_SSL=false
|
||||
ENCRYPTION_KEY=sample-encryption-key
|
||||
|
@@ -19,6 +19,7 @@
|
||||
"axios": "0.24.0",
|
||||
"bcrypt": "^5.0.1",
|
||||
"cors": "^2.8.5",
|
||||
"crypto-js": "^4.1.1",
|
||||
"debug": "~2.6.9",
|
||||
"discord.js": "13.2.0",
|
||||
"dotenv": "^10.0.0",
|
||||
@@ -63,6 +64,7 @@
|
||||
"devDependencies": {
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"@types/cors": "^2.8.12",
|
||||
"@types/crypto-js": "^4.0.2",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/http-errors": "^1.8.1",
|
||||
"@types/morgan": "^1.9.3",
|
||||
|
@@ -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}`;
|
||||
|
@@ -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
|
||||
})
|
||||
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user