Merge pull request #757 from josepfo/feat/create-table-for-operator-ids
feat: create db table and store operator id
This commit is contained in:
commit
24ed69244c
5 changed files with 89 additions and 53 deletions
|
|
@ -1,17 +1,16 @@
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
const _ = require('lodash/fp')
|
const _ = require('lodash/fp')
|
||||||
const hkdf = require('futoin-hkdf')
|
|
||||||
|
|
||||||
const pify = require('pify')
|
const pify = require('pify')
|
||||||
const fs = pify(require('fs'))
|
const fs = pify(require('fs'))
|
||||||
|
|
||||||
const db = require('../db')
|
const db = require('../db')
|
||||||
const mnemonicHelpers = require('../mnemonic-helpers')
|
|
||||||
const configManager = require('../new-config-manager')
|
const configManager = require('../new-config-manager')
|
||||||
const complianceTriggers = require('../compliance-triggers')
|
const complianceTriggers = require('../compliance-triggers')
|
||||||
const options = require('../options')
|
const options = require('../options')
|
||||||
const logger = require('../logger')
|
const logger = require('../logger')
|
||||||
const plugins = require('../plugins')
|
const plugins = require('../plugins')
|
||||||
|
const { getOperatorId } = require('../operator')
|
||||||
|
|
||||||
const TIMEOUT = 10000
|
const TIMEOUT = 10000
|
||||||
const MAX_CONTENT_LENGTH = 2000
|
const MAX_CONTENT_LENGTH = 2000
|
||||||
|
|
@ -133,10 +132,10 @@ function sendRadar (data) {
|
||||||
|
|
||||||
function mapRecord (rates, settings) {
|
function mapRecord (rates, settings) {
|
||||||
const timestamp = new Date().toISOString()
|
const timestamp = new Date().toISOString()
|
||||||
return Promise.all([getMachines(rates, settings), fs.readFile(options.mnemonicPath, 'utf8')])
|
return Promise.all([getMachines(rates, settings), getOperatorId('coinatmradar')])
|
||||||
.then(([machines, mnemonic]) => {
|
.then(([machines, operatorId]) => {
|
||||||
return {
|
return {
|
||||||
operatorId: computeOperatorId(mnemonicHelpers.toEntropyBuffer(mnemonic)),
|
operatorId: operatorId,
|
||||||
operator: {
|
operator: {
|
||||||
name: null,
|
name: null,
|
||||||
phone: null,
|
phone: null,
|
||||||
|
|
@ -157,7 +156,3 @@ function update (rates, settings) {
|
||||||
.then(sendRadar)
|
.then(sendRadar)
|
||||||
.catch(err => logger.error(`Failure to update CoinATMRadar`, err))
|
.catch(err => logger.error(`Failure to update CoinATMRadar`, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeOperatorId (masterSeed) {
|
|
||||||
return hkdf(masterSeed, 16, { salt: 'lamassu-server-salt', info: 'operator-id' }).toString('hex')
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,15 @@
|
||||||
const pify = require('pify')
|
const { getOperatorId } = require('../operator')
|
||||||
const fs = pify(require('fs'))
|
|
||||||
const hkdf = require('futoin-hkdf')
|
|
||||||
|
|
||||||
const state = require('./state')
|
|
||||||
const mnemonicHelpers = require('../mnemonic-helpers')
|
|
||||||
const options = require('../options')
|
|
||||||
const logger = require('../logger')
|
|
||||||
|
|
||||||
function computeOperatorId (masterSeed) {
|
|
||||||
return hkdf(masterSeed, 16, { salt: 'lamassu-server-salt', info: 'operator-id' }).toString('hex')
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMnemonic () {
|
|
||||||
if (state.mnemonic) return Promise.resolve(state.mnemonic)
|
|
||||||
return fs.readFile(options.mnemonicPath, 'utf8').then(mnemonic => {
|
|
||||||
state.mnemonic = mnemonic
|
|
||||||
return mnemonic
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function findOperatorId (req, res, next) {
|
function findOperatorId (req, res, next) {
|
||||||
return getMnemonic().then(mnemonic => {
|
return getOperatorId('middleware')
|
||||||
return computeOperatorId(mnemonicHelpers.toEntropyBuffer(mnemonic))
|
.then(({ operatorId }) => {
|
||||||
}).then(id => {
|
res.locals.operatorId = operatorId
|
||||||
res.locals.operatorId = id
|
return next()
|
||||||
return next()
|
})
|
||||||
}).catch(e => {
|
.catch(e => {
|
||||||
logger.error('Error while computing operator id\n' + e)
|
console.error('Error while computing operator id\n' + e)
|
||||||
next(e)
|
next(e)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = findOperatorId
|
module.exports = findOperatorId
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,21 @@
|
||||||
const fs = require('fs')
|
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
const hkdf = require('futoin-hkdf')
|
|
||||||
const session = require('express-session')
|
const session = require('express-session')
|
||||||
const PgSession = require('connect-pg-simple')(session)
|
const PgSession = require('connect-pg-simple')(session)
|
||||||
const mnemonicHelpers = require('../../mnemonic-helpers')
|
|
||||||
const db = require('../../db')
|
const db = require('../../db')
|
||||||
const options = require('../../options')
|
const options = require('../../options')
|
||||||
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
|
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
|
||||||
|
const { getOperatorId } = require('../../operator')
|
||||||
const getSecret = () => {
|
|
||||||
const mnemonic = fs.readFileSync(options.mnemonicPath, 'utf8')
|
|
||||||
return hkdf(
|
|
||||||
mnemonicHelpers.toEntropyBuffer(mnemonic),
|
|
||||||
16,
|
|
||||||
{ info: 'operator-id' }
|
|
||||||
).toString('hex')
|
|
||||||
}
|
|
||||||
|
|
||||||
const hostname = options.hostname
|
const hostname = options.hostname
|
||||||
|
|
||||||
router.use('*', session({
|
router.use('*', async (req, res, next) => getOperatorId('authentication').then(({ operatorId }) => session({
|
||||||
store: new PgSession({
|
store: new PgSession({
|
||||||
pgPromise: db,
|
pgPromise: db,
|
||||||
tableName: USER_SESSIONS_TABLE_NAME
|
tableName: USER_SESSIONS_TABLE_NAME
|
||||||
}),
|
}),
|
||||||
name: 'lamassu_sid',
|
name: 'lamassu_sid',
|
||||||
secret: getSecret(),
|
secret: operatorId,
|
||||||
resave: false,
|
resave: false,
|
||||||
saveUninitialized: false,
|
saveUninitialized: false,
|
||||||
cookie: {
|
cookie: {
|
||||||
|
|
@ -36,6 +25,7 @@ router.use('*', session({
|
||||||
sameSite: true,
|
sameSite: true,
|
||||||
maxAge: 60 * 10 * 1000 // 10 minutes
|
maxAge: 60 * 10 * 1000 // 10 minutes
|
||||||
}
|
}
|
||||||
}))
|
})(req, res, next))
|
||||||
|
)
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|
|
||||||
10
lib/operator.js
Normal file
10
lib/operator.js
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
const db = require('./db')
|
||||||
|
const _ = require('lodash/fp')
|
||||||
|
|
||||||
|
function getOperatorId (service) {
|
||||||
|
const sql = `SELECT operator_id FROM operator_ids WHERE service = '${service}'`
|
||||||
|
return db.oneOrNone(sql)
|
||||||
|
.then(_.mapKeys(_.camelCase))
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { getOperatorId }
|
||||||
60
migrations/1623413776161-create-operator-ids.js
Normal file
60
migrations/1623413776161-create-operator-ids.js
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
var db = require('./db')
|
||||||
|
const pify = require('pify')
|
||||||
|
const fs = pify(require('fs'))
|
||||||
|
const hkdf = require('futoin-hkdf')
|
||||||
|
|
||||||
|
const state = require('../lib/middlewares/state')
|
||||||
|
const mnemonicHelpers = require('../lib/mnemonic-helpers')
|
||||||
|
const options = require('../lib/options')
|
||||||
|
|
||||||
|
function computeOperatorId (masterSeed) {
|
||||||
|
return hkdf(masterSeed, 16, { salt: 'lamassu-server-salt', info: 'operator-id' }).toString('hex')
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMnemonic () {
|
||||||
|
if (state.mnemonic) return Promise.resolve(state.mnemonic)
|
||||||
|
return fs.readFile(options.mnemonicPath, 'utf8').then(mnemonic => {
|
||||||
|
state.mnemonic = mnemonic
|
||||||
|
return mnemonic
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function generateOperatorId () {
|
||||||
|
return getMnemonic().then(mnemonic => {
|
||||||
|
return computeOperatorId(mnemonicHelpers.toEntropyBuffer(mnemonic))
|
||||||
|
}).then(id => {
|
||||||
|
return id
|
||||||
|
}).catch(e => {
|
||||||
|
console.error('Error while computing operator id\n' + e)
|
||||||
|
throw e
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.up = function (next) {
|
||||||
|
const sql =
|
||||||
|
[
|
||||||
|
`CREATE TABLE operator_ids (
|
||||||
|
id serial PRIMARY KEY,
|
||||||
|
operator_id TEXT NOT NULL,
|
||||||
|
service TEXT NOT NULL
|
||||||
|
)`
|
||||||
|
]
|
||||||
|
generateOperatorId()
|
||||||
|
.then(operatorId => {
|
||||||
|
const sql2 = [
|
||||||
|
`INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','middleware')`,
|
||||||
|
`INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','coinatmradar')`,
|
||||||
|
`INSERT INTO operator_ids (operator_id, service) VALUES ('${operatorId}','authentication')`
|
||||||
|
]
|
||||||
|
db.multi(sql.concat(sql2), next)
|
||||||
|
.then(() => next())
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
db.multi(sql, next)
|
||||||
|
.then(() => next())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.down = function (next) {
|
||||||
|
next()
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue