@growy/strapi-plugin-encrypted-field 1.4.1 → 1.6.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.
@@ -28,7 +28,9 @@ const Input = (props) => {
28
28
  });
29
29
  };
30
30
 
31
- const label = intlLabel?.id ? formatMessage(intlLabel) : (intlLabel || name);
31
+ // Extraer solo el nombre del campo sin prefijos
32
+ const fieldName = name.includes('.') ? name.split('.').pop() : name;
33
+ const label = intlLabel?.id ? formatMessage(intlLabel) : (intlLabel || fieldName);
32
34
 
33
35
  return (
34
36
  <Field.Root
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@growy/strapi-plugin-encrypted-field",
3
- "version": "1.4.1",
3
+ "version": "1.6.0",
4
4
  "description": "Campo personalizado de texto cifrado para Strapi",
5
5
  "strapi": {
6
6
  "name": "encrypted-field",
@@ -1,6 +1,53 @@
1
1
  const { encrypt, decrypt, validateValue, isEncryptedField } = require('./utils/crypto');
2
2
 
3
3
  module.exports = ({ strapi }) => {
4
+ // Registrar middleware de descifrado
5
+ strapi.server.use(async (ctx, next) => {
6
+ await next();
7
+
8
+ if (!ctx.body || !ctx.body.data) return;
9
+
10
+ const decryptData = (data) => {
11
+ if (!data || typeof data !== 'object') return;
12
+
13
+ // Intentar obtener el modelo del content type
14
+ const uid = ctx.state?.route?.info?.apiName
15
+ ? `api::${ctx.state.route.info.apiName}.${ctx.state.route.info.apiName}`
16
+ : null;
17
+
18
+ if (!uid) return;
19
+
20
+ let model;
21
+ try {
22
+ model = strapi.getModel(uid);
23
+ } catch (e) {
24
+ return;
25
+ }
26
+
27
+ if (!model?.attributes) return;
28
+
29
+ for (const [key, attribute] of Object.entries(model.attributes)) {
30
+ if (!isEncryptedField(attribute)) continue;
31
+
32
+ if (data[key] && typeof data[key] === 'string') {
33
+ try {
34
+ const decrypted = decrypt(data[key], strapi);
35
+ strapi.log.info(`Descifrando campo ${key} en respuesta API`);
36
+ data[key] = decrypted;
37
+ } catch (error) {
38
+ strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
39
+ }
40
+ }
41
+ }
42
+ };
43
+
44
+ if (Array.isArray(ctx.body.data)) {
45
+ ctx.body.data.forEach(decryptData);
46
+ } else {
47
+ decryptData(ctx.body.data);
48
+ }
49
+ });
50
+
4
51
  strapi.db.lifecycles.subscribe({
5
52
  async beforeCreate(event) {
6
53
  if (!event.model?.uid) return;
@@ -14,13 +61,20 @@ module.exports = ({ strapi }) => {
14
61
  if (!isEncryptedField(attribute)) continue;
15
62
  if (Object.prototype.hasOwnProperty.call(data, key)) {
16
63
  const value = data[key];
17
- if (value === null || value === undefined) continue;
64
+ if (value === null || value === undefined || value === '') continue;
65
+
66
+ // No cifrar si ya está cifrado (formato: iv:authTag:encrypted)
67
+ if (typeof value === 'string' && value.split(':').length === 3) {
68
+ strapi.log.info(`Campo ${key} ya está cifrado, saltando cifrado`);
69
+ continue;
70
+ }
18
71
 
19
72
  const validation = validateValue(value, attribute);
20
73
  if (!validation.valid) {
21
74
  throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
22
75
  }
23
76
 
77
+ strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
24
78
  data[key] = encrypt(value, strapi);
25
79
  }
26
80
  }
@@ -38,13 +92,20 @@ module.exports = ({ strapi }) => {
38
92
  if (!isEncryptedField(attribute)) continue;
39
93
  if (Object.prototype.hasOwnProperty.call(data, key)) {
40
94
  const value = data[key];
41
- if (value === null || value === undefined) continue;
95
+ if (value === null || value === undefined || value === '') continue;
96
+
97
+ // No cifrar si ya está cifrado (formato: iv:authTag:encrypted)
98
+ if (typeof value === 'string' && value.split(':').length === 3) {
99
+ strapi.log.info(`Campo ${key} ya está cifrado, saltando cifrado`);
100
+ continue;
101
+ }
42
102
 
43
103
  const validation = validateValue(value, attribute);
44
104
  if (!validation.valid) {
45
105
  throw new Error(`Validación fallida para el campo "${key}": ${validation.error}`);
46
106
  }
47
107
 
108
+ strapi.log.info(`Cifrando campo ${key} en ${event.model.uid}`);
48
109
  data[key] = encrypt(value, strapi);
49
110
  }
50
111
  }
package/server/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  const register = require('./register');
2
2
  const bootstrap = require('./bootstrap');
3
+ const decrypt = require('./middlewares/decrypt');
3
4
 
4
5
  module.exports = {
5
6
  register,
6
7
  bootstrap,
8
+ middlewares: {
9
+ decrypt,
10
+ },
7
11
  };
@@ -0,0 +1,43 @@
1
+ const { decrypt, isEncryptedField } = require('../utils/crypto');
2
+
3
+ module.exports = (config, { strapi }) => {
4
+ return async (ctx, next) => {
5
+ await next();
6
+
7
+ if (!ctx.body || !ctx.body.data) return;
8
+
9
+ const decryptData = (data) => {
10
+ if (!data || typeof data !== 'object') return;
11
+
12
+ const contentType = ctx.params?.model || ctx.state?.route?.info?.type;
13
+ if (!contentType) return;
14
+
15
+ let model;
16
+ try {
17
+ model = strapi.getModel(contentType);
18
+ } catch (e) {
19
+ return;
20
+ }
21
+
22
+ if (!model?.attributes) return;
23
+
24
+ for (const [key, attribute] of Object.entries(model.attributes)) {
25
+ if (!isEncryptedField(attribute)) continue;
26
+
27
+ if (data[key] && typeof data[key] === 'string') {
28
+ try {
29
+ data[key] = decrypt(data[key], strapi);
30
+ } catch (error) {
31
+ strapi.log.error(`Error descifrando campo ${key}: ${error.message}`);
32
+ }
33
+ }
34
+ }
35
+ };
36
+
37
+ if (Array.isArray(ctx.body.data)) {
38
+ ctx.body.data.forEach(decryptData);
39
+ } else {
40
+ decryptData(ctx.body.data);
41
+ }
42
+ };
43
+ };