From aead4c753102aa27ba0770c2984f99c1d5e52aa9 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 27 May 2015 11:14:37 -0400 Subject: [PATCH] Make use of resin-register-device --- package.json | 1 + src/bootstrap.coffee | 24 ++---------------------- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index f0fd32f4..71a4e40b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "pubnub": "~3.6.4", "randomstring": "~1.0.3", "request": "^2.51.0", + "resin-register-device": "^1.0.1", "server-destroy": "^1.0.0", "sqlite3": "~3.0.4", "tty.js": "0.2.14-1", diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index d53654e2..d80e7a28 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -2,30 +2,10 @@ Promise = require 'bluebird' _ = require 'lodash' knex = require './db' utils = require './utils' -crypto = require 'crypto' +deviceRegister = require 'resin-register-device' { resinApi } = require './request' fs = Promise.promisifyAll(require('fs')) -registerDevice = (apiKey, userId, applicationId, deviceType, uuid) -> - # I'd be nice if the UUID matched the output of a SHA-256 function, but although the length limit of the CN - # attribute in a X.509 certificate is 64 chars, a 32 byte UUID (64 chars in hex) doesn't pass the certificate - # validation in OpenVPN This either means that the RFC counts a final NULL byte as part of the CN or that the - # OpenVPN/OpenSSL implementation has a bug. - if not uuid? - uuid = crypto.pseudoRandomBytes(31).toString('hex') - - resinApi.post( - resource: 'device' - body: - user: userId - application: applicationId - uuid: uuid - device_type: deviceType - customOptions: - apikey: apiKey - ).then (data) -> - _.pick(data, 'id', 'uuid') - module.exports = -> # Load config file fs.readFileAsync('/boot/config.json', 'utf8') @@ -34,7 +14,7 @@ module.exports = -> userConfig.deviceType ?= 'raspberry-pi' if userConfig.uuid? and userConfig.registered_at? return userConfig - registerDevice(userConfig.apiKey, userConfig.userId, userConfig.applicationId, userConfig.deviceType, userConfig.uuid) + deviceRegister.register(resinApi, userConfig) .then (device) -> userConfig.uuid = device.uuid userConfig.registered_at = Date.now()