hoodie-plugin-store-crypto
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -8,4 +8,19 @@ 'use strict' | ||
function cryptoStore (hoodie) { | ||
var state = {} | ||
var withIdPrefixStore = {} // store prefix APIs from hoodie-store. Workaround for #42 | ||
var state = { | ||
getWithPrefixAPI: function (prefix) { // get a prefix API. This is a workaround for #42 | ||
if (prefix == null) { | ||
return hoodie.store | ||
} | ||
if (withIdPrefixStore[prefix] != null) { | ||
return withIdPrefixStore[prefix] | ||
} | ||
withIdPrefixStore[prefix] = hoodie.store.withIdPrefix(prefix) | ||
return withIdPrefixStore[prefix] | ||
} | ||
} | ||
var handler = { | ||
@@ -18,2 +33,4 @@ on: hoodie.store.on, | ||
hoodie.cryptoStore = bindFunctions(hoodie.store, state, null, handler, true) | ||
hoodie.account.on('signout', hoodie.cryptoStore.lock) | ||
} |
@@ -43,4 +43,3 @@ 'use strict' | ||
withIdPrefix: function (moarPrefix) { | ||
var oldPrefix = prefix || '' | ||
var nextPrefix = oldPrefix + moarPrefix | ||
var nextPrefix = prefix == null ? moarPrefix : prefix + moarPrefix | ||
var nextParentEmitter = prefix == null ? emitter : parentEmitter | ||
@@ -57,2 +56,4 @@ | ||
api.lock = require('./lock').bind(null, state) | ||
api.withPassword = function (password, salt) { | ||
@@ -59,0 +60,0 @@ var nextState = Object.create(state) |
@@ -52,11 +52,20 @@ 'use strict' | ||
return store.db.allDocs({include_docs: true}) | ||
.then(function (docs) { | ||
var filtered = docs.rows // update all encryped docs! _design/* too! | ||
.then(function (rows) { | ||
var docs = rows.rows // update all encryped docs! _design/* too! | ||
.map(function (row) { | ||
return row.doc | ||
}) | ||
return docs | ||
}) | ||
.catch(function (err) { // TODO: remove if Store-client with db as getter is published | ||
if (err.message === 'database is destroyed') { | ||
return store.findAll() | ||
} | ||
throw err | ||
}) | ||
.then(function (docs) { | ||
var decrypted = docs | ||
.filter(isEncryptedObject) | ||
var decrypted = filtered | ||
.map(function (doc) { | ||
@@ -63,0 +72,0 @@ return decryptOne(data.oldKey, doc) |
@@ -38,7 +38,21 @@ 'use strict' | ||
.then(function (res) { | ||
var objects = res.rows | ||
return res.rows | ||
.filter(isntDesignDoc) | ||
.map(function (row) { | ||
return decryptOne(key, row.doc) | ||
return row.doc | ||
}) | ||
}) | ||
.catch(function (error) { // workaround for #42 | ||
if (error.message === 'database is destroyed') { | ||
return state.getWithPrefixAPI(prefix).findAll() | ||
} | ||
throw error | ||
}) | ||
.then(function (res) { | ||
var objects = res | ||
.map(function (doc) { | ||
return decryptOne(key, doc) | ||
.catch(function (err) { | ||
@@ -50,3 +64,3 @@ var parentState = Object.getPrototypeOf(state) | ||
return decryptOne(parentState.key, row.doc) | ||
return decryptOne(parentState.key, doc) | ||
}) | ||
@@ -53,0 +67,0 @@ }) |
@@ -9,2 +9,3 @@ 'use strict' | ||
var findAll = require('./find-all') | ||
var remove = require('./remove') | ||
@@ -39,4 +40,4 @@ module.exports = removeAll | ||
.then(function (encryped) { | ||
return store.db.bulkDocs(encryped) | ||
.then(function (encrypted) { | ||
return store.db.bulkDocs(encrypted) | ||
@@ -47,3 +48,3 @@ .then(function (result) { | ||
var encryptDoc = encryped[index] | ||
var encryptDoc = encrypted[index] | ||
encryptDoc._rev = oneResult.rev | ||
@@ -55,3 +56,19 @@ return decryptOne(key, encryptDoc) | ||
}) | ||
.catch(function (error) { | ||
if (error.message === 'database is destroyed') { // workaround for #42 | ||
var deleted = encrypted.map(function (doc) { | ||
return remove(store, state, prefix, doc._id) | ||
.catch(function (error) { | ||
return error | ||
}) | ||
}) | ||
return Promise.all(deleted) | ||
} | ||
throw error | ||
}) | ||
}) | ||
} |
@@ -43,8 +43,22 @@ 'use strict' | ||
.then(function (result) { | ||
var docs = result.rows | ||
.then(function (res) { | ||
return res.rows | ||
.filter(isntDesignDoc) | ||
.map(function (row) { | ||
return decryptOne(key, row.doc) | ||
return row.doc | ||
}) | ||
}) | ||
.catch(function (error) { // workaround for #42 | ||
if (error.message === 'database is destroyed') { | ||
return state.getWithPrefixAPI(prefix).findAll() | ||
} | ||
throw error | ||
}) | ||
.then(function (result) { | ||
var docs = result | ||
.map(function (doc) { | ||
return decryptOne(key, doc) | ||
.catch(function (err) { | ||
@@ -56,3 +70,3 @@ var parentState = Object.getPrototypeOf(state) | ||
return decryptOne(parentState.key, row.doc) | ||
return decryptOne(parentState.key, doc) | ||
}) | ||
@@ -59,0 +73,0 @@ }) |
{ | ||
"name": "hoodie-plugin-store-crypto", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "End-to-end crypto plugin for the Hoodie client store.", | ||
@@ -20,3 +20,8 @@ "main": "index.js", | ||
"hoodie", | ||
"plugin" | ||
"plugin", | ||
"storage", | ||
"data", | ||
"crypto", | ||
"end-to-end", | ||
"ete" | ||
], | ||
@@ -23,0 +28,0 @@ "author": "Christopher Astfalk <christopher.astfalk@icloud.com>", |
@@ -177,2 +177,15 @@ # hoodie-plugin-store-crypto | ||
#### Sign out | ||
The `cryptoStore` listen automatically to [`hoodie.account.on('signout')`](http://docs.hood.ie/en/latest/api/client/hoodie.account.html#events) events and locks itself. You don't need to add any setup for it. | ||
The [`cryptoStore.lock()`](#cryptostorelock) method is there, so that you can add a lock after a timeout functionality or lock the store in a save way when closing an tab. | ||
```javascript | ||
window.addEventListener('beforeunload', function (event) { | ||
// do your cleanup | ||
hoodie.cryptoStore.lock() // lock the cryptoStore in an cryptographic save way. | ||
}) | ||
``` | ||
#### Open your app while signed in | ||
@@ -278,2 +291,4 @@ | ||
'_deleted', | ||
'_attachments', | ||
'_conflicts', | ||
'hoodie' | ||
@@ -312,2 +327,4 @@ ] | ||
'_deleted', | ||
'_attachments', | ||
'_conflicts', | ||
'hoodie' | ||
@@ -348,2 +365,3 @@ ] | ||
- [cryptoStore.changePassword(oldPassword, newPassword)](#cryptostorechangepasswordoldpassword-newpassword) | ||
- [cryptoStore.lock()](#cryptostorelock) | ||
- [cryptoStore.add(properties)](#cryptostoreaddproperties) | ||
@@ -509,2 +527,14 @@ - [cryptoStore.add(arrayOfProperties)](#cryptostoreaddarrayofproperties) | ||
### cryptoStore.lock() | ||
```javascript | ||
cryptoStore.lock() | ||
``` | ||
This locks the store and every method fails until a new password is set. It also overwrites the internal key's memory in a in an cryptographic save way (10 times). | ||
Resolves with a Boolean. `true` if the store is now locked, `false` if the store was already locked. | ||
The `cryptoStore` listen automatically to [`hoodie.account.on('signout')`](http://docs.hood.ie/en/latest/api/client/hoodie.account.html#events) events and locks itself. | ||
### cryptoStore.add(properties) | ||
@@ -511,0 +541,0 @@ |
93009
34
1127
1548