From a713bb0ae78fa572fadf7dcc7ddd4d35591823f8 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Wed, 22 Jun 2016 15:05:58 -0700 Subject: [PATCH 1/2] minimum temporary auth for server, clients still independent from auth though --- examples/express-auth.js | 30 ++++++++++++++++++++++++++++++ lib/wsp.js | 24 +++++++++++++++++------- 2 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 examples/express-auth.js diff --git a/examples/express-auth.js b/examples/express-auth.js new file mode 100644 index 00000000..c48061ac --- /dev/null +++ b/examples/express-auth.js @@ -0,0 +1,30 @@ +console.log("If modules not found, run `npm install` in /example folder!"); // git subtree push -P examples heroku master // OR // git subtree split -P examples master && git push heroku [['HASH']]:master --force +var port = process.env.OPENSHIFT_NODEJS_PORT || process.env.VCAP_APP_PORT || process.env.PORT || process.argv[2] || 80; + +var express = require('express'); +var app = express(); + +var Gun = require('gun'); +var gun = Gun({ + file: 'data.json', + s3: { + key: '', // AWS Access Key + secret: '', // AWS Secret Token + bucket: '' // The bucket you want to save into + } +}); + +gun.wsp(app/*, function(req, res, next){ + console.log("auth!", req, req.body['#']); + if('get' === req.method){ + if('example/todo/data' === req.body['#']){ + next(req, res); + } + } + if('put' === req.method){ + res({body: {err: "Permission denied!"}}); + } +}*/); +app.use(express.static(__dirname)).listen(port); + +console.log('Server started on port ' + port + ' with /gun'); \ No newline at end of file diff --git a/lib/wsp.js b/lib/wsp.js index 46343fda..ead5d8e1 100644 --- a/lib/wsp.js +++ b/lib/wsp.js @@ -1,5 +1,5 @@ ;(function(wsp){ - var Gun = require('../gun') + var Gun = require('../gun') , ws = require('ws').Server , http = require('./http') , url = require('url'); @@ -23,7 +23,8 @@ }); gun.__.opt.ws.port = gun.__.opt.ws.port || opt.ws.port || port || 80; } - var wsp = gun.wsp = gun.wsp || function(server){ + var wsp = gun.wsp = gun.wsp || function(server, auth){ + gun.wsp.auth = auth; if(!server){ return gun } if(Gun.fns.is(server.address)){ if(server.address()){ @@ -112,15 +113,24 @@ // all streams, technically PATCH but implemented as PUT or POST, are forwarded to other trusted peers // except for the ones that are listed in the message as having already been sending to. // all states, implemented with GET, are replied to the source that asked for it. - function tran(req, res){ - if(!req || !res || !req.body || !req.headers || !req.headers.id){ return } - if(gun.wsp.msg(req.headers.id)){ return } - req.method = req.body? 'put' : 'get'; + function flow(req, res){ gun.wsp.on('network').emit(Gun.obj.copy(req)); if(req.headers.rid){ return } // no need to process. if(Gun.is.lex(req.body)){ return tran.get(req, res) } else { return tran.put(req, res) } - cb({body: {hello: 'world'}}); + } + function tran(req, res){ + if(!req || !res || !req.body || !req.headers || !req.headers.id){ return } + if(gun.wsp.msg(req.headers.id)){ return } + req.method = (req.body && !Gun.is.lex(req.body))? 'put' : 'get'; + if(gun.wsp.auth){ return gun.wsp.auth(req, function(reply){ + if(!reply.headers){ reply.headers = {} } + if(!reply.headers['Content-Type']){ reply.headers['Content-Type'] = tran.json } + if(!reply.rid){ reply.headers.rid = req.headers.id } + if(!reply.id){ reply.headers.id = gun.wsp.msg() } + res(reply); + }, flow) } + else { return flow(req, res) } } tran.get = function(req, cb){ var key = req.url.key From a83ff0c114dbb41a3b1918aced49653b08281cae Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Wed, 22 Jun 2016 16:56:34 -0700 Subject: [PATCH 2/2] fix driver to actually connect to other servers --- gun.js | 33 ++++++++++++++++++++------------- package.json | 2 +- test/common.js | 44 +++++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/gun.js b/gun.js index 5fc45ec2..98c3aa85 100644 --- a/gun.js +++ b/gun.js @@ -1173,7 +1173,8 @@ var soul = lex[Gun._.soul]; if(!soul){ return } cb = cb || function(){}; - (opt.headers = Gun.obj.copy(tab.headers)).id = tab.msg(); + var ropt = {}; + (ropt.headers = Gun.obj.copy(tab.headers)).id = tab.msg(); (function local(soul, cb){ tab.store.get(tab.prefix + soul, function(err, data){ if(!data){ return } // let the peers handle no data. @@ -1184,11 +1185,11 @@ }); }(soul, cb)); if(!(cb.local = opt.local)){ - tab.request.s[opt.headers.id] = tab.error(cb, "Error: Get failed!", function(reply){ + tab.request.s[ropt.headers.id] = tab.error(cb, "Error: Get failed!", function(reply){ setTimeout(function(){ tab.put(Gun.is.graph.ify(reply.body), function(){}, {local: true, peers: {}}) },1); // and flush the in memory nodes of this graph to localStorage after we've had a chance to union on it. }); Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){ var p = {}; - tab.request(url, lex, tab.request.s[opt.headers.id], opt); + tab.request(url, lex, tab.request.s[ropt.headers.id], ropt); cb.peers = true; }); var node = gun.__.graph[soul]; @@ -1198,18 +1199,18 @@ } tab.peers(cb); } tab.put = tab.put || function(graph, cb, opt){ - //console.log("SAVE", graph); cb = cb || function(){}; opt = opt || {}; - (opt.headers = Gun.obj.copy(tab.headers)).id = tab.msg(); + var ropt = {}; + (ropt.headers = Gun.obj.copy(tab.headers)).id = tab.msg(); Gun.is.graph(graph, function(node, soul){ if(!gun.__.graph[soul]){ return } tab.store.put(tab.prefix + soul, gun.__.graph[soul], function(err){if(err){ cb({err: err}) }}); }); if(!(cb.local = opt.local)){ - tab.request.s[opt.headers.id] = tab.error(cb, "Error: Put failed!"); + tab.request.s[ropt.headers.id] = tab.error(cb, "Error: Put failed!"); Gun.obj.map(opt.peers || gun.__.opt.peers, function(peer, url){ - tab.request(url, graph, tab.request.s[opt.headers.id], opt); + tab.request(url, graph, tab.request.s[ropt.headers.id], ropt); cb.peers = true; }); } tab.peers(cb); @@ -1287,16 +1288,21 @@ gun.__.opt.wire.get = gun.__.opt.wire.get || tab.get; gun.__.opt.wire.put = gun.__.opt.wire.put || tab.put; gun.__.opt.wire.key = gun.__.opt.wire.key || tab.key; + + Tab.request = request; + Gun.Tab = Tab; }); var request = (function(){ function r(base, body, cb, opt){ - opt = opt || (base.length? {base: base} : base); - opt.base = opt.base || base; - opt.body = opt.body || body; + var o = base.length? {base: base} : {}; + o.base = opt.base || base; + o.body = opt.body || body; + o.headers = opt.headers; + o.url = opt.url; cb = cb || function(){}; - if(!opt.base){ return } - r.transport(opt, cb); + if(!o.base){ return } + r.transport(o, cb); } r.createServer = function(fn){ r.createServer.s.push(fn) } r.createServer.ing = function(req, cb){ @@ -1328,7 +1334,8 @@ return true; } if(ws === false){ return } - ws = r.ws.peers[opt.base] = new WS(opt.base.replace('http','ws')); + try{ws = r.ws.peers[opt.base] = new WS(opt.base.replace('http','ws')); + }catch(e){} ws.onopen = function(o){ r.back = 2; r.ws(opt, cb) }; ws.onclose = window.onbeforeunload = function(c){ if(!c){ return } diff --git a/package.json b/package.json index 57caac27..43dcc6fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gun", - "version": "0.3.92", + "version": "0.3.93", "description": "Graph engine", "main": "index.js", "scripts": { diff --git a/test/common.js b/test/common.js index c934c4ba..ef9ae2e7 100644 --- a/test/common.js +++ b/test/common.js @@ -3799,7 +3799,7 @@ describe('Gun', function(){ }); it("get context", function(done){ // TODO: HUH?????? This was randomly causing errors? - var gun = Gun(); + var gun = Gun(); var ref = gun.get('ctx/lol').get('ctx/foo').put({hello: 'world'}); gun.get('ctx/lol').val(function(implicit){ done.fail = true; @@ -3975,29 +3975,31 @@ describe('Gun', function(){ it("Don't put on parents", function(done){ // TODO: ADD TO 0.5 BRANCH! // Another Stefdv find. var test = gun.get('test'); - test.path('try.this.at.lvl4').put({msg:'hoi'}) - test.val(function(node,b){ - delete node._; - expect(Gun.obj.empty(node, 'try')).to.be.ok(); - node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.try)]); + test.path('try.this.at.lvl4').put({msg:'hoi'}); + setTimeout(function(){ // TODO: Is this cheating?? + test.val(function(node,b){ + delete node._; + expect(Gun.obj.empty(node, 'try')).to.be.ok(); + node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.try)]); - delete node._; - expect(Gun.obj.empty(node, 'this')).to.be.ok(); - node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.this)]); + delete node._; + expect(Gun.obj.empty(node, 'this')).to.be.ok(); + node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.this)]); + + delete node._; + expect(Gun.obj.empty(node, 'at')).to.be.ok(); + node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.at)]); - delete node._; - expect(Gun.obj.empty(node, 'at')).to.be.ok(); - node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.at)]); + delete node._; + expect(Gun.obj.empty(node, 'lvl4')).to.be.ok(); + node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.lvl4)]); - delete node._; - expect(Gun.obj.empty(node, 'lvl4')).to.be.ok(); - node = Gun.obj.copy(gun.__.graph[Gun.is.rel(node.lvl4)]); - - delete node._; - expect(Gun.obj.empty(node, 'msg')).to.be.ok(); - expect(node.msg).to.be('hoi'); - done(); - }); + delete node._; + expect(Gun.obj.empty(node, 'msg')).to.be.ok(); + expect(node.msg).to.be('hoi'); + done(); + }); + },100); }); });