api-beep-onboarding
Version:
Oracle OCI FaaS for Api Beep Onboarding library
129 lines (109 loc) • 3.46 kB
JavaScript
/**
*
* BeePay 2.023-2.024 - Oracle OCI FaaS for Api Beep Onboarding
* Utilities
*
* Changes:
* jlugo: 2023-dic-01. File creation
* jlugo: 2024-apr-25. Moved to a library
*/
/* RGVzYXJyb2xsYWRvIHBvciBKb25hdGhhbiBMdWdv */
import { Buffer } from "node:buffer";
import process from "node:process";
/**
*
* ENVironment variables
*/
export const env = {
baseUrl: process.env.BASE_URL || "https://way4wsapi.cert.apolotest.owb.dev/wsruntime/ws",
certificate: process.env.CERT_FILENAME || "Way4.WSApi.cert.apolotest.owb.dev.p12",
ocid_secret: process.env.OCID_SECRET || "",
bank_code: process.env.BANK_CODE || "0051",
urlPattern: process.env.URL_PATTERN || "",
environment: process.env.ENVIRONMENT || "Development",
debug: process.env.DEBUG || 0
}
/**
* Is production runtime environment
*
* @returns {Boolean} - true: Runtime environment is production
*/
function isProduction() {
return (env.environment.toLowerCase() === "production");
}
/**
* Check all functions configuration parameters
*
* @returns {Boolean} - true: All configuration parameters are set
*/
export function checkParameters() {
return !(env.baseUrl.length === 0 ||
env.certificate.length === 0 ||
env.ocid_secret.length === 0 ||
env.bank_code.length === 0);
}
/**
* Get sub (user) from IDCS token
*
* @param {String} token
* @returns {Boolean|null}
*/
export function getUserFromToken(token) {
if (token === undefined || token == null || token.length === 0) {
return null;
}
// Assumed we have a JWT token
const payload = JSON.parse(Buffer.from(token.split(".")[1], "base64").toString());
return payload.sub;
}
/**
* Read user claims from bearer authorization token and validate access to runtime environment
*
* @param {String} user - Authenticated user
* @returns {boolean} - true: Authorization is valid
*/
export function checkUserEnvAuth(user) {
if (user === undefined || user == null || user.length === 0) {
return false;
}
return user.includes("prod") ? isProduction() : !isProduction()
}
/**
*
* @param {Object} params
* @param {Object} param
* @returns {Boolean}
*/
export function checkUriParameters(params, param) {
return Object.entries(params).
every(elem => {
param.field = elem[0];
return !(!elem[1] || elem[1].length === 0);
});
}
/**
* Add debug console
*
* @param {Object} console - node.js original console object
* @param {Object} options - Console options. enable: true|false
* @returns {Object}
*/
export const debugConsole = (console, options = {}) => {
const nextConsoleObj = { ...console };
const { enabled = true } = options;
for (let key in nextConsoleObj) {
if (nextConsoleObj.hasOwnProperty(key) && typeof nextConsoleObj[key] === "function") {
const func = nextConsoleObj[key];
nextConsoleObj[key] = function () {
if (enabled) {
const args = Array.from(arguments);
if (key === "log") {
args.unshift(`${options.prefix}`);
}
func.apply(nextConsoleObj, args);
}
};
}
}
return nextConsoleObj;
}