From 7b951f961f22c7cdce4b63180d2f2722ff873d46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1?= Date: Thu, 14 Apr 2022 12:01:20 +0100 Subject: [PATCH] feat: save T&C hash to the `user_config` --- lib/middlewares/populateDeviceId.js | 1 - lib/new-settings-loader.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/middlewares/populateDeviceId.js b/lib/middlewares/populateDeviceId.js index 3bf50761..4c8913ae 100644 --- a/lib/middlewares/populateDeviceId.js +++ b/lib/middlewares/populateDeviceId.js @@ -11,7 +11,6 @@ function sha256 (buf) { } const populateDeviceId = function (req, res, next) { - logger.info(`DEBUG LOG - Method: ${req.method} Path: ${req.path}`) const deviceId = _.isFunction(req.connection.getPeerCertificate) ? sha256(req.connection.getPeerCertificate().raw) : null diff --git a/lib/new-settings-loader.js b/lib/new-settings-loader.js index 2f812c51..9dbba793 100644 --- a/lib/new-settings-loader.js +++ b/lib/new-settings-loader.js @@ -1,8 +1,11 @@ +const crypto = require('crypto') + const _ = require('lodash/fp') const db = require('./db') const migration = require('./config-migration') const { asyncLocalStorage } = require('./async-storage') const { getOperatorId } = require('./operator') +const { getTermsConditions, setTermsConditions } = require('./new-config-manager') const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1 const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2 @@ -23,6 +26,30 @@ const SECRET_FIELDS = [ 'twilio.authToken' ] +/* + * JSON.stringify isn't necessarily deterministic so this function may compute + * different hashes for the same object. + */ +const md5hash = obj => + crypto + .createHash('MD5') + .update(JSON.stringify(obj)) + .digest('hex') + +const addTermsHash = configs => { + configs = _.omit('termsConditions_hash', configs) + const terms = getTermsConditions(configs) + return _.isEmpty(terms) ? + configs : + _.flow( + _.omit(['hash']), + md5hash, + hash => _.set('hash', hash, terms), + setTermsConditions, + _.assign(configs), + )(terms) +} + const accountsSql = `update user_config set data = $2, valid = $3, schema_version = $4 where type = $1; insert into user_config (type, data, valid, schema_version) select $1, $2, $3, $4 where $1 not in (select type from user_config)` @@ -74,7 +101,7 @@ const configSql = 'insert into user_config (type, data, valid, schema_version) v function saveConfig (config) { return Promise.all([loadLatestConfigOrNone(), getOperatorId('middleware')]) .then(([currentConfig, operatorId]) => { - const newConfig = _.assign(currentConfig, config) + const newConfig = addTermsHash(_.assign(currentConfig, config)) return db.tx(t => { return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION]) .then(() => t.none('NOTIFY $1:name, $2', ['reload', JSON.stringify({ schema: asyncLocalStorage.getStore().get('schema'), operatorId })]))