-
Notifications
You must be signed in to change notification settings - Fork 2
/
acl-metadata.js
69 lines (64 loc) · 2.56 KB
/
acl-metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const errors = require('@kba/anno-errors')
const jwt = require('jsonwebtoken')
const axios = require('axios')
const {envyConf} = require('envyconf')
const cacheManager = require('cache-manager')
const _caches = {}
function cached(metadataEndpoint, collection, context, cb) {
const cache = (collection in _caches) ? _caches[collection] : cacheManager.caching({
store: 'memory',
max: 1000,
ttl: 60 * 60 * 12
})
return cache.wrap(context, function() {
return new Promise(function(resolve, reject) {
axios.get(`${metadataEndpoint}?uri=${context}`)
.then(({data}) => resolve(data))
.catch(err => reject(err))
})
})
}
module.exports = function AclMetadataMiddlewareFactory(cb) {
function AclMetadataMiddleware(req, resp, next) {
const {collection, collectionConfig} = req.annoOptions = req.annoOptions || {}
if (!collectionConfig) {
// console.log(errors.badRequest("aclMetadata: Missing 'collection' in the request context"))
return next()
}
const metadataToken = req.header('x-anno-metadata')
const context = req.header('x-anno-context')
const {metadataEndpoint, secret} = collectionConfig
if (metadataToken) {
if (!secret) {
console.log(errors.badRequest(`No 'secret' for collection: ${collection}`))
return next()
}
jwt.verify(metadataToken, secret, (err, decoded) => {
if (err) {
return next(errors.badRequest(`JWT choked on this X-Anno-Metadata token: ${err}`))
}
req.annoOptions.metadata = decoded
return next()
})
} else if (!metadataEndpoint) {
// console.log(errors.badRequest(`Missing 'metadataEndpoint' in the configuration of collection '${collection}'`))
return next()
} else if (!context) {
console.log(errors.badRequest(`No X-Anno-Context was passed, nothing to do :(`))
return next()
} else {
cached(metadataEndpoint, collection, context)
.then(data => {
req.annoOptions.metadata = data
console.log("AclMetadataMiddleware finished", data)
next()
})
.catch(err => {
console.log(err)
next()
})
}
}
AclMetadataMiddleware.unless = require('express-unless')
return cb(null, AclMetadataMiddleware)
}