This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeyManagement.js
90 lines (75 loc) · 1.95 KB
/
keyManagement.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const debug = require('debug')('keyManagement')
const Nano = require('nano')
const router = require('express').Router()
const kuuid = require('kuuid')
const path = require('path')
const nano = Nano(process.env.COUCH_URL)
const db = nano.db.use(process.env.COUCH_KEYS_DATABASE)
router.get('/', (req, res, next) => {
res.sendFile(path.join(__dirname, 'static', 'key-management.html'))
})
router.post('/create', async (req, res, next) => {
debug(req.body)
if (req.body.keyname) {
const keyDetails = {
_id: kuuid.id(),
owner: res.locals.w3id_userid,
name: req.body.keyname,
created: Date.now()
}
await db.insert(keyDetails)
res.json({
status: 'ok',
msg: 'Key successfully created',
data: keyDetails
})
} else {
res.status(401)
res.json({
status: 'err',
msg: 'Required parameters not passed'
})
}
})
router.post('/delete', async (req, res, next) => {
if (req.body.key) {
const existingKey = await db.get(req.body.key)
if (existingKey) {
await db.destroy(existingKey._id, existingKey._rev)
res.json({
status: 'ok',
msg: `Key "${req.body.key} deleted"`
})
} else {
res.status(422)
res.json({
status: 'err',
msg: `Key "${req.body.key} could not be found for deletion"`
})
}
} else {
res.status(422)
res.json({
status: 'err',
msg: 'No key was passed for deletion'
})
}
})
router.get('/list', async (req, res, next) => {
const whitelistProperties = ['_id', 'owner', 'name', 'created']
const keys = await db.list({ include_docs: true }).then((result) => {
const sanitisedOutput = result.rows.map(doc => {
const sanitisedDoc = {}
whitelistProperties.forEach(key => {
sanitisedDoc[key] = doc.doc[key]
})
return sanitisedDoc
})
return sanitisedOutput
})
res.json({
status: 'ok',
data: keys
})
})
module.exports = router