endo-core
Version:
Put some description here
47 lines (34 loc) • 1.86 kB
text/coffeescript
url = require 'url'
class CredentialsDeviceController
constructor: ({, , , }) ->
throw new Error 'appOctobluHost is required' unless ?
throw new Error 'credentialsDeviceService is required' unless ?
throw new Error 'serviceUrl is required' unless ?
throw new Error 'userDeviceManagerUrl is required' unless ?
getCredentialsDevice: (req, res, next) =>
{credentialsDeviceUuid} = req.params
authorizedUuid = req.meshbluAuth.uuid
.authorizedFind {authorizedUuid, credentialsDeviceUuid}, (error, credentialsDevice) =>
return res.sendError error if error?
req.credentialsDevice = credentialsDevice
next()
get: (req, res) =>
req.credentialsDevice.getPublicDevice (error, publicDevice) =>
return res.sendError error if error?
res.send publicDevice
upsert: (req, res) =>
encrypted = req.user
{id} = req.user
authorizedUuid = req.meshbluAuth.uuid
.findOrCreate id, (error, credentialsDevice) =>
return res.sendError error if error?
credentialsDevice.update {authorizedUuid, id, encrypted}, (error) =>
return res.sendError error if error?
serviceUrl = url.parse
serviceUrl.pathname = credentialsDevice.getUuid()
userDeviceManagerUrl = url.parse , true
userDeviceManagerUrl.query.meshbluAuthBearer = req.meshbluAuth.bearerToken
userDeviceManagerUrl.query.credentialsDeviceUrl = url.format serviceUrl
userDeviceManagerUrl.query.appOctobluHost = url.format
return res.redirect 201, url.format(userDeviceManagerUrl)
module.exports = CredentialsDeviceController