feat: save T&C hash to the user_config

This commit is contained in:
André Sá 2022-04-14 12:01:20 +01:00
parent 6c43f7536d
commit 7b951f961f
2 changed files with 28 additions and 2 deletions

View file

@ -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

View file

@ -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 })]))