X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fcommon.js;h=1c19aeb9dbde3172858ee3dcea9a00ceb1a8004e;hb=9c604adfcde56e35767e3eba70890308ec2d3110;hp=c0aaf3799c7f3d2609ae2ce7ee8c6cec3482facb;hpb=dd173e6b450cbba8100883514610c9fde83d050a;p=squeep-authentication-module diff --git a/lib/common.js b/lib/common.js index c0aaf37..1c19aeb 100644 --- a/lib/common.js +++ b/lib/common.js @@ -11,15 +11,58 @@ const { common } = require('@squeep/api-dingus'); const freezeDeep = (o) => { Object.freeze(o); Object.getOwnPropertyNames(o).forEach((prop) => { - if (Object.hasOwnProperty.call(o, prop) + if (Object.hasOwn(o, prop) && ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection && !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection } }); return o; -} +}; + +/** + * Return a new object duplicating `o`, without the properties specified. + * @param {Object} o + * @param {String[]} props + * @returns {Object} + */ +const omit = (o, props) => { + return Object.fromEntries(Object.entries(o).filter(([k]) => !props.includes(k))); +}; + +/** + * Helper to log mystery-box statistics. + * @param {ConsoleLike} logger + * @param {String} scope + * @returns {Function} + */ +const mysteryBoxLogger = (logger, scope) => { + return (s) => { + logger.debug(scope, `${s.packageName}@${s.packageVersion}:${s.method}`, omit(s, [ + 'packageName', + 'packageVersion', + 'method', + ])); + }; +}; + +/** + * Hide sensitive part of an Authorization header. + * @param {String} authHeader + * @returns {String} + */ +const obscureAuthorizationHeader = (authHeader) => { + if (!authHeader) { + return authHeader; + } + const space = authHeader.indexOf(' '); + // This blurs entire string if no space found, because -1. + return authHeader.slice(0, space + 1) + '*'.repeat(authHeader.length - (space + 1)); +}; module.exports = Object.assign(Object.create(common), { freezeDeep, + mysteryBoxLogger, + obscureAuthorizationHeader, + omit, }); \ No newline at end of file