diff --git a/src/db.js b/src/db.js index 33a0f1b..4a4cd85 100644 --- a/src/db.js +++ b/src/db.js @@ -12,23 +12,53 @@ export function createViews(couchdb) { language: 'javascript', views: { getSecret: { - map: `function (doc) { - if(doc.secret){ - var key = Object.keys(doc.secret)[0]; - emit(key, {res: doc.secret[key], rev: doc._rev}); - } - }`, +// VIEW getSecret + map: ` +function (doc) { + if(doc.secret){ + var key = Object.keys(doc.secret)[0]; + emit(key, {res: doc.secret[key], rev: doc._rev}); + } +} + `, +// END VIEW }, getMetadatas: { - map: `function (doc) { - if(doc.secret){ - var key = Object.keys(doc.secret)[0]; - var res = doc.secret[key].users; - doc.secret[key].users.forEach(function(user){ - emit(user, {res: {title: key, iv_meta: doc.secret[key].iv_meta, metadatas: doc.secret[key].metadatas}, rev: doc._rev}); - }); - } - }`, +// VIEW getMetadatas + map: ` +function (doc) { + if(doc.secret){ + var key = Object.keys(doc.secret)[0]; + doc.secret[key].users.forEach(function(user){ + var res = {}; + res[key] = { + iv: doc.secret[key].iv_meta, + secret: doc.secret[key].metadatas, + }; + emit(user, res); + }); + } +} + `, +// END VIEW + }, + getDatabase: { +// VIEW getDatabase + map: ` +function (doc) { + if(doc.secret){ + var key = Object.keys(doc.secret)[0]; + doc.secret[key].users.forEach(function(user){ + var res = {}; + res[key] = JSON.parse(JSON.stringify(doc.secret[key])); + res[key].users = [user]; + res[key].rev = doc._rev; + emit(user, res); + }); + } +} + `, +// END VIEW }, }, }).then(() => couchdb.insert(couchdb.databaseName, { @@ -36,12 +66,16 @@ export function createViews(couchdb) { language: 'javascript', views: { getUser: { - map: `function (doc) { - if(doc.user){ - var key = Object.keys(doc.user)[0]; - emit(key, {res: doc.user[key], rev: doc._rev}); - } - }`, +// VIEW getUser + map: ` +function (doc) { + if(doc.user){ + var key = Object.keys(doc.user)[0]; + emit(key, {res: doc.user[key], rev: doc._rev}); + } +} + `, +// END VIEW }, }, })); diff --git a/src/routes/GetDatabase.js b/src/routes/GetDatabase.js index 7ccfe93..bbc3dd0 100644 --- a/src/routes/GetDatabase.js +++ b/src/routes/GetDatabase.js @@ -1,48 +1,47 @@ import { Router } from 'express'; -import url from 'url'; -import _ from 'lodash'; import Console from '../console'; import Utils from '../utils'; +function getDatabase(couchdb, name) { + const view = '_design/secrets/_view/getDatabase'; + return couchdb.get(couchdb.databaseName, view, { key: name }) + .then(({ data }) => + data.rows.reduce((secrets, { value }) => Object.assign(secrets, value), {})); +} export default ({ couchdb }) => { const route = Router(); - route.get('/:name', (req, res) => { + + route.post('/:name', (req, res) => { let rawUser; + let jsonBody; const db = { users: {}, secrets: {} }; Utils.checkSignature({ couchdb, name: req.params.name, - sig: req.query.sig, - data: `${req.baseUrl}${url.parse(req.url).pathname}`, + sig: req.body.sig, + data: req.body.json, }) .then((user) => { rawUser = user; + jsonBody = JSON.parse(req.body.json); db.users[req.params.name] = rawUser.data; - const hashedTitles = Object.keys(rawUser.data.keys); - const secretPromises = []; - if (hashedTitles.length !== 0) { - hashedTitles.forEach((hashedTitle) => { - secretPromises.push( - Utils.secretExists({ couchdb, title: hashedTitle }, false) - .then(secret => ({ - secret, - title: hashedTitle, - }))); - }); - return Promise.all(secretPromises); - } - return Promise.resolve([]); + return getDatabase(couchdb, req.params.name); }) .then((secrets) => { - _.remove(secrets, secret => secret.secret.notFound); - secrets.forEach((rawSecret) => { - const secret = rawSecret.secret.data; - db.secrets[rawSecret.title] = secret; - db.secrets[rawSecret.title].users = [req.params.name]; + const updatedSecrets = {}; + Object.keys(secrets).forEach((key) => { + if (typeof jsonBody[key] === 'undefined' || jsonBody[key] !== secrets[key].rev) { + updatedSecrets[key] = secrets[key]; + } }); - + Object.keys(jsonBody).forEach((key) => { + if (typeof secrets[key] === 'undefined') { + updatedSecrets[key] = false; + } + }); + db.secrets = updatedSecrets; res.json(db); }) .catch((error) => { diff --git a/src/routes/GetUser.js b/src/routes/GetUser.js index 6f9b4c7..53437f9 100644 --- a/src/routes/GetUser.js +++ b/src/routes/GetUser.js @@ -10,16 +10,8 @@ import Utils from '../utils'; function getAllMetadatas(couchdb, name) { const view = '_design/secrets/_view/getMetadatas'; return couchdb.get(couchdb.databaseName, view, { key: name }) - .then(({ data }) => { - const allMetadatas = {}; - data.rows.forEach((row) => { - allMetadatas[row.value.res.title] = { - iv: row.value.res.iv_meta, - secret: row.value.res.metadatas, - }; - }); - return allMetadatas; - }); + .then(({ data }) => + data.rows.reduce((allMetadatas, { value }) => Object.assign(allMetadatas, value), {})); } export default ({ redis, couchdb }) => { diff --git a/src/utils.js b/src/utils.js index 5d72d8d..d23aac7 100644 --- a/src/utils.js +++ b/src/utils.js @@ -141,7 +141,10 @@ function checkSignature({ couchdb, name, sig, data }) { const n = new Buffer(user.publicKey.n, 'base64'); const e = new Buffer(user.publicKey.e, 'base64'); - const publicKey = rsa.setPublicKey(new BigInteger(n.toString('hex'), 16), new BigInteger(e.toString('hex'), 16)); + const publicKey = rsa.setPublicKey( + new BigInteger(n.toString('hex'), 16), + new BigInteger(e.toString('hex'), 16), + ); const signature = new Buffer(sig, 'hex'); const pss = forge.pss.create({