Skip to content

Commit a3b2e56

Browse files
author
sevenbitbyte
committed
completed secure config
1 parent 1e209ba commit a3b2e56

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

examples/secure-config.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,50 @@ const prompt = require('prompt')
55

66

77
async function main(){
8-
//const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})
8+
const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})
99

1010

1111
const jsonConfig = new Dataparty.Config.JsonFileConfig({
12-
foo: 'bar',
1312
basePath: '/tmp'
1413
})
1514

1615

1716
const secureConfig = new Dataparty.Config.SecureConfig({
18-
config: jsonConfig
17+
config: memoryConfig
1918
})
2019

2120

2221
secureConfig.on('locked', ()=>{ console.log('locked') })
2322

24-
secureConfig.on('unlocked', ()=>{ console.log('unlocked') })
23+
secureConfig.on('unlocked', async ()=>{
24+
console.log('unlocked')
25+
26+
await secureConfig.write('timestamp', Date.now())
27+
})
2528

2629
secureConfig.on('timeout', ()=>{ console.log('timeout') })
2730

28-
secureConfig.on('ready', ()=>{ console.log('ready') })
31+
secureConfig.on('ready', async ()=>{
32+
console.log('ready')
33+
34+
console.log('config', await secureConfig.readAll())
35+
})
36+
37+
let blocked = false
2938

3039
secureConfig.on('blocked', async (reason)=>{
3140

3241
if(await secureConfig.isInitialized() && secureConfig.isLocked()){
3342

34-
43+
if(blocked){
44+
await secureConfig.waitForUnlocked()
45+
return
46+
}
3547

48+
blocked = true
3649
console.log('blocked -',reason)
3750

51+
3852
const {password} = await prompt.get({
3953
properties: {
4054
password: {
@@ -43,7 +57,9 @@ async function main(){
4357
}
4458
}})
4559

46-
secureConfig.unlock(password)
60+
await secureConfig.unlock(password)
61+
62+
blocked = false
4763
}
4864

4965

@@ -78,7 +94,9 @@ async function main(){
7894
console.log("passwords don't match")
7995
}
8096

81-
secureConfig.setPassword(password)
97+
secureConfig.setPassword(password, {
98+
foo: 'bar'
99+
})
82100

83101
})
84102

@@ -88,6 +106,15 @@ async function main(){
88106

89107
console.log('wait over')
90108

109+
console.log('main config', await secureConfig.readAll())
110+
111+
setTimeout(async ()=>{
112+
113+
114+
console.log('timer config', await secureConfig.readAll())
115+
116+
}, 30000)
117+
91118
//process.exit()
92119
}
93120

src/config/secure-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const PASSWORD_HASHING_ROUNDS = 1000000
1212
class SecureConfig extends IConfig {
1313
constructor({
1414
id = 'secure-config',
15-
config, timeoutMs=60*10*1000, includeActivity=true
15+
config, timeoutMs=15*1000, includeActivity=true
1616
}){
1717
super()
1818
this.id = id || 'secure-config'
@@ -22,7 +22,7 @@ class SecureConfig extends IConfig {
2222
this.identity = null
2323
this.timer = null
2424
this.lastActivity = null
25-
this.timeoutMs = timeoutMs || 60*10*1000
25+
this.timeoutMs = timeoutMs || 15*1000
2626
this.includeActivity = includeActivity || true
2727
}
2828

0 commit comments

Comments
 (0)