open-api-aurum-connector2
Version:
Module to connect to the OPEN API Aurum Core
69 lines (57 loc) • 2.46 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;
};
//this method requiere user autentication, not clients
const updatePassword = async (token, body) => {
try {
logger.info(`module-hub-aurum-core-connector :: updatePassword "${module_name}" ...`);
const config = global[configurations_key];
let endpoint = config?.profile?.updatePassword?.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 :: updatePassword to endpoint: ${endpoint}`);
//if old password is present, encrypt it
if (body.oldPassword) {
body.oldPassword = cryptoAESauth(body.oldPassword).toString();
}
//if new password is present, encrypt it
if (body.newPassword) {
body.newPassword = cryptoAESauth(body.newPassword).toString();
}
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 responseJson;
} catch (error) {
logger.error(`module-hub-aurum-core-connector :: Error on updatePassword: ${error}`);
return {
code: '500',
error_message: "Error on updatePassword",
error_description: error
};
}
};
module.exports = updatePassword;