UNPKG

open-api-aurum-connector2

Version:

Module to connect to the OPEN API Aurum Core

79 lines (64 loc) 2.77 kB
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) => { 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)}`); //INVOKE ENDPOINT, POST, headers: authorization bearer(token), body: raw json (body) const response = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json", Authorization: "Bearer " + token, }, 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;