Skip to content

Commit df56b72

Browse files
authored
Merge pull request #66 from datapartyjs/secure-config
Secure config
2 parents ab21322 + e21f55f commit df56b72

10 files changed

Lines changed: 442 additions & 40 deletions

File tree

examples/secure-config.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
const Dataparty = require('../src/index')
3+
4+
const prompt = require('prompt')
5+
6+
let secureConfig = null
7+
8+
async function main(){
9+
const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})
10+
11+
12+
const jsonConfig = new Dataparty.Config.JsonFileConfig({
13+
basePath: '/tmp'
14+
})
15+
16+
17+
secureConfig = new Dataparty.Config.SecureConfig({
18+
config: jsonConfig
19+
})
20+
21+
22+
secureConfig.on('locked', ()=>{ console.log('locked') })
23+
24+
secureConfig.on('unlocked', async ()=>{
25+
console.log('unlocked')
26+
27+
await secureConfig.write('timestamp', Date.now())
28+
})
29+
30+
secureConfig.on('timeout', ()=>{ console.log('timeout') })
31+
32+
secureConfig.on('ready', async ()=>{
33+
console.log('ready')
34+
35+
console.log('config', await secureConfig.readAll())
36+
})
37+
38+
let blocked = false
39+
40+
secureConfig.on('blocked', async (reason)=>{
41+
42+
if(await secureConfig.isInitialized() && secureConfig.isLocked()){
43+
44+
if(blocked){
45+
console.log('blocked true')
46+
await secureConfig.waitForUnlocked()
47+
return
48+
}
49+
50+
blocked = true
51+
console.log('blocked -',reason)
52+
53+
54+
const {password} = await prompt.get({
55+
properties: {
56+
password: {
57+
message: 'Enter password',
58+
hidden: true
59+
}
60+
}})
61+
62+
await secureConfig.unlock(password)
63+
64+
blocked = false
65+
}
66+
67+
68+
})
69+
70+
secureConfig.on('setup-required', async ()=>{
71+
72+
console.log('setup-required')
73+
74+
let password = ''
75+
76+
while(1){
77+
let passes = await prompt.get({
78+
properties: {
79+
password1: {
80+
message: 'Set password',
81+
hidden: true
82+
},
83+
password2: {
84+
message: 'Confim password',
85+
hidden: true
86+
}
87+
}
88+
})
89+
90+
if(passes.password1 == passes.password2){
91+
92+
password = passes.password1
93+
break
94+
}
95+
96+
console.log("passwords don't match")
97+
}
98+
99+
await secureConfig.setPassword(password, {
100+
foo: 'bar'
101+
})
102+
103+
console.log('password set')
104+
105+
106+
await secureConfig.unlock(password)
107+
108+
})
109+
110+
console.log('starting')
111+
112+
await secureConfig.start()
113+
114+
console.log('wait for startup')
115+
116+
await secureConfig.waitForUnlocked('startup')
117+
118+
console.log('wait over')
119+
120+
console.log('main config', await secureConfig.readAll())
121+
122+
let timer = setTimeout(async ()=>{
123+
124+
125+
console.log('timer config', await secureConfig.readAll())
126+
127+
}, 30000)
128+
129+
}
130+
131+
main().catch((err)=>{
132+
console.error('crashed', err)
133+
}).then(()=>{
134+
console.log('done')
135+
})

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.21",
4+
"version": "1.2.22",
55
"main": "dist/dataparty.js",
66
"frontend": "dist/dataparty-browser.js",
77
"backend": "dist/dataparty.js",
@@ -59,7 +59,7 @@
5959
},
6060
"dependencies": {
6161
"@dataparty/bouncer-db": "1.0.1",
62-
"@dataparty/crypto": "1.0.3",
62+
"@dataparty/crypto": "^1.1.1",
6363
"@dataparty/tasker": "^0.0.2",
6464
"@diva.exchange/i2p-sam": "^4.1.8",
6565
"@markwylde/liferaft": "^1.3.4",
@@ -73,7 +73,6 @@
7373
"colors": "1.3.1",
7474
"cors": "^2.8.5",
7575
"debug": "^3.1.0",
76-
"deep-set": "^1.0.1",
7776
"dom-storage": "^2.1.0",
7877
"eventemitter3": "^4.0.7",
7978
"express": "^4.17.1",
@@ -97,7 +96,6 @@
9796
"origin-router": "^1.6.4",
9897
"parse-url": "^5.0.1",
9998
"promisfy": "^1.2.0",
100-
"prompt": "^1.0.0",
10199
"roslib": "^1.3.0",
102100
"sanitize-filename": "^1.6.3",
103101
"simple-peer": "9.7.2",
@@ -130,6 +128,7 @@
130128
"parcel": "^2.3.1",
131129
"path-browserify": "^1.0.1",
132130
"process": "^0.11.10",
131+
"prompt": "^1.3.0",
133132
"punycode": "^1.4.1",
134133
"querystring-es3": "^0.2.1",
135134
"stream-browserify": "^3.0.0",

src/config/iconfig.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
1+
const EventEmitter = require('eventemitter3')
22

33
/**
44
* @interface module:Config.IConfig
55
* @link module.Config
66
*/
7-
class IConfig {
8-
constructor(){}
7+
class IConfig extends EventEmitter {
8+
constructor(){super()}
99
async start(){ throw 'not implemented' }
1010
async clear(){ throw 'not implemented' }
11-
readAll(){ throw 'not implemented' }
12-
read(key){ throw 'not implemented' }
11+
async readAll(){ throw 'not implemented' }
12+
async read(key){ throw 'not implemented' }
1313
async write(key, data){ throw 'not implemented' }
14-
exists(key){ throw 'not implemented' }
14+
async exists(key){ throw 'not implemented' }
1515
async save(){ throw 'not implemented' }
1616
}
1717

src/config/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
exports.NconfConfig = require('./nconf')
88
exports.MemoryConfig = require('./memory')
99
exports.JsonFileConfig = require('./json-file')
10-
exports.LocalStorageConfig = require('./local-storage')
10+
exports.LocalStorageConfig = require('./local-storage')
11+
12+
exports.SecureConfig = require('./secure-config')

src/config/json-file.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ class JsonFileConfig extends IConfig {
5252
logger('started')
5353
}
5454

55-
clear () {
55+
async clear () {
5656
this.content = {}
5757
}
5858

59-
readAll(){
59+
async readAll(){
6060

6161
return Object.assign({}, this.content)
6262
}
6363

64-
read(key){
64+
async read(key){
6565
logger('reading path: ' + key)
6666
return reach( this.content, key)
6767
}
@@ -73,8 +73,8 @@ class JsonFileConfig extends IConfig {
7373
}
7474

7575

76-
exists(key){
77-
return (read(key) !== undefined)
76+
async exists(key){
77+
return (await read(key)) !== undefined
7878
}
7979

8080
async save(){

src/config/local-storage.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class LocalStorageConfig extends IConfig {
2323
this.defaults.logicalSeparator = '.'
2424
}
2525

26-
start () {
27-
return Promise.resolve(this)
26+
async start () {
27+
return this
2828
}
2929

30-
clear () {
30+
async clear () {
3131
localStorage.setItem(this.basePath, JSON.stringify({}))
3232
}
3333

34-
readAll(){
34+
async readAll(){
3535
try{
3636
return Object.assign(
3737
{},
@@ -44,14 +44,14 @@ class LocalStorageConfig extends IConfig {
4444
}
4545
}
4646

47-
read(key){
47+
async read(key){
4848
logger('reading path: ' + key)
49-
return reach( this.readAll(), key)
49+
return reach( await this.readAll(), key)
5050
}
5151

5252
async write(key, value){
5353

54-
let data = this.readAll()
54+
let data = await this.readAll()
5555

5656
deepSet(data, key, value)
5757

@@ -61,8 +61,8 @@ class LocalStorageConfig extends IConfig {
6161
}
6262

6363

64-
exists(key){
65-
return (read(key) !== undefined)
64+
async exists(key){
65+
return (await read(key)) !== undefined
6666
}
6767

6868
async save(){

src/config/memory.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ class MemoryConfig extends IConfig {
2121
this.content = Object.assign({}, defaults)
2222
}
2323

24-
start () {
24+
async start () {
2525
//read and merge defaults
2626
return this
2727
}
2828

29-
clear () {
29+
async clear () {
3030
this.content = {}
3131
}
3232

33-
readAll(){
33+
async readAll(){
3434

3535
return Object.assign({}, this.content)
3636
}
3737

38-
read(key){
38+
async read(key){
3939
logger('reading path: ' + key)
40-
return reach( this.readAll(), key)
40+
return reach( await this.readAll(), key)
4141
}
4242

4343
async write(key, value){
4444

45-
let data = this.readAll()
45+
let data = await this.readAll()
4646

4747
deepSet(data, key, value)
4848

@@ -52,8 +52,8 @@ class MemoryConfig extends IConfig {
5252
}
5353

5454

55-
exists(key){
56-
return (read(key) !== undefined)
55+
async exists(key){
56+
return (await read(key)) !== undefined
5757
}
5858

5959
async save(){

src/config/nconf.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class NconfConfig extends IConfig {
3131
this.started=false
3232
}
3333

34-
start () {
34+
async start () {
3535
if(this.started){ return Promise.resolve(this) }
3636

3737
this.touchDir('')
@@ -66,7 +66,7 @@ class NconfConfig extends IConfig {
6666
return Promise.resolve(this)
6767
}
6868

69-
clear () {
69+
async clear () {
7070
logger('clear')
7171
return Promise((resolve,reject)=>{
7272
logger('clearing')
@@ -80,12 +80,12 @@ class NconfConfig extends IConfig {
8080
})
8181
}
8282

83-
readAll(){
83+
async readAll(){
8484
logger('read all')
8585
return nconf.get();
8686
}
8787

88-
read(key){
88+
async read(key){
8989
logger('reading key: ' + key)
9090
let val = nconf.get(key)
9191

@@ -104,8 +104,8 @@ class NconfConfig extends IConfig {
104104
}
105105

106106

107-
exists(key){
108-
return (read(key) !== undefined)
107+
async exists(key){
108+
return (await read(key)) !== undefined
109109
}
110110

111111
async save(){

0 commit comments

Comments
 (0)