move some pair stuff to lamassu-admin-server
This commit is contained in:
parent
6422c36644
commit
99cd1b72c6
2 changed files with 11 additions and 27 deletions
14
lib/app.js
14
lib/app.js
|
|
@ -9,7 +9,7 @@ var plugins = require('./plugins')
|
|||
var logger = require('./logger')
|
||||
var configManager = require('./config-manager')
|
||||
|
||||
const db = require('./db')
|
||||
const pair = require('./pair')
|
||||
|
||||
module.exports = function (options) {
|
||||
var app = express()
|
||||
|
|
@ -64,11 +64,15 @@ module.exports = function (options) {
|
|||
|
||||
authMiddleware = function (req, res, next) {
|
||||
const deviceId = req.connection.getPeerCertificate().fingerprint
|
||||
const sql = 'select id from devices where device_id=$1 and authorized=$2'
|
||||
db.one(sql, [deviceId, true])
|
||||
.then(() => {
|
||||
|
||||
return pair.isPaired(deviceId)
|
||||
.then(r => {
|
||||
if (r) {
|
||||
req.deviceId = deviceId
|
||||
next()
|
||||
return next()
|
||||
}
|
||||
|
||||
throw new Error('Unauthorized')
|
||||
})
|
||||
.catch(e => res.status(403).end())
|
||||
}
|
||||
|
|
|
|||
22
lib/pair.js
22
lib/pair.js
|
|
@ -1,26 +1,6 @@
|
|||
const fs = require('fs')
|
||||
const pify = require('pify')
|
||||
const readFile = pify(fs.readFile)
|
||||
const path = require('path')
|
||||
const crypto = require('crypto')
|
||||
const db = require('./db')
|
||||
|
||||
const CA_PATH = path.resolve(__dirname, '..', 'ca-cert.pem')
|
||||
|
||||
function totem (ipAddress) {
|
||||
return readFile(CA_PATH)
|
||||
.then(data => {
|
||||
const caHash = crypto.createHash('sha256').update(data).digest()
|
||||
const token = crypto.randomBytes(32)
|
||||
const ip = Buffer.from(ipAddress.split('.').map(s => parseInt(s, 10)))
|
||||
const buf = Buffer.concat([ip, caHash, token])
|
||||
const sql = 'insert into pairing_tokens (token) values ($1)'
|
||||
|
||||
return db.none(sql, [token.toString('hex')])
|
||||
.then(() => buf.toString('base64'))
|
||||
})
|
||||
}
|
||||
|
||||
function pair (token, deviceId) {
|
||||
const sql = `delete from pairing_tokens
|
||||
where token=$1
|
||||
|
|
@ -42,4 +22,4 @@ function isPaired (deviceId) {
|
|||
.then(() => true)
|
||||
}
|
||||
|
||||
module.exports = {totem, pair, isPaired}
|
||||
module.exports = {totem, pair, unpair, isPaired}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue