Verschlüsselung korrigiert

This commit is contained in:
Torsten Schulz
2024-07-21 15:12:56 +02:00
parent 597761cb15
commit 12d66d6f9c
4 changed files with 98 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
import User from './user.js';
import UserParamType from '../type/user_param.js';
import { encrypt, decrypt, generateIv } from '../../utils/encryption.js';
const UserParam = sequelize.define('user_param', {
userId: {
@@ -18,20 +19,52 @@ const UserParam = sequelize.define('user_param', {
references: {
model: UserParamType,
key: 'id'
},
}
},
value: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
if (value) {
const iv = generateIv();
this.setDataValue('iv', iv.toString('hex'));
this.setDataValue('value', encrypt(value, iv));
}
}
},
iv: {
type: DataTypes.STRING,
allowNull: false
}
}, {
tableName: 'user_param',
schema: 'community',
underscored: true
underscored: true,
hooks: {
beforeSave: (userParam) => {
if (userParam.value && !userParam.iv) {
const iv = generateIv();
userParam.iv = iv.toString('hex');
userParam.value = encrypt(userParam.value, iv);
}
},
afterFind: (userParams) => {
if (userParams) {
if (Array.isArray(userParams)) {
userParams.forEach((userParam) => {
const iv = Buffer.from(userParam.iv, 'hex');
userParam.value = decrypt(userParam.value, iv);
});
} else {
const iv = Buffer.from(userParams.iv, 'hex');
userParams.value = decrypt(userParams.value, iv);
}
}
}
}
});
UserParam.belongsTo(User, { foreignKey: 'userId' });
UserParam.belongsTo(UserParamType, { foreignKey: 'param_type_id' });
UserParam.belongsTo(UserParamType, { foreignKey: 'paramTypeId' });
export default UserParam;