@growy/strapi-plugin-encrypted-field 1.5.0 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@growy/strapi-plugin-encrypted-field",
3
- "version": "1.5.0",
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;
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
+ };