endo-core
Version:
Put some description here
131 lines (106 loc) • 5.23 kB
text/coffeescript
_ = require 'lodash'
MeshbluHTTP = require 'meshblu-http'
Encryption = require 'meshblu-encryption'
url = require 'url'
credentialsDeviceUpdateGenerator = require '../config-generators/credentials-device-update-config-generator'
userDeviceConfigGenerator = require '../config-generators/user-device-config-generator'
statusDeviceConfigGenerator = require '../config-generators/status-device-create-config-generator'
class CredentialsDevice
constructor: ({, , , , , }) ->
throw new Error('deviceType is required') unless ?
throw new Error('serviceUuid is required') unless ?
{, , } =
= Encryption.fromJustGuess
= new MeshbluHTTP
createUserDevice: ({authorizedUuid}, callback) =>
resourceOwnerName = .decrypt().username
userDeviceConfig = userDeviceConfigGenerator
authorizedUuid: authorizedUuid
credentialsUuid:
deviceType:
imageUrl:
resourceOwnerName: resourceOwnerName
formSchemaUrl: ()
messageSchemaUrl: ()
responseSchemaUrl: ()
.register userDeviceConfig, (error, userDevice) =>
return callback error if error?
subscription = {subscriberUuid: , emitterUuid: userDevice.uuid, type: 'message.received'}
.createSubscription subscription, (error) =>
return callback error if error?
{userDeviceUuid: userDevice.uuid, authorizedUuid}, (error, {uuid}={}) =>
callback error if error?
{userDeviceUuid: userDevice.uuid, userDeviceToken: userDevice.token, statusDeviceUuid: uuid}, (error) =>
callback error, userDevice
createStatusDevice: ({userDeviceUuid, authorizedUuid}, callback) =>
statusDeviceConfig = statusDeviceConfigGenerator {userDeviceUuid, authorizedUuid}
.register statusDeviceConfig, callback
updateUserStatusDevice: ({userDeviceUuid, userDeviceToken, statusDeviceUuid}, callback) =>
userDeviceMeshblu = new MeshbluHTTP _.defaults {uuid: userDeviceUuid, token: userDeviceToken},
userDeviceMeshblu.updateDangerously userDeviceUuid, $set: statusDevice: statusDeviceUuid, callback
deleteUserDeviceSubscription: ({userDeviceUuid}, callback) =>
return callback 'Cannot remove the credentials subscription to itself', 403 if userDeviceUuid ==
subscription =
emitterUuid: userDeviceUuid
subscriberUuid:
type: 'message.received'
.deleteSubscription subscription, (error, ignored) =>
callback error
getPublicDevice: (callback) =>
.device , (error, credentialsDevice) =>
return callback error if error?
decrypted = .decrypt
decrypted = _.omit decrypted, 'secrets'
return callback null, _.defaults({username: decrypted.username}, credentialsDevice.options)
getUserDevices: (callback) =>
.subscriptions , (error, subscriptions) =>
return callback error if error?
return callback null, subscriptions
getUserDevice: (uuid, callback) =>
.search { uuid }, {}, (error, devices) =>
return callback error if error
return callback 'User Device not found!', 404 if _.isEmpty devices
device = _.first devices
return callback null, device
getUuid: =>
update: ({authorizedUuid, encrypted, id}, callback) =>
{endo, endoSignature} = {authorizedUuid, encrypted, id}
endo.encrypted = .encrypt endo.encrypted
update = credentialsDeviceUpdateGenerator {endo, endoSignature, }
.updateDangerously , update, callback
_getFormSchemaUrl: =>
uri = url.parse
uri.pathname = "#{uri.pathname}v1/form-schema"
return url.format uri
_getMessageSchemaUrl: =>
uri = url.parse
uri.pathname = "#{uri.pathname}v1/message-schema"
return url.format uri
_getResponseSchemaUrl: =>
uri = url.parse
uri.pathname = "#{uri.pathname}v1/response-schema"
return url.format uri
_getSignedUpdate: ({authorizedUuid, encrypted, id}) =>
encrypted = _.cloneDeep encrypted
encrypted.secrets ?= {}
encrypted.secrets.credentialsDeviceToken =
endo = {
authorizedKey: .sign authorizedUuid
idKey: .sign id
credentialsDeviceUuid:
version: '1.0.0'
encrypted: encrypted
}
endoSignature = .sign(endo)
return {endo, endoSignature}
_userDevicesFromSubscriptions: (subscriptions) =>
_(subscriptions)
.filter type: 'message.received'
.reject emitterUuid:
.map ({emitterUuid}) => {uuid: emitterUuid}
.value()
_userError: (message, code) =>
error = new Error message
error.code = code if code?
return error
module.exports = CredentialsDevice