@growy/strapi-plugin-encrypted-field 1.7.0 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server/bootstrap.js +99 -93
package/package.json
CHANGED
package/server/bootstrap.js
CHANGED
|
@@ -1,125 +1,131 @@
|
|
|
1
1
|
const { encrypt, decrypt, validateValue, isEncryptedField } = require('./utils/crypto');
|
|
2
2
|
|
|
3
3
|
module.exports = ({ strapi }) => {
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
const models = Object.values(strapi.contentTypes);
|
|
6
6
|
|
|
7
|
+
strapi.log.info(`Total de modelos encontrados: ${models.length}`);
|
|
8
|
+
|
|
7
9
|
models.forEach((model) => {
|
|
8
|
-
const
|
|
10
|
+
const encryptedFields = Object.entries(model.attributes || {})
|
|
11
|
+
.filter(([key, attr]) => isEncryptedField(attr))
|
|
12
|
+
.map(([key]) => key);
|
|
9
13
|
|
|
10
|
-
if (
|
|
14
|
+
if (encryptedFields.length === 0) return;
|
|
11
15
|
|
|
12
16
|
const uid = model.uid;
|
|
13
17
|
|
|
14
|
-
strapi.log.info(
|
|
18
|
+
strapi.log.info(`✓ Registrando lifecycles de cifrado para ${uid} - Campos: ${encryptedFields.join(', ')}`);
|
|
15
19
|
|
|
16
|
-
|
|
20
|
+
|
|
17
21
|
strapi.db.lifecycles.subscribe({
|
|
18
22
|
models: [uid],
|
|
19
23
|
|
|
20
24
|
async beforeCreate(event) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
const { data } = event.params;
|
|
26
|
+
|
|
27
|
+
strapi.log.info(`[beforeCreate] Evento recibido para ${event.model?.uid}`);
|
|
28
|
+
strapi.log.info(`[beforeCreate] Data recibida: ${JSON.stringify(data)}`);
|
|
29
|
+
|
|
30
|
+
if (!event.model?.uid) return;
|
|
31
|
+
|
|
32
|
+
const currentModel = strapi.getModel(event.model.uid);
|
|
33
|
+
|
|
34
|
+
if (!currentModel?.attributes) return;
|
|
35
|
+
|
|
36
|
+
for (const [key, attribute] of Object.entries(currentModel.attributes)) {
|
|
37
|
+
if (!isEncryptedField(attribute)) continue;
|
|
33
38
|
|
|
34
|
-
|
|
35
|
-
if (typeof value === 'string' && value.split(':').length === 3) {
|
|
36
|
-
strapi.log.info(`Campo ${key} ya está cifrado, saltando cifrado`);
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
+
strapi.log.info(`[beforeCreate] Campo cifrado detectado: ${key}`);
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
42
|
+
const value = data[key];
|
|
43
|
+
|
|
44
|
+
strapi.log.info(`[beforeCreate] Valor del campo ${key}: ${value}`);
|
|
45
|
+
|
|
46
|
+
if (value === null || value === undefined || value === '') {
|
|
47
|
+
strapi.log.info(`[beforeCreate] Valor vacío, saltando cifrado`);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const validation = validateValue(value, attribute);
|
|
52
|
+
if (!validation.valid) {
|
|
53
|
+
throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
strapi.log.info(`✓ Cifrando campo ${key} con valor: ${value}`);
|
|
57
|
+
data[key] = encrypt(value, strapi);
|
|
58
|
+
strapi.log.info(`✓ Campo ${key} cifrado exitosamente`);
|
|
43
59
|
}
|
|
44
|
-
|
|
45
|
-
strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
|
|
46
|
-
data[key] = encrypt(value, strapi);
|
|
47
60
|
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
61
|
+
},
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!validation.valid) {
|
|
73
|
-
throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
|
|
63
|
+
async beforeUpdate(event) {
|
|
64
|
+
if (!event.model?.uid) return;
|
|
65
|
+
|
|
66
|
+
const { data } = event.params;
|
|
67
|
+
const currentModel = strapi.getModel(event.model.uid);
|
|
68
|
+
|
|
69
|
+
if (!currentModel?.attributes) return;
|
|
70
|
+
|
|
71
|
+
for (const [key, attribute] of Object.entries(currentModel.attributes)) {
|
|
72
|
+
if (!isEncryptedField(attribute)) continue;
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
74
|
+
const value = data[key];
|
|
75
|
+
if (value === null || value === undefined || value === '') continue;
|
|
76
|
+
|
|
77
|
+
const validation = validateValue(value, attribute);
|
|
78
|
+
if (!validation.valid) {
|
|
79
|
+
throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
|
|
83
|
+
data[key] = encrypt(value, strapi);
|
|
74
84
|
}
|
|
75
|
-
|
|
76
|
-
strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
|
|
77
|
-
data[key] = encrypt(value, strapi);
|
|
78
85
|
}
|
|
79
|
-
}
|
|
80
|
-
},
|
|
86
|
+
},
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
async afterFindOne(event) {
|
|
89
|
+
const { result } = event;
|
|
90
|
+
if (!result) return;
|
|
91
|
+
if (!event.model?.uid) return;
|
|
92
|
+
|
|
93
|
+
const currentModel = strapi.getModel(event.model.uid);
|
|
94
|
+
|
|
95
|
+
if (!currentModel?.attributes) return;
|
|
96
|
+
|
|
97
|
+
for (const [key, attribute] of Object.entries(currentModel.attributes)) {
|
|
98
|
+
if (!isEncryptedField(attribute)) continue;
|
|
99
|
+
if (Object.prototype.hasOwnProperty.call(result, key)) {
|
|
100
|
+
const value = result[key];
|
|
101
|
+
if (typeof value === 'string' && value) {
|
|
102
|
+
strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}`);
|
|
103
|
+
result[key] = decrypt(value, strapi);
|
|
104
|
+
}
|
|
98
105
|
}
|
|
99
106
|
}
|
|
100
|
-
}
|
|
101
|
-
},
|
|
107
|
+
},
|
|
102
108
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
109
|
+
async afterFindMany(event) {
|
|
110
|
+
const { result } = event;
|
|
111
|
+
if (!result || !Array.isArray(result)) return;
|
|
112
|
+
if (!event.model?.uid) return;
|
|
113
|
+
|
|
114
|
+
const currentModel = strapi.getModel(event.model.uid);
|
|
115
|
+
|
|
116
|
+
if (!currentModel?.attributes) return;
|
|
117
|
+
|
|
118
|
+
for (const item of result) {
|
|
119
|
+
for (const [key, attribute] of Object.entries(currentModel.attributes)) {
|
|
120
|
+
if (!isEncryptedField(attribute)) continue;
|
|
121
|
+
if (Object.prototype.hasOwnProperty.call(item, key)) {
|
|
122
|
+
const value = item[key];
|
|
123
|
+
if (typeof value === 'string' && value) {
|
|
124
|
+
item[key] = decrypt(value, strapi);
|
|
125
|
+
}
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
128
|
}
|
|
122
|
-
}
|
|
123
129
|
},
|
|
124
130
|
});
|
|
125
131
|
});
|