@ejfdelgado/ejflab-back 1.1.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/LICENSE +201 -0
- package/README.md +2 -0
- package/package.json +57 -0
- package/srv/AudIASrv.mjs +218 -0
- package/srv/AuthorizationSrv.mjs +213 -0
- package/srv/ComputeEngineSrv.mjs +289 -0
- package/srv/EmailHandler.mjs +54 -0
- package/srv/Image2MeshSrv.mjs +101 -0
- package/srv/ImagiationSrv.mjs +408 -0
- package/srv/KeysSrv.mjs +104 -0
- package/srv/MainHandler.mjs +140 -0
- package/srv/MainReplacer.mjs +77 -0
- package/srv/MfaSrv.mjs +266 -0
- package/srv/MilvusSrv.mjs +152 -0
- package/srv/MinioSrv.mjs +154 -0
- package/srv/MongoSrv.mjs +320 -0
- package/srv/MyError.mjs +48 -0
- package/srv/MyFileService.mjs +392 -0
- package/srv/MyFileServiceLocal.mjs +177 -0
- package/srv/MyPdf.mjs +37 -0
- package/srv/MyShell.mjs +205 -0
- package/srv/MySqlSrv.mjs +43 -0
- package/srv/Network.mjs +111 -0
- package/srv/OpenCVSrv.mjs +27 -0
- package/srv/PageSrv.mjs +234 -0
- package/srv/PayUSrv.mjs +186 -0
- package/srv/PayUSrvConstants.mjs +46 -0
- package/srv/PostgresSrv.mjs +109 -0
- package/srv/SecretsSrv.mjs +126 -0
- package/srv/SocketIOCall.mjs +494 -0
- package/srv/TupleSrv.mjs +141 -0
- package/srv/UtilesSrv.mjs +8 -0
- package/srv/callprocessors/AskIceServersProcessor.mjs +14 -0
- package/srv/callprocessors/AskRoomProcessor.mjs +15 -0
- package/srv/callprocessors/CallUserProcessor.mjs +17 -0
- package/srv/callprocessors/ChatSetSawProcessor.mjs +42 -0
- package/srv/callprocessors/CheckSrcResponseProcessor.mjs +28 -0
- package/srv/callprocessors/ClientChangeProcessor.mjs +19 -0
- package/srv/callprocessors/CloseVideoChatProcessor.mjs +16 -0
- package/srv/callprocessors/DestroyModelProcessor.mjs +16 -0
- package/srv/callprocessors/DisconnectProcessor.mjs +53 -0
- package/srv/callprocessors/GenericProcessor.mjs +7 -0
- package/srv/callprocessors/GetModelProcessor.mjs +11 -0
- package/srv/callprocessors/IncludeOtherPeersProcessor.mjs +12 -0
- package/srv/callprocessors/LoadFlowChartProcessor.mjs +103 -0
- package/srv/callprocessors/MakeAnswerProcessor.mjs +17 -0
- package/srv/callprocessors/OnIceCandidateProcessor.mjs +13 -0
- package/srv/callprocessors/OpenVideoChatProcessor.mjs +17 -0
- package/srv/callprocessors/PauseFlowChartProcessor.mjs +16 -0
- package/srv/callprocessors/ProcessResponseProcessor.mjs +123 -0
- package/srv/callprocessors/ReadSrcResponseProcessor.mjs +30 -0
- package/srv/callprocessors/RegisterProcessorProcessor.mjs +23 -0
- package/srv/callprocessors/RegisterSourceProcessor.mjs +22 -0
- package/srv/callprocessors/SendChatProcessor.mjs +71 -0
- package/srv/callprocessors/StartFlowChartProcessor.mjs +48 -0
- package/srv/callprocessors/StopFlowChartProcessor.mjs +16 -0
- package/srv/callprocessors/SubscribemeProcessor.mjs +13 -0
- package/srv/callprocessors/UpdateMyInformationProcessor.mjs +30 -0
- package/srv/common/FirebasConfig.mjs +160 -0
- package/srv/common/General.mjs +69 -0
- package/srv/common/MimeTypeMap.mjs +142 -0
- package/srv/common/MyStore.mjs +169 -0
- package/srv/common/Usuario.mjs +101 -0
- package/srv/common/Utilidades.mjs +43 -0
package/srv/PayUSrv.mjs
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
import axios from "axios";
|
2
|
+
import geoip from 'geoip-lite';
|
3
|
+
import { PayUSrvConstants } from "./PayUSrvConstants.mjs";
|
4
|
+
import { SecretsSrv } from "./SecretsSrv.mjs";
|
5
|
+
|
6
|
+
/**
|
7
|
+
* It is important to validate the length and numbers of credit cards by franchise, together with the security codes.
|
8
|
+
*/
|
9
|
+
export class PayUSrv {
|
10
|
+
static DEFAULTS = {
|
11
|
+
"timeout": 60000,//Maximum said by payu
|
12
|
+
"language": PayUSrvConstants.LANGUAGES[0].val,
|
13
|
+
"country": PayUSrvConstants.COUNTRIES[0].val,
|
14
|
+
"currency": PayUSrvConstants.CURRENCIES[0].val,
|
15
|
+
//"keysprefix": "edelgado@panal.co-payu-",
|
16
|
+
"keysprefix": "info@pais.tv+",
|
17
|
+
//"keysprefix": "edgar.jose.fernando.delgado@gmail.com+",
|
18
|
+
};
|
19
|
+
static mode = "dev";
|
20
|
+
static endpoint = PayUSrvConstants.ENDPOINTS[PayUSrv.mode];
|
21
|
+
|
22
|
+
/*
|
23
|
+
[
|
24
|
+
{
|
25
|
+
txt: "AMEX",
|
26
|
+
val: "12"
|
27
|
+
},
|
28
|
+
{
|
29
|
+
txt: "PSE",
|
30
|
+
val: "254"
|
31
|
+
},
|
32
|
+
]
|
33
|
+
*/
|
34
|
+
static async paymentmethods(req, res, next) {
|
35
|
+
const clientIp = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
|
36
|
+
const response = await PayUSrv.getPaymentMethodsLocal(clientIp);
|
37
|
+
res.status(200).json(response.data.paymentMethods).end();
|
38
|
+
}
|
39
|
+
/*
|
40
|
+
[
|
41
|
+
{
|
42
|
+
val: "fd8273ef-2fe8-4598-91d6-f09ecdff42c9",
|
43
|
+
txt: "ALIANZA FIDUCIARIA S A",
|
44
|
+
pseCode: "5016"
|
45
|
+
},
|
46
|
+
]
|
47
|
+
*/
|
48
|
+
static async psebanks(req, res, next) {
|
49
|
+
const clientIp = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
|
50
|
+
const response = await PayUSrv.getPSEBankListLocal(clientIp);
|
51
|
+
res.status(200).json(response.data.banks).end();
|
52
|
+
}
|
53
|
+
static async ping(req, res, next) {
|
54
|
+
const response = await PayUSrv.pingLocal();
|
55
|
+
res.status(200).json(response.data).end();
|
56
|
+
}
|
57
|
+
static async getPSEBankListLocal(clientIp) {
|
58
|
+
const country = PayUSrv.getCurrentCountryFromIp(clientIp);
|
59
|
+
PayUSrv.checkValidCountry(country);
|
60
|
+
const request = {
|
61
|
+
"command": "GET_BANKS_LIST",
|
62
|
+
"bankListInformation": {
|
63
|
+
paymentMethod: "PSE",
|
64
|
+
paymentCountry: country,
|
65
|
+
}
|
66
|
+
};
|
67
|
+
const response = await PayUSrv.generalRequest(request);
|
68
|
+
PayUSrv.checkReponseError(response);
|
69
|
+
const NO_PSE_CODES = ["0"];
|
70
|
+
response.data.banks = response.data.banks.filter((el) => {
|
71
|
+
return NO_PSE_CODES.indexOf(el.pseCode) < 0;
|
72
|
+
}).map((el) => {
|
73
|
+
return { val: el.id, txt: el.description, pseCode: el.pseCode };
|
74
|
+
});
|
75
|
+
return response;
|
76
|
+
}
|
77
|
+
static async pingLocal() {
|
78
|
+
const request = {
|
79
|
+
"command": "PING",
|
80
|
+
};
|
81
|
+
const response = await PayUSrv.generalRequest(request);
|
82
|
+
PayUSrv.checkReponseError(response);
|
83
|
+
return response;
|
84
|
+
}
|
85
|
+
static async getPaymentMethodsLocal(clientIp) {
|
86
|
+
const country = PayUSrv.getCurrentCountryFromIp(clientIp);
|
87
|
+
PayUSrv.checkValidCountry(country);
|
88
|
+
const request = {
|
89
|
+
"command": "GET_PAYMENT_METHODS",
|
90
|
+
};
|
91
|
+
const response = await PayUSrv.generalRequest(request);
|
92
|
+
PayUSrv.checkReponseError(response);
|
93
|
+
// Se debe filtrar por país actual
|
94
|
+
response.data.paymentMethods = response.data.paymentMethods.filter((oneMethod) => {
|
95
|
+
return oneMethod.country == country && oneMethod.enabled;
|
96
|
+
}).map((actual) => {
|
97
|
+
return { txt: actual.description, val: actual.id };
|
98
|
+
});
|
99
|
+
|
100
|
+
return response;
|
101
|
+
}
|
102
|
+
static async generalRequest(extra) {
|
103
|
+
const credentials = await PayUSrv.getCredentials();
|
104
|
+
const request = {
|
105
|
+
"test": PayUSrv.isTest(),
|
106
|
+
"language": PayUSrv.DEFAULTS.language,
|
107
|
+
|
108
|
+
"merchant": {
|
109
|
+
"apiLogin": credentials.payu_api_login,
|
110
|
+
"apiKey": credentials.payu_api_key,
|
111
|
+
}
|
112
|
+
};
|
113
|
+
Object.assign(request, extra);
|
114
|
+
// Call the service...
|
115
|
+
const url = PayUSrv.endpoint.serviceCgi;
|
116
|
+
const options = {
|
117
|
+
timeout: PayUSrv.DEFAULTS.timeout,
|
118
|
+
headers: {
|
119
|
+
'Content-Type': 'application/json; charset=utf-8',
|
120
|
+
'Accept': 'application/json',
|
121
|
+
},
|
122
|
+
};
|
123
|
+
const response = await new Promise((resolve, reject) => {
|
124
|
+
axios.post(url, request, options)
|
125
|
+
.then(res => { resolve(res) })
|
126
|
+
.catch(error => {
|
127
|
+
if (error.code === 'ECONNABORTED') {
|
128
|
+
reject(error);
|
129
|
+
} else {
|
130
|
+
reject(error);
|
131
|
+
}
|
132
|
+
});
|
133
|
+
});
|
134
|
+
|
135
|
+
//console.log(response.data);
|
136
|
+
//console.log(`response.status = ${response.status}`);
|
137
|
+
//console.log(`response.statusText = ${response.statusText}`);
|
138
|
+
//console.log(response.headers);
|
139
|
+
//console.log(response.config);
|
140
|
+
|
141
|
+
if ([200].indexOf(response.status) < 0) {
|
142
|
+
throw new Error(response.statusText);
|
143
|
+
}
|
144
|
+
|
145
|
+
return response;
|
146
|
+
}
|
147
|
+
static getCurrentCountryFromIp(clientIp) {
|
148
|
+
clientIp = "186.102.39.136";
|
149
|
+
const detail = geoip.lookup(clientIp);
|
150
|
+
//console.log(`clientIp = ${clientIp} detail.country = ${detail.country}`);
|
151
|
+
if (detail == null) {
|
152
|
+
//throw new Error("No se puede determinar en qué país te encuentras.");
|
153
|
+
return PayUSrv.DEFAULTS.country;
|
154
|
+
} else {
|
155
|
+
// 2 letter ISO-3166-1 country code
|
156
|
+
return detail.country;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
static checkReponseError(response) {
|
160
|
+
if (response.data.code !== "SUCCESS") {
|
161
|
+
if (typeof response.data.error == "string") {
|
162
|
+
throw new Error(response.data.error);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
static checkValidCountry(country) {
|
167
|
+
if (PayUSrvConstants.VALID_COUNTRIES.indexOf(country) < 0) {
|
168
|
+
throw new Error(`Tu país ${country} no acepta pagos por este medio.`);
|
169
|
+
}
|
170
|
+
}
|
171
|
+
static setMode(mode) {
|
172
|
+
if (["dev", "pro"].indexOf(mode) < 0) {
|
173
|
+
throw Error("Se debe seleccionar un modo dev o pro");
|
174
|
+
}
|
175
|
+
PayUSrv.mode = mode;
|
176
|
+
PayUSrv.endpoint = PayUSrvConstants.ENDPOINTS[PayUSrv.mode];
|
177
|
+
}
|
178
|
+
static async getCredentials() {
|
179
|
+
const respuesta = await SecretsSrv.localRead(["payu_api_login", "payu_api_key"], PayUSrv.DEFAULTS.keysprefix);
|
180
|
+
const mapa = respuesta.mapa;
|
181
|
+
return mapa;
|
182
|
+
}
|
183
|
+
static isTest() {
|
184
|
+
return !(PayUSrv.mode == "pro");
|
185
|
+
}
|
186
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
// https://d1ril7hq6s7m5z.cloudfront.net/latam/en/docs/getting-started/response-codes-and-variables.html
|
3
|
+
export class PayUSrvConstants {
|
4
|
+
static ENDPOINTS = {
|
5
|
+
// Credenciales de prueba?
|
6
|
+
// https://developers.payulatam.com/latam/es/docs/getting-started/test-your-solution.html
|
7
|
+
"dev": {
|
8
|
+
"serviceCgi": "https://sandbox.api.payulatam.com/payments-api/4.0/service.cgi",
|
9
|
+
"paymentsUrl": "https://sandbox.api.payulatam.com/payments-api/",
|
10
|
+
"reportsUrl": "https://sandbox.api.payulatam.com/reports-api/",
|
11
|
+
|
12
|
+
},
|
13
|
+
"pro": {
|
14
|
+
"serviceCgi": "https://api.payulatam.com/payments-api/4.0/service.cgi",
|
15
|
+
"paymentsUrl": "https://api.payulatam.com/payments-api/",
|
16
|
+
"reportsUrl": "https://api.payulatam.com/reports-api/",
|
17
|
+
}
|
18
|
+
}
|
19
|
+
//https://d1ril7hq6s7m5z.cloudfront.net/latam/en/docs/getting-started/response-codes-and-variables.html#processing-countries
|
20
|
+
static VALID_COUNTRIES = ["AR", "BR", "CL", "CO", "MX", "PA", "PE"];
|
21
|
+
static DOCUMENT_TYPES = [
|
22
|
+
{ txt: "Cédula de ciudadanía", val: "CC" },
|
23
|
+
{ txt: "Cédula de extrangería", val: "CE" },
|
24
|
+
{ txt: "Teléfono móvil", val: "CEL" },
|
25
|
+
{ txt: "Nit", val: "NIT" },
|
26
|
+
{ txt: "Registro civil", val: "RC" },
|
27
|
+
{ txt: "Tarjeta de identidad", val: "TI" },
|
28
|
+
];
|
29
|
+
static LANGUAGES = [
|
30
|
+
{ txt: "Español", val: "es" },
|
31
|
+
{ txt: "English", val: "en" },
|
32
|
+
{ txt: "Português", val: "pt" },
|
33
|
+
];
|
34
|
+
static COUNTRIES = [
|
35
|
+
{ txt: "Colombia", val: "CO" },
|
36
|
+
];
|
37
|
+
static CURRENCIES = [
|
38
|
+
{ txt: "Peso Colombiano", val: "COP" },
|
39
|
+
{ txt: "Dolar Estadounidense", val: "USD" },
|
40
|
+
{ txt: "Peso Argentino", val: "ARS" },
|
41
|
+
{ txt: "Real Brasilero", val: "BRL" },
|
42
|
+
{ txt: "Peso Chileno", val: "CLP" },
|
43
|
+
{ txt: "Peso Mexicano", val: "MXN" },
|
44
|
+
{ txt: "Sol Peruano", val: "PEN" },
|
45
|
+
];
|
46
|
+
}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import pg from 'pg'
|
2
|
+
import fs from "fs";
|
3
|
+
import { MyTemplate } from '@ejfdelgado/ejflab-common/src/MyTemplate.js';
|
4
|
+
const { Pool } = pg
|
5
|
+
|
6
|
+
|
7
|
+
export class PostgresSrv {
|
8
|
+
static renderer = new MyTemplate();
|
9
|
+
static pool = null;
|
10
|
+
static {
|
11
|
+
PostgresSrv.renderer.registerFunction("noQuotes", PostgresSrv.noQuotes);
|
12
|
+
PostgresSrv.renderer.registerFunction("sanitizeText", PostgresSrv.sanitizeText);
|
13
|
+
const types = pg.types;
|
14
|
+
types.setTypeParser(types.builtins.INT8, function (val) {
|
15
|
+
const bigNumber = BigInt(val);
|
16
|
+
if (bigNumber > Number.MAX_SAFE_INTEGER) {
|
17
|
+
// Fallback...
|
18
|
+
return 0;
|
19
|
+
}
|
20
|
+
return Number(bigNumber);
|
21
|
+
});
|
22
|
+
}
|
23
|
+
static noQuotes(val, ...args) {
|
24
|
+
return val.replace(/'/g, "''");
|
25
|
+
}
|
26
|
+
static sanitizeText(val, ...args) {
|
27
|
+
let text = val;
|
28
|
+
text = PostgresSrv.noQuotes(text);
|
29
|
+
return text;
|
30
|
+
}
|
31
|
+
static getConnectionParams() {
|
32
|
+
const host = process.env.POSTGRES_HOST || "postgres";
|
33
|
+
const port = parseInt(process.env.POSTGRES_PORT || "5432");
|
34
|
+
const database = process.env.POSTGRES_DB || "nogales";
|
35
|
+
const user = process.env.POSTGRES_USER || "user";
|
36
|
+
const password = process.env.POSTGRES_PASSWORD || "pass";
|
37
|
+
return {
|
38
|
+
host,
|
39
|
+
port,
|
40
|
+
database,
|
41
|
+
user,
|
42
|
+
password
|
43
|
+
};
|
44
|
+
}
|
45
|
+
|
46
|
+
static async executeTextInTransaction(sql, model = {}) {
|
47
|
+
const pool = PostgresSrv.getPool();
|
48
|
+
const client = await pool.connect();
|
49
|
+
await client.query('BEGIN')
|
50
|
+
try {
|
51
|
+
// Insert into media
|
52
|
+
const result = await PostgresSrv.executeText(sql, model, client);
|
53
|
+
await client.query('COMMIT');
|
54
|
+
return result;
|
55
|
+
} catch (e) {
|
56
|
+
await client.query('ROLLBACK');
|
57
|
+
throw e
|
58
|
+
} finally {
|
59
|
+
client.release();
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
static async executeText(sql, model = {}, client = null) {
|
64
|
+
const sqlRendered = PostgresSrv.renderer.render(sql, model);
|
65
|
+
console.log(sqlRendered);
|
66
|
+
let localClient = false;
|
67
|
+
if (!client) {
|
68
|
+
const pool = PostgresSrv.getPool();
|
69
|
+
localClient = true;
|
70
|
+
client = await pool.connect()
|
71
|
+
}
|
72
|
+
const result = await client.query(sqlRendered);
|
73
|
+
if (localClient) {
|
74
|
+
client.release();
|
75
|
+
}
|
76
|
+
return result;
|
77
|
+
}
|
78
|
+
|
79
|
+
static async executeFile(path, model = {}, client = null) {
|
80
|
+
const sql = fs.readFileSync(path, "utf-8");
|
81
|
+
return await PostgresSrv.executeText(sql, model, client);
|
82
|
+
}
|
83
|
+
|
84
|
+
static async executeFileInTransaction(path, model = {}) {
|
85
|
+
const sql = fs.readFileSync(path, "utf-8");
|
86
|
+
return await PostgresSrv.executeTextInTransaction(sql, model);
|
87
|
+
}
|
88
|
+
|
89
|
+
static getPool() {
|
90
|
+
if (PostgresSrv.pool == null) {
|
91
|
+
const params = PostgresSrv.getConnectionParams();
|
92
|
+
PostgresSrv.pool = new Pool({
|
93
|
+
user: params.user,
|
94
|
+
password: params.password,
|
95
|
+
host: params.host,
|
96
|
+
port: params.port,
|
97
|
+
database: params.database,
|
98
|
+
});
|
99
|
+
}
|
100
|
+
return PostgresSrv.pool;
|
101
|
+
}
|
102
|
+
|
103
|
+
static async test(req, res, next) {
|
104
|
+
const model = { other: "other" };
|
105
|
+
const result = await PostgresSrv.executeText("SELECT NOW() as ping", model);
|
106
|
+
const row = result.rows[0];
|
107
|
+
res.status(200).send({ row });
|
108
|
+
}
|
109
|
+
}
|
@@ -0,0 +1,126 @@
|
|
1
|
+
import { MyRoutes } from "@ejfdelgado/ejflab-common/src/MyRoutes.js";
|
2
|
+
import { ModuloDatoSeguroBack } from "@ejfdelgado/ejflab-common/src/ModuloDatoSeguroBack.mjs";
|
3
|
+
import { General } from "./common/General.mjs";
|
4
|
+
import { MyStore } from "./common/MyStore.mjs";
|
5
|
+
import { Utilidades } from "./common/Utilidades.mjs";
|
6
|
+
|
7
|
+
const SECRET_TYPE = "secret";
|
8
|
+
|
9
|
+
export class SecretsSrv {
|
10
|
+
static masterName = "dont_use";
|
11
|
+
static masterPub = "key_pub";
|
12
|
+
static masterPri = "key_pri";
|
13
|
+
static async getMasterKey(prefix) {
|
14
|
+
let response = null;
|
15
|
+
const dbData = await MyStore.readById(SECRET_TYPE, prefix + SecretsSrv.masterName);
|
16
|
+
if (dbData == undefined) {
|
17
|
+
// Debe crear una llave, guardarla y retornarla
|
18
|
+
response = ModuloDatoSeguroBack.generateKey(10);
|
19
|
+
await MyStore.createById(SECRET_TYPE, prefix + SecretsSrv.masterName, { val: response });
|
20
|
+
} else {
|
21
|
+
response = dbData.val;
|
22
|
+
}
|
23
|
+
let changed = "";
|
24
|
+
const intermediate = `${process.env.SPAN}`;
|
25
|
+
for (let i = 0; i < response.length; i++) {
|
26
|
+
const car = intermediate[i % intermediate.length];
|
27
|
+
changed += `${response[i]}${car}`;
|
28
|
+
}
|
29
|
+
return changed;
|
30
|
+
}
|
31
|
+
// SecretsSrv.localRead(["llave1", "llave2"], "edelgado@panal.co+");
|
32
|
+
// {mapa: {llave1: "XXXXX", llave2: undefined}, lista: [{key: "llave1", val: "XXXXX"}, undefined]}
|
33
|
+
static async localRead(arg, prefix) {
|
34
|
+
const master = await SecretsSrv.getMasterKey(prefix);
|
35
|
+
const argsPrefix = arg.map(v => prefix + v);
|
36
|
+
const response = await MyStore.readByIds(SECRET_TYPE, argsPrefix);
|
37
|
+
const lista = [];
|
38
|
+
const mapa = {};
|
39
|
+
for (let i = 0; i < argsPrefix.length; i++) {
|
40
|
+
const key = argsPrefix[i];
|
41
|
+
if (key in response) {
|
42
|
+
const payload = response[key];
|
43
|
+
const one = { key: arg[i] };
|
44
|
+
one.val = ModuloDatoSeguroBack.decifrarSimple(payload.val, master);
|
45
|
+
lista.push(one);
|
46
|
+
mapa[one.key] = one.val;
|
47
|
+
} else {
|
48
|
+
lista.push(undefined);
|
49
|
+
mapa[arg[i]] = undefined;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
return {
|
53
|
+
lista,
|
54
|
+
arg,
|
55
|
+
master,
|
56
|
+
mapa,
|
57
|
+
};
|
58
|
+
}
|
59
|
+
// SecretsSrv.localSave({"llave1": "secreto"}, "edelgado@panal.co+");
|
60
|
+
static async localSave(arg, prefix) {
|
61
|
+
const master = await SecretsSrv.getMasterKey(prefix);
|
62
|
+
const keys = Object.keys(arg);
|
63
|
+
const promises = [];
|
64
|
+
for (let i = 0; i < keys.length; i++) {
|
65
|
+
const key = keys[i];
|
66
|
+
let secreto = arg[key];
|
67
|
+
secreto = ModuloDatoSeguroBack.cifrarSimple(secreto, master);
|
68
|
+
promises.push(MyStore.createById(SECRET_TYPE, prefix + key, { val: secreto }));
|
69
|
+
}
|
70
|
+
await Promise.all(promises);
|
71
|
+
return {
|
72
|
+
arg,
|
73
|
+
master
|
74
|
+
};
|
75
|
+
}
|
76
|
+
static generateOldPrefix(req, res) {
|
77
|
+
const elpath = Utilidades.leerRefererPath(req);
|
78
|
+
const partes = MyRoutes.splitPageData(elpath);
|
79
|
+
const token = res.locals.token;
|
80
|
+
return `${token.email}${partes.pageType}/`;
|
81
|
+
}
|
82
|
+
static generatePrefix(req, res) {
|
83
|
+
const token = res.locals.token;
|
84
|
+
return `${token.email}+`;
|
85
|
+
}
|
86
|
+
static async read(req, res, next) {
|
87
|
+
const ans = {};
|
88
|
+
ans.keys = General.readParam(req, "key");
|
89
|
+
const prefix = SecretsSrv.generatePrefix(req, res);
|
90
|
+
const response = await SecretsSrv.localRead(ans.keys, prefix);
|
91
|
+
Object.assign(ans, response);
|
92
|
+
res.status(200).json(ans.lista).end();
|
93
|
+
}
|
94
|
+
static async save(req, res, next) {
|
95
|
+
const ans = {};
|
96
|
+
ans.map = General.readParam(req, "map");
|
97
|
+
const prefix = SecretsSrv.generatePrefix(req, res);
|
98
|
+
const response = await SecretsSrv.localSave(ans.map, prefix);
|
99
|
+
Object.assign(ans, response);
|
100
|
+
res.status(200).json(Object.keys(ans.map).length).end();
|
101
|
+
}
|
102
|
+
static async getPubKey(req, res, next) {
|
103
|
+
const ans = {};
|
104
|
+
const myKeys = [
|
105
|
+
SecretsSrv.masterPub,
|
106
|
+
SecretsSrv.masterPri
|
107
|
+
];
|
108
|
+
const prefix = SecretsSrv.generatePrefix(req, res);
|
109
|
+
const dbData = await SecretsSrv.localRead(myKeys, prefix);
|
110
|
+
if (dbData[0] == undefined || dbData[1] == undefined) {
|
111
|
+
// Debe crear una llave, guardarla y retornarla
|
112
|
+
const pair = ModuloDatoSeguroBack.generateKeyPair();
|
113
|
+
const myMap = {};
|
114
|
+
myMap[SecretsSrv.masterPub] = pair.public;
|
115
|
+
myMap[SecretsSrv.masterPri] = pair.private;
|
116
|
+
const response = await SecretsSrv.localSave(myMap, prefix);
|
117
|
+
console.log("response=" + JSON.stringify(response));
|
118
|
+
} else {
|
119
|
+
const cipherPub = dbData[myKeys[0]];
|
120
|
+
const cipherPri = dbData[myKeys[1]];
|
121
|
+
console.log("cipherPub=" + JSON.stringify(cipherPub));
|
122
|
+
console.log("cipherPri=" + JSON.stringify(cipherPri));
|
123
|
+
}
|
124
|
+
res.status(200).json(ans).end();
|
125
|
+
}
|
126
|
+
}
|