open-api-aurum-connector-fingo
Version:
Module to connect to the OPEN API Aurum Core
99 lines (77 loc) • 3.41 kB
JavaScript
const { module_name, configurations_key } = require("../utils/Constants.js");
const { logger } = require("../utils/Logger.js");
const CryptoJS = require("crypto-js");
const cryptoAESauth = (plaintextPassword) => {
logger.info(`module-hub-aurum-core-connector :: cryptoAESauth "${module_name}" ...`);
const config = global[configurations_key];
const CONSUMER_KEY = config?.consumerKey || "";
logger.info(`module-hub-aurum-core-connector :: CONSUMER_KEY: ${CONSUMER_KEY}`);
let preEncypted = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse(CONSUMER_KEY));
let newKey = CryptoJS.enc.Hex.parse(preEncypted.toString(CryptoJS.enc.Hex).substr(0, 32));
var encrypted = CryptoJS.AES.encrypt(plaintextPassword, newKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
logger.info(`module-hub-aurum-core-connector :: Encrypted: ${encrypted}`);
return encrypted;
};
const registerCreate = async (token, body, headers) => {
try {
//login
logger.info(`module-hub-aurum-core-connector :: registerCreate "${module_name}" ...`);
const config = global[configurations_key];
let endpoint = config?.register?.create?.endpoint || "";
//replace endpoint with the correct values
endpoint = endpoint.replace("{mgtwUrl}", config.mgtwUrl);
endpoint = endpoint.replace("{tenantName}", config.tenantName);
logger.info(`module-hub-aurum-core-connector :: Logging in to endpoint: ${endpoint}`);
//body es un json, si no tiene branchId agregar el default
if (!body.branchId) {
body.branchId = config?.register?.create?.branchId || "";
}
//body es un json, si no tiene termsAndConditionsId agregar el default
if (!body.termsAndConditionsId) {
body.termsAndConditionsId = config?.register?.create?.termsAndConditionsId || "";
}
//if password is present, encrypt it
if (body.password) {
body.password = cryptoAESauth(body.password).toString();
}
logger.info(`module-hub-aurum-core-connector :: Body: ${JSON.stringify(body)}`);
let headersAurum = {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
latitude: "",
longitude: "",
};
//add headers latitud and longitud if exists
//print log headers
logger.info(`module-hub-aurum-core-connector :: Headers: ${JSON.stringify(headers)}`);
if (headers && headers.latitud) {
headersAurum.latitude = headers.latitud;
}
if (headers && headers.longitud) {
headersAurum.longitude = headers.longitud;
}
logger.info(`module-hub-aurum-core-connector :: Headers Aurum: ${JSON.stringify(headersAurum)}`);
//INVOKE ENDPOINT, POST, headers: authorization bearer(token), body: raw json (body)
const response = await fetch(endpoint, {
method: "POST",
headers: headersAurum,
body: JSON.stringify(body),
});
//PARSE RESPONSE
const responseJson = await response.json();
logger.info(`module-hub-aurum-core-connector :: Response: ${JSON.stringify(responseJson)}`);
//RETURN RESPONSE
return responseJson;
} catch (error) {
logger.error(`Error registering "${module_name}"`, error);
return {
responseCode: 500,
error: error,
error_description: "Error registering",
};
}
};
module.exports = registerCreate;