En-/decryption fixed

This commit is contained in:
Torsten Schulz
2024-07-28 16:12:48 +02:00
parent 4c12303edc
commit 4b6ad3aefe
27 changed files with 3315 additions and 97 deletions

View File

@@ -1,25 +1,18 @@
import { sequelize } from '../../utils/sequelize.js';
import { DataTypes } from 'sequelize';
import bcrypt from 'bcrypt';
import { encrypt, generateIv } from '../../utils/encryption.js';
import { encrypt, decrypt } from '../../utils/encryption.js';
import crypto from 'crypto';
const User = sequelize.define('user', {
email: {
type: DataTypes.STRING,
type: DataTypes.BLOB, // Verwende BLOB, um die E-Mail als bytea zu speichern
allowNull: false,
unique: true,
set(value) {
if (value) {
const iv = generateIv();
this.setDataValue('iv', iv.toString('hex'));
this.setDataValue('email', encrypt(value, iv));
this.setDataValue('email', Buffer.from(encrypt(value), 'hex'));
}
}
},
iv: {
type: DataTypes.STRING,
allowNull: false
},
},
username: {
type: DataTypes.STRING,
@@ -46,6 +39,14 @@ const User = sequelize.define('user', {
hashedId: {
type: DataTypes.STRING,
allowNull: true
},
searchable: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
authCode: {
type: DataTypes.STRING,
allowNull: true
}
}, {
tableName: 'user',
@@ -57,6 +58,11 @@ const User = sequelize.define('user', {
user.hashedId = hashedId;
await user.save();
}
},
getterMethods: {
email() {
return decrypt(this.getDataValue('email').toString('hex'));
}
}
});

View File

@@ -63,13 +63,14 @@ const UserParam = sequelize.define('user_param', {
UserParam.upsertParam = async function (userId, paramTypeId, value) {
try {
const val = value !== null && value !== undefined ? value.toString() : '';
const [userParam, created] = await UserParam.findOrCreate({
where: { userId, paramTypeId },
defaults: { value }
defaults: { value: val }
});
if (!created) {
userParam.value = value;
userParam.value = value !== null && value !== undefined ? value.toString() : '';
await userParam.save();
}
} catch (error) {