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

added author and userid middleware, refactored, retrieve profile info from...

added author and userid middleware, refactored, retrieve profile info from auth0 on post request, updated default config, added profile resource, added axios dep
parent 16868f36
......@@ -18,6 +18,13 @@
"dbName": "motionbank-api"
}
},
"profiles": {
"mongodb": {
"prefix": "sys_",
"url": "mongodb://localhost:27017/motionbank-api",
"dbName": "motionbank-api"
}
},
"auth": {
"jwks": {
"cache": true,
......
......@@ -415,6 +415,15 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.7.0.tgz",
"integrity": "sha512-32NDda82rhwD9/JBCCkB+MRYDp0oSvlo2IL6rQWA10PQi7tDUM3eqMSltXmY+Oyl/7N3P3qNtAlv7X0d9bI28w=="
},
"axios": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": {
"follow-redirects": "^1.3.0",
"is-buffer": "^1.1.5"
}
},
"babel-code-frame": {
"version": "6.26.0",
"resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz",
......@@ -881,7 +890,6 @@
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"dev": true,
"requires": {
"ms": "2.0.0"
}
......@@ -1378,6 +1386,14 @@
"write": "^0.2.1"
}
},
"follow-redirects": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.1.tgz",
"integrity": "sha512-v9GI1hpaqq1ZZR6pBD1+kI7O24PhDvNGNodjS3MdcEqyrahCp8zbtpv+2B/krUnSmUH80lbAS7MrdeK5IylgKg==",
"requires": {
"debug": "^3.1.0"
}
},
"forever-agent": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
......@@ -1639,8 +1655,7 @@
"is-buffer": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==",
"dev": true
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w=="
},
"is-builtin-module": {
"version": "1.0.0",
......
......@@ -30,6 +30,7 @@
"dependencies": {
"@polka/send-type": "^0.4.0",
"acl": "^0.4.11",
"axios": "^0.18.0",
"body-parser": "^1.18.3",
"config": "^1.30.0",
"cors": "^2.8.4",
......
......@@ -44,8 +44,6 @@ const setup = async function () {
* Setup API server (Polka)
*/
const
models = require('mbjs-data-models'),
Service = require('./service'),
app = polka({
server,
onError (err, req, res, next) {
......@@ -63,12 +61,33 @@ const setup = async function () {
* Set up ACL
*/
const
setupACL = require('./acl'),
setupACL = require('./auth/acl'),
acl = await setupACL(app)
/**
* Middleware
*/
const addUserUUID = require('./middleware/user')
addUserUUID(app)
const addAuthor = require('./middleware/author')
addAuthor(app)
/**
* Configure Profiles
*/
const
Profiles = require('./profiles'),
profiles = new Profiles(app)
profiles.on('message', message => console.log(message))
/**
* Configure resources
*/
const
models = require('mbjs-data-models'),
Service = require('./service')
const annotations = new Service('annotations', app, models.Annotation, winston, acl)
annotations.on('message', message => primus.write(message))
......
const
config = require('config'),
axios = require('axios')
const setup = async function (app) {
app.use(async (req, res, next) => {
if (req.method.toLowerCase() === 'post') {
if (req.body) {
const profile = await axios.get(`${config.auth.jwt.issuer}userinfo`, {
headers: {
Authorization: req.headers.authorization
}
})
req.body.author = {
id: req.user.uuid,
name: profile.data.name
}
}
}
next()
})
}
module.exports = setup
const { ObjectUtil } = require('mbjs-utils')
const setup = async function (app) {
app.use((req, res, next) => {
if (req.user) {
req.user.uuid = ObjectUtil.uuid5(req.user.sub)
}
next()
})
}
module.exports = setup
const
config = require('config'),
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter'),
{ ObjectUtil } = require('mbjs-utils'),
{ MongoDB } = require('mbjs-persistence')
class Profiles extends TinyEmitter {
constructor (app) {
super()
// TODO: make db adapter configurable (nedb, etc.)
this._client = new MongoDB(ObjectUtil.merge({ name: 'profiles', logger: console }, config.get('profiles.mongodb')), 'uuid')
const _this = this
app.get('/profiles/:id', async (req, res) => {
const result = await this.client.get(req.params.id, req.params)
if (result) {
return _this._response(req, res, result)
}
send(res, 404)
})
app.put('/profiles/:id', async (req, res) => {
const data = req.body
let result = await this.client.get(req.params.id)
if (result) {
data.uuid = req.params.id
result = await this.client.update(req.params.id, result, req.params)
return _this._response(req, res, result)
}
send(res, 404)
})
app.patch('/profiles/:id', async (req, res) => {
let existing = await this.client.get(req.params.id)
if (existing) {
existing = ObjectUtil.merge(existing, req.body)
await this.client.update(req.params.id, existing, req.params)
return _this._response(req, res, existing)
}
send(res, 404)
})
app.delete('/profiles/:id', async (req, res) => {
let existing = await this.client.get(req.params.id)
if (existing) {
const result = await this.client.remove(req.params.id, req.params)
if (result) {
return _this._response(req, res, existing)
}
}
send(res, 404)
})
}
_response (req, res, data = {}) {
this.emit('message', { method: req.method, id: data.id })
send(res, 200, data)
}
get client () {
return this._client
}
}
module.exports = Profiles
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