This commit is contained in:
Josh Harvey 2016-11-20 17:49:36 +02:00
parent becd62a1fb
commit 938eb9ac97
9 changed files with 153 additions and 68 deletions

View file

@ -1,10 +1,9 @@
const path = require('path')
const fs = require('fs')
const pify = require('pify')
const readFile = pify(fs.readFile)
const db = require('./db')
const CA_PATH = path.resolve(__dirname, '..', 'certs', 'root-ca.crt.pem')
const options = require('./options')
const logger = require('./logger')
function pullToken (token) {
const sql = `delete from pairing_tokens
@ -14,26 +13,31 @@ function pullToken (token) {
}
function pair (token, deviceId) {
pullToken(token)
return pullToken(token)
.then(r => {
if (r.expired) return false
const insertSql = `insert into devices (device_id, name) values ($1, $2)
on conflict (device_id)
do update set name=$1, paired=TRUE, display=TRUE`
do update set name=$2, paired=TRUE, display=TRUE`
return db.none(insertSql, [deviceId, r.name])
.then(() => true)
})
.catch(() => false)
.catch(err => {
logger.debug(err)
return false
})
}
function authorizeCaDownload (caToken) {
return pullToken(caToken)
}
.then(r => {
if (r.expired) throw new Error('Expired')
function ca () {
return readFile(CA_PATH)
const caPath = options.caPath
return readFile(caPath, {encoding: 'utf8'})
})
}
function isPaired (deviceId) {
@ -43,4 +47,4 @@ function isPaired (deviceId) {
.then(() => true)
}
module.exports = {pair, authorizeCaDownload, ca, isPaired}
module.exports = {pair, authorizeCaDownload, isPaired}