diff --git a/www/common/outer/async-store.js b/www/common/outer/async-store.js index abc3843da..0487f6a34 100644 --- a/www/common/outer/async-store.js +++ b/www/common/outer/async-store.js @@ -440,6 +440,7 @@ define([ if (e) { return void cb({error: e}); } store.rpc = call; + store.onRpcReadyEvt.fire(); Store.getPinLimit(null, null, function (obj) { if (obj.error) { console.error(obj.error); } @@ -1598,6 +1599,50 @@ define([ }); }; + Store.onRejected = function (allowed, _cb) { + var cb = Util.once(Util.mkAsync(_cb)); + + // There is an allow list: check if we can authenticate + if (!Array.isArray(allowed)) { return void cb('EINVAL'); } + if (!store.loggedIn || !store.proxy.edPublic) { return void cb('EFORBIDDEN'); } + + var teamModule = store.modules['team']; + var teams = (teamModule && teamModule.getTeams()) || []; + var _store; + + if (allowed.indexOf(store.proxy.edPublic) !== -1) { + // We are allowed: use our own rpc + _store = store; + } else if (teams.some(function (teamId) { + // We're not allowed: check our teams + var ed = Util.find(store, ['proxy', 'teams', teamId, 'keys', 'drive', 'edPublic']); + if (allowed.indexOf(ed) === -1) { return false; } + // This team is allowed: use its rpc + var t = teamModule.getTeam(teamId); + _store = t; + return true; + })) {} + + var auth = function () { + var rpc = _store.rpc; + if (!rpc) { return void cb('EFORBIDDEN'); } + rpc.send('COOKIE', '', function (err) { + cb(err); + }); + } + + // Wait for the RPC we need to be ready and then tyr to authenticate + if (_store.onRpcReadyEvt) { + _store.onRpcReadyEvt.reg(function () { + auth(); + }); + return; + } + + // Fall back to the old system in case onRpcReadyEvt doesn't exist (shouldn't happen) + auth(); + }; + Store.joinPad = function (clientId, data) { if (data.versionHash) { return void getVersionHash(clientId, data); @@ -1701,37 +1746,7 @@ define([ }, onError: onError, onChannelError: onError, - onRejected: function (allowed, _cb) { - var cb = Util.once(Util.mkAsync(_cb)); - - // There is an allow list: check if we can authenticate - if (!Array.isArray(allowed)) { return void cb('EINVAL'); } - if (!store.loggedIn || !store.proxy.edPublic) { return void cb('EFORBIDDEN'); } - - onReadyEvt.reg(function () { - var rpc; - var teamModule = store.modules['team']; - var teams = (teamModule && teamModule.getTeams()) || []; - - if (allowed.indexOf(store.proxy.edPublic) !== -1) { - // We are allowed: use our own rpc - rpc = store.rpc; - } else if (teams.some(function (teamId) { - // We're not allowed: check our teams - var ed = Util.find(store, ['proxy', 'teams', teamId, 'keys', 'drive', 'edPublic']); - if (allowed.indexOf(ed) === -1) { return false; } - // This team is allowed: use its rpc - var t = teamModule.getTeam(teamId); - rpc = t.rpc; - return true; - })) {} - - if (!rpc) { return void cb('EFORBIDDEN'); } - rpc.send('COOKIE', '', function (err) { - cb(err); - }); - }); - }, + onRejected: Store.onRejected, onConnectionChange: function (info) { if (!info.state) { channel.bcast("PAD_DISCONNECT"); @@ -2589,7 +2604,7 @@ define([ progress: 100*obj.progress/obj.max }; postMessage(clientId, 'LOADING_DRIVE', data); - }); + }, true); }).nThen(function (waitFor) { loadUniversal(Team, 'team', waitFor, clientId); }).nThen(function () { @@ -2782,6 +2797,7 @@ define([ var rt = window.rt = Listmap.create(listmapConfig); store.driveSecret = secret; store.proxy = rt.proxy; + store.onRpcReadyEvt = Util.mkEvent(true); store.loggedIn = typeof(data.userHash) !== "undefined"; var returned = {}; diff --git a/www/common/outer/sharedfolder.js b/www/common/outer/sharedfolder.js index 8ade0d250..b30cfead8 100644 --- a/www/common/outer/sharedfolder.js +++ b/www/common/outer/sharedfolder.js @@ -107,15 +107,12 @@ define([ // If we try to load an existing shared folder (isNew === false) but this folder // doesn't exist in the database, abort and cb nThen(function (waitFor) { - // XXX use a config.cache flag in the new branches - // If we don't have a network yet and we're pulling our own SF (no team id) - // Make sure we have a cache - if (!config.store.id && !config.store.network) { + // If we're in onCacheReady, make sure we have a cache for this shared folder + if (config.cache) { Cache.getChannelCache(secret.channel, waitFor(function (err, res) { if (err === "EINVAL") { // Cache not found waitFor.abort(); store.manager.restrictedProxy(id, secret.channel); - // XXX unrestrict when we connect? return void cb(null); } })); @@ -178,16 +175,6 @@ define([ readOnly: !Boolean(secondaryKey) }; - // If there is an allow list and we're offline, try again when we're synced - var onRejected = function (allowed, _cb) { - var cb = Util.once(Util.mkAsync(_cb)); - if (store.offline && config.Store) { - config.Store.onReadyEvt.reg(cb); - return; - } - cb('ERESTRICTED'); - }; - var owners = data.owners; var listmapConfig = { data: {}, @@ -204,7 +191,7 @@ define([ validateKey: secret.keys.validateKey || undefined, owners: owners }, - onRejected: onRejected // XXX not working + onRejected: config.Store && config.Store.onRejected }; var rt = sf.rt = Listmap.create(listmapConfig); rt.proxy.on('cacheready', function () { @@ -368,7 +355,7 @@ define([ - userObject: userObject associated to the main drive - handler: a function (sfid, rt) called for each shared folder loaded */ - SF.loadSharedFolders = function (Store, network, store, userObject, waitFor, progress) { + SF.loadSharedFolders = function (Store, network, store, userObject, waitFor, progress, cache) { var shared = Util.find(store.proxy, ['drive', UserObject.SHARED_FOLDERS]) || {}; var steps = Object.keys(shared).length; var i = 1; @@ -381,6 +368,7 @@ define([ network: network, store: store, Store: Store, + cache: cache, isNewChannel: Store.isNewChannel }, id, sf, waitFor(function () { progress({ diff --git a/www/common/outer/team.js b/www/common/outer/team.js index 63ce333e1..c6e275e6d 100644 --- a/www/common/outer/team.js +++ b/www/common/outer/team.js @@ -184,6 +184,7 @@ define([ Pinpad.create(ctx.store.network, data, function (e, call) { if (e) { return void cb(e); } team.rpc = call; + team.onRpcReadyEvt.fire(); cb(); }); }); @@ -202,6 +203,7 @@ define([ handleSharedFolder: function (sfId, rt) { handleSharedFolder(ctx, id, sfId, rt); }, sharedFolders: {}, // equivalent of store.sharedFolders in async-store roster: roster, + onRpcReadyEvt: Util.mkEvent(true), offline: true }; ctx.cache[id] = team; @@ -334,7 +336,7 @@ define([ ctx.updateProgress({ progress: ctx.progress }); - }); + }, true); }).nThen(function () { cb(); });