@growy/strapi-plugin-encrypted-field 1.8.0 → 1.9.0
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 +6 -3
- package/server/middlewares/decrypt.js +45 -22
- package/server/register.js +6 -0
package/package.json
CHANGED
package/server/bootstrap.js
CHANGED
|
@@ -113,8 +113,9 @@ module.exports = ({ strapi }) => {
|
|
|
113
113
|
if (Object.prototype.hasOwnProperty.call(result, key)) {
|
|
114
114
|
const value = result[key];
|
|
115
115
|
if (typeof value === 'string' && value) {
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
const decrypted = decrypt(value, strapi);
|
|
117
|
+
strapi.log.info(`Descifrando campo ${key} en ${event.model.uid}: ${value.substring(0, 20)}... -> ${decrypted}`);
|
|
118
|
+
result[key] = decrypted;
|
|
118
119
|
}
|
|
119
120
|
}
|
|
120
121
|
}
|
|
@@ -135,7 +136,9 @@ module.exports = ({ strapi }) => {
|
|
|
135
136
|
if (Object.prototype.hasOwnProperty.call(item, key)) {
|
|
136
137
|
const value = item[key];
|
|
137
138
|
if (typeof value === 'string' && value) {
|
|
138
|
-
|
|
139
|
+
const decrypted = decrypt(value, strapi);
|
|
140
|
+
strapi.log.info(`Descifrando campo ${key}: ${value.substring(0, 20)}... -> ${decrypted}`);
|
|
141
|
+
item[key] = decrypted;
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
144
|
}
|
|
@@ -4,40 +4,63 @@ module.exports = (config, { strapi }) => {
|
|
|
4
4
|
return async (ctx, next) => {
|
|
5
5
|
await next();
|
|
6
6
|
|
|
7
|
-
if (!ctx.body
|
|
7
|
+
if (!ctx.body) return;
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// Función recursiva para descifrar campos en cualquier nivel de anidación
|
|
10
|
+
const decryptRecursive = (obj, modelUid = null) => {
|
|
11
|
+
if (!obj || typeof obj !== 'object') return;
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
let model;
|
|
16
|
-
try {
|
|
17
|
-
model = strapi.getModel(contentType);
|
|
18
|
-
} catch (e) {
|
|
13
|
+
// Si es un array, procesar cada elemento
|
|
14
|
+
if (Array.isArray(obj)) {
|
|
15
|
+
obj.forEach(item => decryptRecursive(item, modelUid));
|
|
19
16
|
return;
|
|
20
17
|
}
|
|
21
18
|
|
|
22
|
-
|
|
19
|
+
// Detectar si es un componente por el campo __component
|
|
20
|
+
let currentModelUid = modelUid;
|
|
21
|
+
if (obj.__component) {
|
|
22
|
+
currentModelUid = obj.__component;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Obtener el modelo si tenemos un UID
|
|
26
|
+
let model = null;
|
|
27
|
+
if (currentModelUid) {
|
|
28
|
+
try {
|
|
29
|
+
model = strapi.getModel(currentModelUid) || strapi.components[currentModelUid];
|
|
30
|
+
} catch (e) {
|
|
31
|
+
// Ignorar si no se encuentra el modelo
|
|
32
|
+
}
|
|
33
|
+
}
|
|
23
34
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
// Descifrar campos del modelo actual
|
|
36
|
+
if (model?.attributes) {
|
|
37
|
+
for (const [key, attribute] of Object.entries(model.attributes)) {
|
|
38
|
+
if (isEncryptedField(attribute) && obj[key] && typeof obj[key] === 'string') {
|
|
39
|
+
try {
|
|
40
|
+
const decrypted = decrypt(obj[key], strapi);
|
|
41
|
+
strapi.log.info(`[Middleware] Descifrando ${key} en ${currentModelUid}: ${obj[key].substring(0, 20)}... -> ${decrypted}`);
|
|
42
|
+
obj[key] = decrypted;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
|
|
45
|
+
}
|
|
32
46
|
}
|
|
33
47
|
}
|
|
34
48
|
}
|
|
49
|
+
|
|
50
|
+
// Procesar recursivamente todos los valores del objeto
|
|
51
|
+
for (const value of Object.values(obj)) {
|
|
52
|
+
if (value && typeof value === 'object') {
|
|
53
|
+
decryptRecursive(value, currentModelUid);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
35
56
|
};
|
|
36
57
|
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
// Procesar ctx.body.data si existe
|
|
59
|
+
if (ctx.body.data) {
|
|
60
|
+
decryptRecursive(ctx.body.data);
|
|
39
61
|
} else {
|
|
40
|
-
|
|
62
|
+
// Procesar ctx.body directamente
|
|
63
|
+
decryptRecursive(ctx.body);
|
|
41
64
|
}
|
|
42
65
|
};
|
|
43
66
|
};
|
package/server/register.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
module.exports = ({ strapi }) => {
|
|
2
|
+
const decryptMiddleware = require('./middlewares/decrypt');
|
|
3
|
+
|
|
4
|
+
strapi.server.use(decryptMiddleware({}, { strapi }));
|
|
5
|
+
|
|
6
|
+
strapi.log.info('✓ Middleware de descifrado registrado');
|
|
7
|
+
|
|
2
8
|
strapi.customFields.register({
|
|
3
9
|
name: 'encrypted-text',
|
|
4
10
|
plugin: 'encrypted-field',
|