Skip to content

Commit 2f7fcc0

Browse files
committed
secure echo example
1 parent d19d0cc commit 2f7fcc0

8 files changed

Lines changed: 84 additions & 8 deletions

File tree

public/app.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ async function main(){
6161
//console.log('send')
6262

6363

64+
let callTime2 = new DeltaTime()
65+
66+
callTime2.start()
67+
68+
const echoReply2 = await party.comms.call('secure-echo', {t:(new Date()).getTime()}, {
69+
expectClearTextReply: true,
70+
sendClearTextRequest: false,
71+
useSessions: false
72+
})
73+
74+
callTime2.end()
75+
complete++
76+
77+
const text = `call deltaMs=<i>${callTime2.deltaMs}</i>ms complete ${complete}`
78+
document.getElementById("console").innerHTML = text
79+
80+
}, 150)
81+
82+
setInterval(async ()=>{
83+
//console.log('send')
84+
85+
6486
let callTime2 = new DeltaTime()
6587

6688
callTime2.start()
@@ -78,7 +100,6 @@ async function main(){
78100
document.getElementById("console").innerHTML = text
79101

80102
}, 75)
81-
82103

83104
}
84105

src/party/iparty.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ class IParty {
200200

201201
if (!cfgIdenStr){
202202
debug('generated new identity')
203-
this._identity = new dataparty_crypto.Identity({id: 'primary'})
204-
await this.config.write(path, this._identity.toJSON(true))
203+
204+
await this.resetIdentity()
205205
} else {
206206
debug('loaded identity')
207207
this._identity = dataparty_crypto.Identity.fromString(JSON.stringify(cfgIdenStr))
@@ -211,6 +211,10 @@ class IParty {
211211
async resetIdentity(){
212212
const path = 'identity'
213213
await this.config.write(path, null)
214+
215+
this._identity = new dataparty_crypto.Identity({id: 'primary'})
216+
await this.config.write(path, this._identity.toJSON(true))
217+
214218
await this.loadIdentity()
215219
}
216220

src/service/endpoint-context.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class EndpointContext {
3636
setCloud(cloud){ this.cloud = cloud }
3737

3838
setSenderKey(key){
39-
this.
4039
this.senderKey = key
4140
}
4241

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Joi = require('@hapi/joi')
2+
const Hoek = require('@hapi/hoek')
3+
const {Message, Routines} = require('@dataparty/crypto')
4+
const debug = require('debug')('dataparty.endpoint.secure-echo')
5+
6+
const IEndpoint = require('../iendpoint')
7+
8+
module.exports = class SecureEchoEndpoint extends IEndpoint {
9+
10+
static get Name(){
11+
return 'secure-echo'
12+
}
13+
14+
15+
static get Description(){
16+
return 'Secure echo input'
17+
}
18+
19+
static get MiddlewareConfig(){
20+
return {
21+
pre: {
22+
decrypt: true,
23+
validate: Joi.object().keys(null).description('any input allowed'),
24+
},
25+
post: {
26+
encrypt: false,
27+
validate: Joi.object().keys(null).description('any output allowed')
28+
}
29+
}
30+
}
31+
32+
static async run(ctx){
33+
34+
ctx.debug('hello')
35+
debug('echo')
36+
ctx.debug('ctx.input', ctx.input)
37+
38+
return ctx.input
39+
}
40+
}

src/service/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ module.exports = {
3333
},
3434
endpoint: {
3535
echo: require('./endpoints/echo'),
36+
secureecho: require('./endpoints/secure-echo'),
3637
identity: require('./endpoints/service-identity')
3738
},
3839
endpoint_paths: {
3940
echo: Path.join(__dirname, './endpoints/echo.js'),
41+
secureecho: Path.join(__dirname, './endpoints/secure-echo.js'),
4042
identity: Path.join(__dirname, './endpoints/service-identity.js')
4143
}
4244
}

src/service/middleware/pre/decrypt.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ module.exports = class Decrypt extends IMiddleware {
2929

3030
static async run(context){
3131

32-
if (!Hoek.reach(context, 'endpoint.MiddlewareConfig.pre.decrypt', false)){
33-
return
34-
}
32+
if (!Config){ return }
33+
34+
context.debug('input', context.input)
3535

3636

3737
const msg = new Message(context.input)
38-
const jsonContent = await msg.decrpyt(this.serviceParty.privateIdentity)
38+
context.debug('privateIdentity', context.party.privateIdentity)
39+
3940
const publicKeys = Routines.extractPublicKeys(msg.enc)
4041

42+
context.debug('sender', publicKeys)
43+
context.debug(typeof context.party.privateIdentity.key.private.box)
44+
context.debug(context.input.enc)
45+
46+
const jsonContent = await msg.decrypt(context.party.privateIdentity)
47+
48+
4149
context.setSenderKey({
4250
type: 'ecdsa',
4351
public: publicKeys

test/test-service-compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ExampleService extends Dataparty.IService {
1717
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)
1818

1919
this.addEndpoint(Dataparty.endpoint_paths.echo)
20+
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
2021
//this.addEndpoint(Dataparty.endpoint_paths.identity)
2122

2223
}

test/test-service-host.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ExampleService extends Dataparty.IService {
1616
this.addMiddleware(Dataparty.middleware_paths.post.encrypt)
1717

1818
this.addEndpoint(Dataparty.endpoint_paths.echo)
19+
this.addEndpoint(Dataparty.endpoint_paths.secureecho)
1920
this.addEndpoint(Dataparty.endpoint_paths.identity)
2021
}
2122

0 commit comments

Comments
 (0)