add ca download route

This commit is contained in:
Josh Harvey 2016-10-21 23:56:58 +03:00
parent 20e10779a7
commit ff162775a1
3 changed files with 44 additions and 5 deletions

View file

@ -1,12 +1,17 @@
const db = require('./db')
function pair (token, deviceId) {
function pullToken (token) {
const sql = `delete from pairing_tokens
where token=$1
returning created < now() - interval '1 hour' as expired`
return db.one(sql, [token])
.then(r => {
if (r.expired) return false
.then(r => r.expired)
}
function pair (token, deviceId) {
pullToken(token)
.then(valid => {
if (!valid) return false
const pairSql = 'insert into paired_devices (device_id) values ($1)'
return db.none(pairSql, [deviceId])
@ -14,6 +19,10 @@ function pair (token, deviceId) {
})
}
function authorizeCaDownload (caToken) {
return pullToken(caToken)
}
function isPaired (deviceId) {
const sql = 'select device_id from paired_devices where device_id=$1'
@ -21,4 +30,4 @@ function isPaired (deviceId) {
.then(() => true)
}
module.exports = {pair, isPaired}
module.exports = {pair, authorizeCaDownload, isPaired}