123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- const SEA = require('./sea')
- const Gun = SEA.Gun;
-
-
- Gun.on('opt', function(at){
- if(!at.sea){
- at.sea = {own: {}};
- at.on('in', security, at);
- at.on('out', signature, at);
- at.on('node', each, at);
- }
- this.to.next(at);
- });
-
-
-
-
-
-
-
-
-
-
-
-
-
- function each(msg){
-
-
- var to = this.to, vertex = (msg.$._).put, c = 0, d;
- Gun.node.is(msg.put, function(val, key, node){ c++;
-
- SEA.verify(val, false, function(data){ c--;
- var tmp = data;
- data = SEA.opt.unpack(data, key, node);
- node[key] = val = data;
- if(d && !c && (c = -1)){ to.next(msg) }
- });
- });
- d = true;
- if(d && !c){ to.next(msg) }
- return;
- }
-
- function signature(msg){
- if(msg.user){
- return this.to.next(msg);
- }
- var ctx = this.as;
- msg.user = ctx.user;
- security.call(this, msg);
- }
-
-
-
- function security(msg){
- var at = this.as, sea = at.sea, to = this.to;
- if(msg.get){
-
- var soul = msg.get['#'];
- if(soul){
- if(typeof soul !== 'string'){ return to.next(msg) }
- if('alias' === soul){
- return to.next(msg);
- } else
- if('~@' === soul.slice(0,2)){
- return to.next(msg);
- } else {
- return to.next(msg);
- }
- }
- }
- if(msg.put){
-
- var check = {}, each = {}, u;
- each.node = function(node, soul){
- if(Gun.obj.empty(node, '_')){ return check['node'+soul] = 0 }
- Gun.obj.map(node, each.way, {soul: soul, node: node});
- };
- each.way = function(val, key){
- var soul = this.soul, node = this.node, tmp;
- if('_' === key){ return }
- if('~@' === soul){
- each.alias(val, key, node, soul); return;
- }
- if('~@' === soul.slice(0,2)){
- each.pubs(val, key, node, soul); return;
- }
- if('~' === soul.slice(0,1) && 2 === (tmp = soul.slice(1)).split('.').length){
- each.pub(val, key, node, soul, tmp, msg.user); return;
- }
- each.any(val, key, node, soul, msg.user); return;
- return each.end({err: "No other data allowed!"});
- };
- each.alias = function(val, key, node, soul){
- if(!val){ return each.end({err: "Data must exist!"}) }
- if('~@'+key === Gun.val.link.is(val)){ return check['alias'+key] = 0 }
- each.end({err: "Mismatching alias."});
- };
- each.pubs = function(val, key, node, soul){
- if(!val){ return each.end({err: "Alias must exist!"}) }
- if(key === Gun.val.link.is(val)){ return check['pubs'+soul+key] = 0 }
- each.end({err: "Alias must match!"});
- };
- each.pub = function(val, key, node, soul, pub, user){
- if('pub' === key){
- if(val === pub){ return (check['pub'+soul+key] = 0) }
- return each.end({err: "Account must match!"});
- }
- check['user'+soul+key] = 1;
- if(user && user.is && pub === user.is.pub){
-
- SEA.sign([soul, key, val, Gun.state.is(node, key)], (user._).sea, function(data){ var rel;
- if(u === data){ return each.end({err: SEA.err || 'Pub signature fail.'}) }
- if(rel = Gun.val.link.is(val)){
- (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
- }
- node[key] = data;
- check['user'+soul+key] = 0;
- each.end({ok: 1});
- });
-
- return;
- }
- SEA.verify(val, pub, function(data){ var rel, tmp;
- data = SEA.opt.unpack(data, key, node);
- if(u === data){
- return each.end({err: "Unverified data."});
- }
- if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){
- (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
- }
- check['user'+soul+key] = 0;
- each.end({ok: 1});
- });
- };
- function relpub(s){
- if(!s){ return }
- s = s.split('~');
- if(!s || !(s = s[1])){ return }
- s = s.split('.');
- if(!s || 2 > s.length){ return }
- s = s.slice(0,2).join('.');
- return s;
- }
- each.any = function(val, key, node, soul, user){ var tmp, pub;
- if(!user || !user.is){
- if(tmp = relpub(soul)){
- check['any'+soul+key] = 1;
- SEA.verify(val, pub = tmp, function(data){ var rel;
- data = SEA.opt.unpack(data, key, node);
- if(u === data){ return each.end({err: "Mismatched owner on '" + key + "'."}) }
- if((rel = Gun.val.link.is(data)) && pub === relpub(rel)){
- (at.sea.own[rel] = at.sea.own[rel] || {})[pub] = true;
- }
- check['any'+soul+key] = 0;
- each.end({ok: 1});
- });
- return;
- }
- check['any'+soul+key] = 1;
- at.on('secure', function(msg){ this.off();
- check['any'+soul+key] = 0;
- if(at.opt.secure){ msg = null }
- each.end(msg || {err: "Data cannot be modified."});
- }).on.on('secure', msg);
-
- return;
- }
- if(!(tmp = relpub(soul))){
- if(at.opt.secure){
- each.end({err: "Soul is missing public key at '" + key + "'."});
- return;
- }
- if(val && val.slice && 'SEA{' === (val).slice(0,4)){
- check['any'+soul+key] = 0;
- each.end({ok: 1});
- return;
- }
-
-
-
-
- check['any'+soul+key] = 0;
- each.end({ok: 1});
-
- return;
- }
- if(!msg.I || (pub = tmp) !== (user.is||noop).pub){
- each.any(val, key, node, soul);
- return;
- }
-
- check['any'+soul+key] = 1;
- SEA.sign([soul, key, val, Gun.state.is(node, key)], (user._).sea, function(data){
- if(u === data){ return each.end({err: 'My signature fail.'}) }
- node[key] = data;
- check['any'+soul+key] = 0;
- each.end({ok: 1});
- });
- }
- each.end = function(ctx){
- if(each.err){ return }
- if((each.err = ctx.err) || ctx.no){
- console.log('NO!', each.err, msg.put);
- return;
- }
- if(!each.end.ed){ return }
- if(Gun.obj.map(check, function(no){
- if(no){ return true }
- })){ return }
- to.next(msg);
- };
- Gun.obj.map(msg.put, each.node);
- each.end({end: each.end.ed = true});
- return;
- }
- to.next(msg);
- }
- SEA.opt.unpack = function(data, key, node){
- if(u === data){ return }
- var tmp = data, soul = Gun.node.soul(node), s = Gun.state.is(node, key);
- if(tmp && 4 === tmp.length && soul === tmp[0] && key === tmp[1] && s === tmp[3]){
- return tmp[2];
- }
- if(s < SEA.opt.shuffle_attack){
- return data;
- }
- }
- SEA.opt.shuffle_attack = 1546329600000;
- var noop = {}, u;
-
|