Skip to content

Commit

Permalink
Merge pull request #7 from secretin/support_history
Browse files Browse the repository at this point in the history
Prepare server to support getHistory
  • Loading branch information
agix authored Jun 23, 2017
2 parents 9c84e93 + 7912a17 commit d9b44c6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import getUser from './routes/GetUser';
import getProtectKey from './routes/GetProtectKey';
import getDatabase from './routes/GetDatabase';
import getSecret from './routes/GetSecret';
import getHistory from './routes/GetHistory';
import createUser from './routes/CreateUser';
import createSecret from './routes/CreateSecret';
import deleteSecret from './routes/DeleteSecret';
Expand Down Expand Up @@ -54,6 +55,7 @@ initializeDb(config, (couchdb, redis) => {
app.use('/secret', createSecret({ couchdb, redis }));
app.use('/secret', updateSecret({ couchdb, redis }));
app.use('/secret', deleteSecret({ couchdb, redis }));
app.use('/history', getHistory({ couchdb, redis }));
app.use('/share', share({ couchdb, redis }));
app.use('/unshare', unshare({ couchdb, redis }));
app.use('/newKey', newKey({ couchdb, redis }));
Expand Down
3 changes: 3 additions & 0 deletions src/routes/CreateSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default ({ couchdb }) => {
})
.then(() => {
Utils.reason(res, 403, 'Secret already exists');
throw 'Secret already exists';
}, (error) => {
if (error.text === 'Secret not found') {
const doc = {
Expand All @@ -30,6 +31,8 @@ export default ({ couchdb }) => {
iv: jsonBody.iv,
metadatas: jsonBody.metadatas,
iv_meta: jsonBody.iv_meta,
history: jsonBody.history,
iv_history: jsonBody.iv_history,
users: [req.params.name],
},
},
Expand Down
52 changes: 52 additions & 0 deletions src/routes/GetHistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Router } from 'express';
import forge from 'node-forge';
import url from 'url';

import Console from '../console';
import Utils from '../utils';

export default ({ couchdb }) => {
const route = Router();
route.get('/:name/:title', (req, res) => {
Utils.checkSignature({
couchdb,
name: req.params.name,
sig: req.query.sig,
data: `${req.baseUrl}${url.parse(req.url).pathname}`,
})
.then(() => Utils.secretExists({ couchdb, title: req.params.title }))
.then((rawSecret) => {
const secret = rawSecret.data;
if (secret.users.indexOf(req.params.name) !== -1) {
return secret;
}
throw {
code: 404,
text: 'Secret not found',
};
})
.catch((error) => {
if (error.text === 'Secret not found') {
return {
history: forge.util.bytesToHex((forge.random.getBytesSync(128))),
iv_history: forge.util.bytesToHex((forge.random.getBytesSync(16))),
};
}
throw error;
})
.then((secret) => {
const newSecret = secret;
delete newSecret.metadatas;
delete newSecret.iv_meta;
delete newSecret.users;
delete newSecret.secret;
delete newSecret.iv;
res.json(newSecret);
})
.catch((error) => {
Console.error(res, error);
});
});

return route;
};
9 changes: 6 additions & 3 deletions src/routes/GetSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ export default ({ couchdb }) => {
return {
secret: forge.util.bytesToHex((forge.random.getBytesSync(128))),
iv: forge.util.bytesToHex((forge.random.getBytesSync(16))),
metadatas: forge.util.bytesToHex((forge.random.getBytesSync(256))),
iv_meta: forge.util.bytesToHex((forge.random.getBytesSync(16))),
users: [req.params.name],
};
}
throw error;
})
.then((secret) => {
res.json(secret);
const newSecret = secret;
delete newSecret.metadatas;
delete newSecret.iv_meta;
delete newSecret.history;
delete newSecret.iv_history;
res.json(newSecret);
})
.catch((error) => {
Console.error(res, error);
Expand Down
2 changes: 2 additions & 0 deletions src/routes/NewKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export default ({ couchdb }) => {
doc.secret[jsonBody.title].iv = jsonBody.secret.iv;
doc.secret[jsonBody.title].iv_meta = jsonBody.secret.iv_meta;
doc.secret[jsonBody.title].metadatas = jsonBody.secret.metadatas;
doc.secret[jsonBody.title].iv_history = jsonBody.secret.iv_history;
doc.secret[jsonBody.title].history = jsonBody.secret.history;
return couchdb.update(couchdb.databaseName, doc);
})
.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/routes/UpdateSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default ({ couchdb }) => {
doc.secret[jsonBody.title].secret = jsonBody.secret;
doc.secret[jsonBody.title].iv_meta = jsonBody.iv_meta;
doc.secret[jsonBody.title].metadatas = jsonBody.metadatas;
doc.secret[jsonBody.title].history = jsonBody.history;
doc.secret[jsonBody.title].iv_history = jsonBody.iv_history;
return couchdb.update(couchdb.databaseName, doc);
})
.then(() => {
Expand Down

0 comments on commit d9b44c6

Please sign in to comment.