File size: 1,716 Bytes
3206347 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import AES from 'crypto-js/aes.js';
import enc from 'crypto-js/enc-utf8.js';
import appConfig from '../config/app.js';
import Base from './base.js';
class AppAuthClient extends Base {
static tableName = 'app_auth_clients';
static jsonSchema = {
type: 'object',
required: ['name', 'appKey', 'formattedAuthDefaults'],
properties: {
id: { type: 'string', format: 'uuid' },
appKey: { type: 'string' },
active: { type: 'boolean' },
authDefaults: { type: ['string', 'null'] },
formattedAuthDefaults: { type: 'object' },
createdAt: { type: 'string' },
updatedAt: { type: 'string' },
},
};
encryptData() {
if (!this.eligibleForEncryption()) return;
this.authDefaults = AES.encrypt(
JSON.stringify(this.formattedAuthDefaults),
appConfig.encryptionKey
).toString();
delete this.formattedAuthDefaults;
}
decryptData() {
if (!this.eligibleForDecryption()) return;
this.formattedAuthDefaults = JSON.parse(
AES.decrypt(this.authDefaults, appConfig.encryptionKey).toString(enc)
);
}
eligibleForEncryption() {
return this.formattedAuthDefaults ? true : false;
}
eligibleForDecryption() {
return this.authDefaults ? true : false;
}
// TODO: Make another abstraction like beforeSave instead of using
// beforeInsert and beforeUpdate separately for the same operation.
async $beforeInsert(queryContext) {
await super.$beforeInsert(queryContext);
this.encryptData();
}
async $beforeUpdate(opt, queryContext) {
await super.$beforeUpdate(opt, queryContext);
this.encryptData();
}
async $afterFind() {
this.decryptData();
}
}
export default AppAuthClient;
|