Skip to content

Commit

Permalink
Merge pull request #5 from secretin/offline_sync
Browse files Browse the repository at this point in the history
Manage get database synchro
  • Loading branch information
agix authored Feb 11, 2017
2 parents a857b74 + c929c6e commit 77ce865
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 57 deletions.
76 changes: 55 additions & 21 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,70 @@ 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, {
_id: '_design/users',
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
},
},
}));
Expand Down
49 changes: 24 additions & 25 deletions src/routes/GetDatabase.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
12 changes: 2 additions & 10 deletions src/routes/GetUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 77ce865

Please sign in to comment.