@growy/strapi-plugin-encrypted-field 1.5.0 → 1.7.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 +19 -3
- package/server/index.js +4 -0
- package/server/middlewares/decrypt.js +43 -0
package/package.json
CHANGED
package/server/bootstrap.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
const { encrypt, decrypt, validateValue, isEncryptedField } = require('./utils/crypto');
|
|
2
2
|
|
|
3
3
|
module.exports = ({ strapi }) => {
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
// Registrar lifecycles para cada content type que tenga campos cifrados
|
|
5
|
+
const models = Object.values(strapi.contentTypes);
|
|
6
|
+
|
|
7
|
+
models.forEach((model) => {
|
|
8
|
+
const hasEncryptedFields = Object.values(model.attributes || {}).some(isEncryptedField);
|
|
9
|
+
|
|
10
|
+
if (!hasEncryptedFields) return;
|
|
11
|
+
|
|
12
|
+
const uid = model.uid;
|
|
13
|
+
|
|
14
|
+
strapi.log.info(`Registrando lifecycles de cifrado para ${uid}`);
|
|
15
|
+
|
|
16
|
+
// Registrar lifecycle específico para este content type
|
|
17
|
+
strapi.db.lifecycles.subscribe({
|
|
18
|
+
models: [uid],
|
|
19
|
+
|
|
20
|
+
async beforeCreate(event) {
|
|
6
21
|
if (!event.model?.uid) return;
|
|
7
22
|
|
|
8
23
|
const { data } = event.params;
|
|
@@ -105,6 +120,7 @@ module.exports = ({ strapi }) => {
|
|
|
105
120
|
}
|
|
106
121
|
}
|
|
107
122
|
}
|
|
108
|
-
|
|
123
|
+
},
|
|
124
|
+
});
|
|
109
125
|
});
|
|
110
126
|
};
|
package/server/index.js
CHANGED
|
@@ -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
|
+
};
|