This commit is contained in:
Josh Harvey 2016-11-08 00:28:57 +00:00
parent 855546f886
commit 785749133d
5 changed files with 50 additions and 19 deletions

View file

@ -9,20 +9,23 @@ const CA_PATH = path.resolve(__dirname, '..', 'certs', 'root-ca.crt.pem')
function pullToken (token) {
const sql = `delete from pairing_tokens
where token=$1
returning created < now() - interval '1 hour' as expired`
returning name, created < now() - interval '1 hour' as expired`
return db.one(sql, [token])
.then(r => r.expired)
}
function pair (token, deviceId) {
pullToken(token)
.then(valid => {
if (!valid) return false
.then(r => {
if (r.expired) return false
const pairSql = 'insert into paired_devices (device_id) values ($1)'
return db.none(pairSql, [deviceId])
const insertSql = `insert into devices (device_id, name) values ($1, $2)
on conflict (device_id)
do update set name=$1, paired=TRUE, display=TRUE`
return db.none(insertSql, [deviceId, r.name])
.then(() => true)
})
.catch(() => false)
}
function authorizeCaDownload (caToken) {
@ -34,7 +37,7 @@ function ca () {
}
function isPaired (deviceId) {
const sql = 'select device_id from paired_devices where device_id=$1'
const sql = 'select device_id from devices where device_id=$1 and paired=TRUE'
return db.one(sql, [deviceId])
.then(() => true)