Skip to content

Commit

Permalink
feature: provide a way for devices to obtain a security token and new…
Browse files Browse the repository at this point in the history
… user to sign up
  • Loading branch information
sbender9 committed Sep 30, 2018
1 parent f90be89 commit 43c7021
Show file tree
Hide file tree
Showing 23 changed files with 2,033 additions and 488 deletions.
9 changes: 4 additions & 5 deletions lib/deltacache.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,10 @@ DeltaCache.prototype.getCachedDeltas = function (user, contextFilter, key) {

deltas = deltas.map(toDelta)

if (this.app.securityStrategy.shouldFilterDeltas()) {
deltas = deltas.filter(delta => {
return this.app.securityStrategy.filterReadDelta(user, delta)
})
}
deltas = deltas.filter(delta => {
return this.app.securityStrategy.filterReadDelta(user, delta)
})

return deltas
}

Expand Down
8 changes: 7 additions & 1 deletion lib/dummysecurity.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ module.exports = function (app, config) {

shouldFilterDeltas: () => {
return false
}
},

allowReadOnly: () => {
return true
},

supportsLogin: () => false
}
}
38 changes: 6 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ const express = require('express'),
getPrimaryPort = ports.getPrimaryPort,
getSecondaryPort = ports.getSecondaryPort,
getExternalPort = ports.getExternalPort,
{ startSecurity, getCertificateOptions } = require('./security.js'),
{
startSecurity,
getCertificateOptions,
getSecurityConfig,
saveSecurityConfig
} = require('./security.js'),
{ startDeltaStatistics, incDeltaStatistics } = require('./deltastats'),
DeltaChain = require('./deltachain')

Expand Down Expand Up @@ -415,34 +420,3 @@ Server.prototype.stop = function(cb) {
}
})
}

function pathForSecurityConfig(app) {
return path.join(app.config.configPath, 'security.json')
}

function saveSecurityConfig(app, data, callback) {
const config = JSON.parse(JSON.stringify(data))
const path = pathForSecurityConfig(app)
fs.writeFile(path, JSON.stringify(data, null, 2), err => {
if (!err) {
fs.chmodSync(path, '600')
}
if (callback) {
callback(err)
}
})
}

function getSecurityConfig(app) {
try {
const optionsAsString = fs.readFileSync(pathForSecurityConfig(app), 'utf8')
try {
return JSON.parse(optionsAsString)
} catch (e) {
console.error('Could not parse security config')
return {}
}
} catch (e) {
return {}
}
}
33 changes: 29 additions & 4 deletions lib/interfaces/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const modulesWithKeyword = require('../modules').modulesWithKeyword
const getLogger = require('../logging')
const _putPath = require('../put').putPath
const { getModulePublic } = require('../config/get')
const { queryRequest } = require('../requestResponse')

// #521 Returns path to load plugin-config assets.
const getPluginConfigPublic = getModulePublic('@signalk/plugin-config')
Expand Down Expand Up @@ -177,12 +178,35 @@ module.exports = function (app) {
return _.get(app.signalk.retrieve(), path)
}

function putSelfPath (path, value) {
return _putPath(app, `vessels.self.${path}`, { value: value })
function putSelfPath (path, value, updateCb) {
return _putPath(
app,
'vessels.self',
path,
{ value: value },
null,
null,
updateCb
)
}

function putPath (path, value) {
return _putPath(app, path, { value: value })
function putPath (path, value, updateCb) {
var parts = path.length > 0 ? path.split('.') : []

if (parts.length > 2) {
var context = `${parts[0]}.${parts[1]}`
var skpath = parts.slice(2).join('.')
}

return _putPath(
app,
context,
skpath,
{ value: value },
null,
null,
updateCb
)
}

function registerPlugin (app, pluginName, metadata, location) {
Expand Down Expand Up @@ -242,6 +266,7 @@ module.exports = function (app) {
getPath,
putSelfPath,
putPath,
queryRequest,
error: msg => {
console.error(`${packageName}:${msg}`)
},
Expand Down
9 changes: 6 additions & 3 deletions lib/interfaces/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ module.exports = function (app) {
return
}
var last = app.deltaCache.buildFullFromDeltas(
req.skUser,
req.skPrincipal,
deltas
)
sendResult(last, path)
}
)
}
} else {
var last = app.deltaCache.buildFull(req.skUser, path)
var last = app.deltaCache.buildFull(req.skPrincipal, path)
sendResult(last, path)
}
})
Expand Down Expand Up @@ -149,7 +149,10 @@ module.exports = function (app) {
server: {
id: 'signalk-server-node',
version: app.config.version
}
},
authenticationRequired: app.securityStrategy.isDummy()
? 'never'
: app.securityStrategy.allowReadOnly() ? 'forWrite' : 'always'
})
})
},
Expand Down
Loading

0 comments on commit 43c7021

Please sign in to comment.