Skip to content
Draft
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
1 change: 0 additions & 1 deletion examples/secure-config-argon2.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const HELP_INFO = `
`

async function main(){
const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'})


const jsonConfig = new Dataparty.Config.JsonFileConfig({
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"express-ipfilter": "^1.3.2",
"express-list-routes": "^1.1.9",
"git-repo-info": "^2.1.1",
"joi": "^17.13.3",
"glob": "^13.0.6",
"joi": "^18.2.1",
"joi-objectid": "^4.0.2",
"jshashes": "^1.0.8",
"jsonpath-plus": "^0.20.1",
Expand All @@ -108,6 +109,7 @@
"simple-peer": "9.11.1",
"source-map": "^0.7.3",
"store-js": "^2.0.4",
"tar": "^7.5.15",
"tingodb": "^0.6.1",
"touch": "^3.1.0",
"url-parse": "^1.4.7",
Expand All @@ -120,6 +122,7 @@
"devDependencies": {
"@dataparty/bouncer-model": "1.4.3",
"@hapi/code": "^9.0.1",
"@hapi/joi": "^17.1.1",
"@hapi/lab": "^25.0.1",
"argon2": "^0.30.3",
"argon2-browser": "^1.18.0",
Expand Down
20 changes: 16 additions & 4 deletions src/comms/peer-comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@ class PeerComms extends ISocketComms {

if(this.party.hostRunner){
const actor = await this.party.hostRunner.auth.lookupIdentity(offer.sender)
const verified = await Routines.verifyDataPQ(actor, signature, offerBSON)
const verified = await Routines.verifyDataPQ(offer.sender, signature, offerBSON)

if(!verified){
throw new Error('DENY(hostRunner) - auth op signature is not valid')
}

if(this.discoverRemoteIdentity){ this.remoteIdentity = actor }
if(this.discoverRemoteIdentity){ this.remoteIdentity = offer.sender }

const authorized = await this.party.hostRunner.auth.isSocketConnectionAllowed(actor)
if(!authorized){
Expand All @@ -406,6 +406,7 @@ class PeerComms extends ISocketComms {
await this.stop()

debug('DENY - client not allowed - ', this.remoteIdentity)
throw new Error('DENY - client not allowed')
}
} else {
const actor = offer.sender
Expand All @@ -420,7 +421,7 @@ class PeerComms extends ISocketComms {
}
}

debug('clienr auth op offer -', offer)
debug('client auth op offer -', offer)
debug('ALLOW - allowing client - ', this.remoteIdentity)

this.aesStream = await AESStream.recoverStream(
Expand Down Expand Up @@ -457,10 +458,18 @@ class PeerComms extends ISocketComms {
return
}

debug('input type', typeof op.input.data, Object.keys(op.input.data))
debug('op.msg type', typeof op.msg, Object.keys(op.msg), Buffer.isBuffer(op.msg))

let bodyValue = Buffer.isBuffer(op.msg) ?
op.input.data :
//Routines.BSON.parseObject(new Routines.BSON.BaseParser( op.msg )) :
JSON.parse(op.msg.toString())

const req = HttpMocks.createRequest({
method: 'GET',
url: '/'+op.input.endpoint,
body: (op.input.data) ? JSON.parse(op.msg.toString()) : undefined
body: bodyValue
})

const res = HttpMocks.createResponse()
Expand All @@ -473,6 +482,9 @@ class PeerComms extends ISocketComms {

debug('route',route)

req.peer = this
req.source = 'PeerComms'

debug('call route', await route._events.route({
method: req.method,
pathname: req.url,
Expand Down
19 changes: 19 additions & 0 deletions src/config/json-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class JsonFileConfig extends IConfig {
this.path = this.basePath +'/config.json'
this.defaults = defaults || {}
this.content = Object.assign({}, this.defaults)
this.writing = false
this.started = false
}

async load(){
Expand All @@ -47,9 +49,16 @@ class JsonFileConfig extends IConfig {
}

async start () {

if(this.started){return}

await this.touchDir('')
await this.load()

fs.watchFile(this.path, this.handleFileChange.bind(this))
logger('started')

this.started = true
}

async clear () {
Expand Down Expand Up @@ -79,7 +88,9 @@ class JsonFileConfig extends IConfig {
}

async save(){
this.writing = true
fs.writeFileSync(this.path, JSON.stringify(this.content, null, 2))
this.writing = false
}

async touchDir (path) {
Expand All @@ -98,6 +109,14 @@ class JsonFileConfig extends IConfig {
})
})
}

async handleFileChange(current, previous){
if(this.writing){ return }

logger('config changed, reloading')

await this.load()
}
}

module.exports = JsonFileConfig
38 changes: 26 additions & 12 deletions src/party/peer/match-maker-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ const EventEmitter = require('eventemitter3')

const debug = require('debug')('dataparty.match-maker-client')


const dataparty_crypto = require('@dataparty/crypto')
const LokiParty = require('../local/loki-party')
const PeerParty = require('./peer-party')
const MemoryConfig = require('../../config/memory')
const RestComms = require('../../comms/rest-comms')
const WebsocketComms = require('../../comms/websocket-comms')

const PeerInvite = require('./peer-invite')

class MatchMakerClient extends EventEmitter {
constructor(identity, contacts, urlOrParty = 'https://postquantum.one/api/', wsUrlOrParty = 'wss://postquantum.one/ws'){
constructor(identity, contacts, urlOrParty = 'https://api.dataparty.xyz/api', wsUrlOrParty = 'wss://api.dataparty.xyz/ws', billingIdentity=null){

super()


this.contacts = contacts
this.sessionKey = null
this.identity = identity
this.wsParty = null
this.restParty = null
this.billingIdentity = null

if(typeof urlOrParty == 'string'){
this.restUrl = urlOrParty
Expand Down Expand Up @@ -71,7 +71,7 @@ class MatchMakerClient extends EventEmitter {
await this.restParty.start()

if(!this.restParty.comms){
this.restParty.comms = new Dataparty.Comms.RestComms({
this.restParty.comms = new RestComms({
party:this.restParty,
config: this.restParty.config
})
Expand All @@ -82,7 +82,7 @@ class MatchMakerClient extends EventEmitter {
await this.announcePublicKeys()
}

if(!this.wsParty){
if(!this.wsParty && this.wsUrl){
this.wsParty = new PeerParty({
comms: new WebsocketComms({
uri: this.wsUrl,
Expand Down Expand Up @@ -157,15 +157,23 @@ class MatchMakerClient extends EventEmitter {
debug('calling onInviteMsg')

await pending.onInviteMsg(msg.invite)

}
}

async announceBillingKey({stripeCheckoutSession}={}){
this.announcePublicKeys(true, {
stripe: stripeCheckoutSession
})
}

async announcePublicKeys(useBillingKeyAsActor=false, billingMethodDetails=null){

async announcePublicKeys(){
let currentActor = useBillingKeyAsActor == true ? this.billingIdentity : this.identity

const announceData = {
annoucement: {
//type: 'guest',//useBillingKeyAsActor ? 'billing_identity' : 'user_identity',
created: Date.now(),
expiry: Date.now() + 24*60*60*1000, //! Set session expiry to 24hr from now
sessionKey: {
Expand All @@ -174,9 +182,9 @@ class MatchMakerClient extends EventEmitter {
public: this.sessionKey.key.public
},
actorKey: {
type: this.identity.key.type,
hash: this.identity.key.hash,
public: this.identity.key.public
type: currentActor.key.type,
hash: currentActor.key.hash,
public: currentActor.key.public
}
},
trust: {
Expand All @@ -186,7 +194,7 @@ class MatchMakerClient extends EventEmitter {
}


const actorSigMsg = await this.identity.sign(announceData.annoucement, true)
const actorSigMsg = await currentActor.sign(announceData.annoucement, true)
const sessionSigMsg = await this.sessionKey.sign(announceData.annoucement, true)

debug('actorSigMsg', actorSigMsg)
Expand All @@ -197,11 +205,17 @@ class MatchMakerClient extends EventEmitter {

debug('announcePublicKeys', announceData)

const announceResult = await this.restParty.comms.call('key/announce', announceData, {
let callPath = useBillingKeyAsActor ? 'billing/key/announce' : 'key/announce'

const announceResult = await this.restParty.comms.call(callPath, announceData, {
expectClearTextReply: false,
sendClearTextRequest: false,
useSessions: false
})

if(announceResult.done != true){
throw new Error('annoucement request failed - '+callPath)
}
}


Expand Down
7 changes: 6 additions & 1 deletion src/service/endpoint-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EndpointContext {
* @param {Debug} options.debug Debug constructor (defaults to npm:Debug)
* @param {boolean} options.sendFullErrors Enables sending full stack traces to client (defaults to false)
*/
constructor({party, endpoint, req, res, input, debug=Debug, sendFullErrors=false}){
constructor({party, endpoint, runner, req, res, input, debug=Debug, sendFullErrors=false}){

/**
* @member module:Service.EndpointContext.debug
Expand All @@ -27,6 +27,11 @@ class EndpointContext {
*/
this.endpoint = endpoint

/**
* @member module:Service.EndpointContext.runner
*/
this.runner = runner

/**
* @member module:Service.EndpointContext.MiddlewareConfig
*/
Expand Down
3 changes: 2 additions & 1 deletion src/service/endpoints/service-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = class ServiceVersion extends IEndpoint {
name: Joi.string(),
branch: Joi.string(),
version: Joi.string(),
githash: Joi.string()
githash: Joi.string(),
owner: Joi.string(),
})
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/service/iservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = class IService {
* @param {*} build
*/
constructor({
name, version, githash='', branch=''
name, version, githash='', branch='', owner=null
}, build){

this.constructors = {
Expand Down Expand Up @@ -48,11 +48,13 @@ module.exports = class IService {
},
tasks: {},
topics: {},
auth: null
auth: null,
files: [],
files_root: null
}

this.compiled = {
package: { name, version, githash, branch },
package: {owner, name, version, githash, branch },
schemas: {
IndexSettings: {},
JSONSchema: [],
Expand All @@ -70,7 +72,9 @@ module.exports = class IService {
},
tasks: {},
topics: {},
auth: {}
auth: {},
files: {},
signatures: []
}

this.compileSettings = {
Expand Down
6 changes: 5 additions & 1 deletion src/service/middleware/post/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ module.exports = class Encrypt extends IMiddleware {
static async run(ctx, {Config}){

if (!Config){ return }


if(ctx.req.source && ctx.req.source == 'PeerComms'){
ctx.setOutput(ctx.output)
return
}

const senderStr = JSON.stringify({key: ctx.senderKey})

Expand Down
10 changes: 9 additions & 1 deletion src/service/middleware/pre/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ module.exports = class Decrypt extends IMiddleware {

if (!Config){ return }



if(!context.input || !context.input.enc){
throw new Error('insecure message')

if(!context.req.source || context.req.source != 'PeerComms'){
throw new Error('insecure message -' + context.req.source)
}

context.setInput( context.input )
return
}

context.debug('input', context.input, typeof context.input)
Expand Down
Loading