This commit is contained in:
Josh Harvey 2016-11-25 19:46:43 +02:00
parent 0a8021691a
commit 3d9814398d
4 changed files with 683 additions and 253 deletions

View file

@ -12,8 +12,7 @@ const pairing = require('./pairing')
let plugins
module.exports = {
init,
getDeviceId
init
}
const STALE_TICKER = 3 * 60 * 1000
@ -62,8 +61,8 @@ function buildBalances (deviceId) {
}
function poll (req, res) {
const deviceId = getDeviceId(req)
const deviceTime = getDeviceTime(req)
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const pid = req.query.pid
pids[deviceId] = {pid, ts: Date.now()}
@ -123,24 +122,25 @@ function trade (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
plugins.trade(getDeviceId(req), tx)
plugins.trade(req.deviceId, tx)
.then(() => cacheAndRespond(req, res))
}
function stateChange (req, res) {
plugins.stateChange(getDeviceId(req), getDeviceTime(req), req.body)
plugins.stateChange(req.deviceId, req.deviceTime, req.body)
.then(() => cacheAndRespond(req, res))
}
function send (req, res) {
function send (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.sendCoins(getDeviceId(req), tx)
return plugins.sendCoins(req.deviceId, tx)
.then(status => {
const body = {txId: status && status.txId}
return cacheAndRespond(req, res, body)
})
.catch(next)
}
function cashOut (req, res) {
@ -148,17 +148,17 @@ function cashOut (req, res) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.cashOut(getDeviceId(req), tx)
return plugins.cashOut(req.deviceId, tx)
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
}
function dispenseAck (req, res) {
plugins.dispenseAck(getDeviceId(req), req.body.tx)
plugins.dispenseAck(req.deviceId, req.body.tx)
.then(() => cacheAndRespond(req, res))
}
function deviceEvent (req, res) {
plugins.logEvent(getDeviceId(req), req.body)
plugins.logEvent(req.deviceId, req.body)
.then(() => cacheAndRespond(req, res))
}
@ -182,7 +182,7 @@ function ca (req, res) {
function pair (req, res, next) {
const token = req.query.token
const deviceId = getDeviceId(req)
const deviceId = req.deviceId
return pairing.pair(token, deviceId)
.then(valid => {
@ -255,7 +255,7 @@ function cacheAction (req, res, next) {
const sql = `insert into idempotents (request_id, device_id, body, status, pending)
values ($1, $2, $3, $4, $5)`
const deviceId = getDeviceId(req)
const deviceId = req.deviceId
db.none(sql, [requestId, deviceId, {}, 204, true])
.then(() => next())
@ -278,7 +278,7 @@ function updateCachedAction (req, body, status) {
const sql = `update idempotents set body=$1, status=$2, pending=$3
where request_id=$4 and device_id=$5 and pending=$6`
const deviceId = getDeviceId(req)
const deviceId = req.deviceId
return db.none(sql, [body, status, false, requestId, deviceId, true])
}
@ -316,7 +316,7 @@ function httpError (msg, code) {
}
function filterOldRequests (req, res, next) {
const deviceTime = getDeviceTime(req)
const deviceTime = req.deviceTime
const delta = Date.now() - deviceTime
if (delta > CLOCK_SKEW) {
@ -328,7 +328,7 @@ function filterOldRequests (req, res, next) {
}
function authorize (req, res, next) {
const deviceId = req.connection.getPeerCertificate().fingerprint
const deviceId = req.deviceId
return pairing.isPaired(deviceId)
.then(r => {
@ -354,6 +354,7 @@ function init (opts) {
app.use(morgan('dev'))
app.use(helmet())
app.use(populateDeviceId)
app.use(bodyParser.json())
app.use(filterOldRequests)
app.post('*', cacheAction)
@ -414,12 +415,12 @@ function init (opts) {
return app
}
function getDeviceTime (req) {
return Date.parse(req.get('date'))
}
function populateDeviceId (req, res, next) {
const deviceId = ((typeof req.connection.getPeerCertificate === 'function' &&
req.connection.getPeerCertificate().fingerprint)) || null
function getDeviceId (req) {
return (typeof req.connection.getPeerCertificate === 'function' &&
req.connection.getPeerCertificate().fingerprint) || 'unknown'
}
req.deviceId = deviceId
req.deviceTime = Date.parse(req.get('date'))
next()
}