Commit e99679cf authored by A. Koch's avatar A. Koch

added acl basics

parent 8b2e8b12
...@@ -10,6 +10,14 @@ ...@@ -10,6 +10,14 @@
"dbName": "motionbank-api" "dbName": "motionbank-api"
} }
}, },
"acl": {
"mongodb": {
"name": "acl",
"prefix": "acl_",
"url": "mongodb://localhost:27017/motionbank-api",
"dbName": "motionbank-api"
}
},
"auth": { "auth": {
"jwks": { "jwks": {
"cache": true, "cache": true,
......
...@@ -2203,20 +2203,21 @@ ...@@ -2203,20 +2203,21 @@
} }
}, },
"mongodb": { "mongodb": {
"version": "3.0.10", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.0.10.tgz", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.0.tgz",
"integrity": "sha512-jy9s4FgcM4rl8sHNETYHGeWcuRh9AlwQCUuMiTj041t/HD02HwyFgmm2VZdd9/mA9YNHaUJLqj0tzBx2QFivtg==", "integrity": "sha512-fSDZRq9FomRqeDSM7MpMTLa8sz+STs3nZ7Ib0+xvmaKZ6nquNDN4zGDsVhjto6UozFvHMDYJMAfJwhqUygXs9g==",
"requires": { "requires": {
"mongodb-core": "3.0.9" "mongodb-core": "3.1.0"
} }
}, },
"mongodb-core": { "mongodb-core": {
"version": "3.0.9", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.0.9.tgz", "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.0.tgz",
"integrity": "sha512-buOWjdLLBlEqjHDeHYSXqXx173wHMVp7bafhdHxSjxWdB9V6Ri4myTqxjYZwL/eGFZxvd8oRQSuhwuIDbaaB+g==", "integrity": "sha512-qRjG62Fu//CZhkgn0jA/k8jh5MhACIq8cOJUryH6sck87pgt+C222MSD02tsCq5zNo/B6ZFHtNodZ2qpf8E86g==",
"requires": { "requires": {
"bson": "~1.0.4", "bson": "~1.0.4",
"require_optional": "^1.0.1" "require_optional": "^1.0.1",
"saslprep": "^1.0.0"
} }
}, },
"morgan": { "morgan": {
...@@ -2778,6 +2779,12 @@ ...@@ -2778,6 +2779,12 @@
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
}, },
"saslprep": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.0.tgz",
"integrity": "sha512-5lvKUEQ7lAN5/vPl5d3k8FQeDbEamu9kizfATfLLWV5h6Mkh1xcieR1FSsJkcSRUk49lF2tAW8gzXWVwtwZVhw==",
"optional": true
},
"schema-object": { "schema-object": {
"version": "4.0.11", "version": "4.0.11",
"resolved": "https://registry.npmjs.org/schema-object/-/schema-object-4.0.11.tgz", "resolved": "https://registry.npmjs.org/schema-object/-/schema-object-4.0.11.tgz",
......
const
Acl = require('acl'),
config = require('config'),
Backend = Acl.mongodbBackend,
{ MongoDB } = require('mbjs-persistence')
const setupACL = async function (app) {
const cfg = config.get('acl.mongodb')
const client = new MongoDB(cfg, 'uuid')
await client.connect()
const acl = new Acl(new Backend(client.db, cfg.prefix))
return acl
}
module.exports = setupACL
...@@ -10,61 +10,72 @@ const ...@@ -10,61 +10,72 @@ const
{ json } = require('body-parser'), { json } = require('body-parser'),
{ ObjectUtil } = require('mbjs-utils') { ObjectUtil } = require('mbjs-utils')
/** const setup = async function () {
* HTTP server /**
*/ * HTTP server
const */
host = config.get('http.host'), const
port = config.get('http.port'), host = config.get('http.host'),
server = http.createServer() port = config.get('http.port'),
server = http.createServer()
/** /**
* Logger * Logger
*/ */
const winston = require('winston') const winston = require('winston')
winston.level = process.env.NODE_ENV === 'production' ? 'error' : 'info' winston.level = process.env.NODE_ENV === 'production' ? 'error' : 'info'
winston.level = process.env.LOG_LEVEL || winston.level winston.level = process.env.LOG_LEVEL || winston.level
/** /**
* Set up WebSockets (Primus) * Set up WebSockets (Primus)
*/ */
const primus = new Primus(server, { transformer: 'uws' }) const primus = new Primus(server, { transformer: 'uws' })
primus.on('connection', spark => { primus.on('connection', spark => {
winston.log('debug', `Spark ${spark.id} connected with address ${spark.address}`) winston.log('debug', `Spark ${spark.id} connected with address ${spark.address}`)
}) })
primus.on('disconnection', spark => { primus.on('disconnection', spark => {
winston.log('debug', `Spark ${spark.id} disconnected with address ${spark.address}`) winston.log('debug', `Spark ${spark.id} disconnected with address ${spark.address}`)
}) })
/** /**
* Setup API server (Polka) * Setup API server (Polka)
*/ */
const const
models = require('mbjs-data-models'), models = require('mbjs-data-models'),
Service = require('./service'), Service = require('./service'),
app = polka({ server }) app = polka({ server })
const jwtCheck = jwt(ObjectUtil.merge({
secret: jwks.expressJwtSecret(config.get('auth.jwks'))
}, config.get('auth.jwt')))
app.use(cors, jwtCheck, json(), morgan(process.env.NODE_ENV === 'production' ? 'common' : 'dev'))
const jwtCheck = jwt(ObjectUtil.merge({ /**
secret: jwks.expressJwtSecret(config.get('auth.jwks')) * Set up ACL
}, config.get('auth.jwt'))) */
const
setupACL = require('./acl'),
acl = await setupACL(app)
app.use(cors, jwtCheck, json(), morgan(process.env.NODE_ENV === 'production' ? 'common' : 'dev')) /**
* Configure resources
*/
const annotations = new Service('annotations', app, models.Annotation, winston, acl)
annotations.on('message', message => primus.write(message))
/** const maps = new Service('maps', app, models.Map, winston, acl)
* Configure resources maps.on('message', message => primus.write(message))
*/
const annotations = new Service('annotations', app, models.Annotation, winston)
annotations.on('message', message => primus.write(message))
const maps = new Service('maps', app, models.Map, winston) const documents = new Service('documents', app, models.Document, winston, acl)
maps.on('message', message => primus.write(message)) documents.on('message', message => primus.write(message))
const documents = new Service('documents', app, models.Document, winston) /**
documents.on('message', message => primus.write(message)) * Start server
*/
app.listen(port, host).then(() => winston.log('info', `API started on ${host}:${port}`))
}
/** setup()
* Start server
*/
app.listen(port, host).then(() => winston.log('info', `API started on ${host}:${port}`))
...@@ -6,10 +6,11 @@ const ...@@ -6,10 +6,11 @@ const
{ MongoDB } = require('mbjs-persistence') { MongoDB } = require('mbjs-persistence')
class Service extends TinyEmitter { class Service extends TinyEmitter {
constructor (name, app, model, logger) { constructor (name, app, model, logger, acl) {
super() super()
this._name = name this._name = name
this._acl = acl
this._Model = model this._Model = model
// TODO: make db adapter configurable (nedb, etc.) // TODO: make db adapter configurable (nedb, etc.)
this._client = new MongoDB(ObjectUtil.merge({ name, logger }, config.get('resources.mongodb')), 'uuid') this._client = new MongoDB(ObjectUtil.merge({ name, logger }, config.get('resources.mongodb')), 'uuid')
...@@ -93,6 +94,10 @@ class Service extends TinyEmitter { ...@@ -93,6 +94,10 @@ class Service extends TinyEmitter {
return this._client return this._client
} }
get acl () {
return this._acl
}
get ModelConstructor () { get ModelConstructor () {
return this._Model return this._Model
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment