endo-core
Version:
Put some description here
117 lines (94 loc) • 4.32 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'
class CredentialsDevice
constructor: ({, , , meshbluConfig, , }) ->
throw new Error('deviceType is required') unless ?
throw new Error('serviceUuid is required') unless ?
{, } = meshbluConfig
= Encryption.fromJustGuess
= new MeshbluHTTP meshbluConfig
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?
return callback null, userDevice
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
getUuid: =>
update: ({authorizedUuid, encrypted, id}, callback) =>
{endo, endoSignature} = {authorizedUuid, encrypted, id}
endo.encrypted = .encrypt endo.encrypted
update = credentialsDeviceUpdateGenerator {endo, endoSignature, }
.updateDangerously , update, (error) =>
return callback error if error?
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}) =>
endo = {
authorizedKey: .sign(authorizedUuid).toString 'base64'
idKey: .sign(id).toString 'base64'
credentialsDeviceUuid:
version: '1.0.0'
encrypted: encrypted
}
endoSignature = .sign endo
return {endo, endoSignature}
_subscribeToOwnMessagesReceived: (callback) =>
subscription = {subscriberUuid: , emitterUuid: , type: 'message.received'}
.createSubscription subscription, (error, ignored) =>
return callback error if error?
return callback()
_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