Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ var PouchPromise = require('pouchdb-promise');
var configId = '_local/crypto';
var transform = require('transform-pouch').transform;
var uuid = require('node-uuid');
function InvalidPasswordError () {
this.name = 'InvalidPasswordError';
this.message = 'unable to authenticate'
}
function genKey(password, salt) {
return new PouchPromise(function (resolve, reject) {
pbkdf2.pbkdf2(password, salt, 1000, 256 / 8, function (err, key) {
Expand All @@ -26,7 +30,7 @@ function cryptoInit(password, options) {
if (options && options.ignore) {
ignore = ignore.concat(options.ignore)
}

var pending = db.get(configId).then(function (doc){
if (!doc.salt) {
throw {
Expand Down Expand Up @@ -70,7 +74,11 @@ function cryptoInit(password, options) {
},
outgoing: function (doc) {
return pending.then(function () {
return decrypt(doc);
try {
return decrypt(doc);
} catch (err) {
throw new InvalidPasswordError();
}
});
}
});
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,17 @@ test('throws error when document has attachments', function (t) {
t.ok(/Attachments cannot be encrypted/.test(e.message), 'throws error');
})
})
test('wrong password', function (t) {
t.plan(1);
var dbName = 'nine';
var db = new PouchDB(dbName, {db: memdown});
db.crypto('correct')
db.put({foo: 'bar'}, 'baz').then(function () {
db.crypto('wrong')
return db.get('baz');
}).then(function (doc) {
t.fail('retrieved document with bad password')
}).catch(function (err) {
t.equals(err.name, 'InvalidPasswordError', 'returns correct error.')
})
});