From 8a0e0621fb9d78a5b5d31375013e1e94f7f0a494 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 27 Nov 2019 17:26:21 +0100 Subject: [PATCH 1/5] Fast auth --- www/auth/main.js | 85 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/www/auth/main.js b/www/auth/main.js index 7930e96e0..afe029bc3 100644 --- a/www/auth/main.js +++ b/www/auth/main.js @@ -1,12 +1,16 @@ define([ 'jquery', - '/common/cryptpad-common.js', + '/common/cryptget.js', + '/common/pinpad.js', '/common/common-constants.js', '/common/outer/local-store.js', + '/common/outer/login-block.js', + '/common/outer/network-config.js', '/common/test.js', '/bower_components/nthen/index.js', + '/bower_components/netflux-websocket/netflux-client.js', '/bower_components/tweetnacl/nacl-fast.min.js' -], function ($, Cryptpad, Constants, LocalStore, Test, nThen) { +], function ($, Crypt, Pinpad, Constants, LocalStore, Block, NetConfig, Test, nThen, Netflux) { var Nacl = window.nacl; var signMsg = function (msg, privKey) { @@ -27,9 +31,55 @@ define([ sessionStorage[Constants.userHashKey]; var proxy; + var rpc; + var network; + var rpcError; + + var loadProxy = function (hash) { + nThen(function (waitFor) { + Crypt.get(hash, waitFor(function (err, val) { + if (err) { + waitFor.abort(); + console.error(err); + } + try { + var parsed = JSON.parse(val); + proxy = parsed; + } catch (e) { + console.log("Can't parse user drive", e); + } + }); + }).nThen(function (waitFor) { + var wsUrl = NetConfig.getWebsocketURL(); + var w = waitFor(); + Netflux.connect(wsUrl).then(function (network) { + network = _network; + w(); + }, function (err) { + rpcError = err; + console.error(err); + waitFor.abort(); + }); + }).nThen(function (waitFor) { + Pinpad.create(network, proxy, waitFor(function (e, call) { + if (e) { + rpcError = e; + return void waitFor.abort(); + } + rpc = call; + })); + }).nThen(function () { + console.log('IFRAME READY'); + Test(function () { + // This is only here to maybe trigger an error. + window.drive = proxy['drive']; + Test.passed(); + }); + }); + }; var whenReady = function (cb) { - if (proxy) { return void cb(); } + if (proxy && (rpc || rpcError)) { return void cb(); } console.log('CryptPad not ready...'); setTimeout(function () { whenReady(cb); @@ -50,6 +100,8 @@ define([ ret.error = "UNAUTH_DOMAIN"; } else if (!LocalStore.isLoggedIn()) { ret.error = "NOT_LOGGED_IN"; + } else if ('LOGIN') { + // XXX Display login modal.... } else { return void whenReady(function () { var sig = signMsg(data.data, proxy.edPrivate); @@ -63,7 +115,14 @@ define([ } } else if (data.cmd === 'UPDATE_LIMIT') { return void whenReady(function () { - Cryptpad.updatePinLimit(function (e, limit, plan, note) { + if (rpcError) { + // XXX + // Tell the user on accounts that there was an issue and they need to wait maximum 24h or contact an admin + } + rpc.updatePinLimits(function (e, limit, plan, note) { + if (e) { + // XXX same as above + } ret.res = [limit, plan, note]; srcWindow.postMessage(JSON.stringify(ret), domain); }); @@ -74,18 +133,8 @@ define([ srcWindow.postMessage(JSON.stringify(ret), domain); }); - nThen(function (waitFor) { - Cryptpad.ready(waitFor()); - }).nThen(function (waitFor) { - Cryptpad.getUserObject(null, waitFor(function (obj) { - proxy = obj; - })); - }).nThen(function () { - console.log('IFRAME READY'); - Test(function () { - // This is only here to maybe trigger an error. - window.drive = proxy['drive']; - Test.passed(); - }); - }); + var userHash = LocalStore.getUserHash(); + if (userHash) { + loadProxy(userHash); + } }); From 03701f02e05445c431d604cc39591bcb5f4dbfb7 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 29 Nov 2019 10:46:34 +0100 Subject: [PATCH 2/5] Remove debug logs --- www/auth/main.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/www/auth/main.js b/www/auth/main.js index afe029bc3..d019c1a8c 100644 --- a/www/auth/main.js +++ b/www/auth/main.js @@ -41,6 +41,7 @@ define([ if (err) { waitFor.abort(); console.error(err); + return; } try { var parsed = JSON.parse(val); @@ -48,11 +49,11 @@ define([ } catch (e) { console.log("Can't parse user drive", e); } - }); + })); }).nThen(function (waitFor) { var wsUrl = NetConfig.getWebsocketURL(); var w = waitFor(); - Netflux.connect(wsUrl).then(function (network) { + Netflux.connect(wsUrl).then(function (_network) { network = _network; w(); }, function (err) { @@ -69,7 +70,6 @@ define([ rpc = call; })); }).nThen(function () { - console.log('IFRAME READY'); Test(function () { // This is only here to maybe trigger an error. window.drive = proxy['drive']; @@ -95,13 +95,15 @@ define([ console.log('CP receiving', data); if (data.cmd === 'PING') { ret.res = 'PONG'; + } else if (data.cmd === 'LOGIN') { + UI.alert('okok'); + return; + // XXX Display login modal.... } else if (data.cmd === 'SIGN') { if (!AUTHORIZED_DOMAINS.filter(function (x) { return x.test(domain); }).length) { ret.error = "UNAUTH_DOMAIN"; } else if (!LocalStore.isLoggedIn()) { ret.error = "NOT_LOGGED_IN"; - } else if ('LOGIN') { - // XXX Display login modal.... } else { return void whenReady(function () { var sig = signMsg(data.data, proxy.edPrivate); From 198786ed908c6a0ce11f992cb565add767cc93e4 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 3 Dec 2019 10:37:47 +0100 Subject: [PATCH 3/5] Login from accounts --- www/auth/main.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/www/auth/main.js b/www/auth/main.js index d019c1a8c..878393012 100644 --- a/www/auth/main.js +++ b/www/auth/main.js @@ -6,11 +6,12 @@ define([ '/common/outer/local-store.js', '/common/outer/login-block.js', '/common/outer/network-config.js', + '/customize/login.js', '/common/test.js', '/bower_components/nthen/index.js', '/bower_components/netflux-websocket/netflux-client.js', '/bower_components/tweetnacl/nacl-fast.min.js' -], function ($, Crypt, Pinpad, Constants, LocalStore, Block, NetConfig, Test, nThen, Netflux) { +], function ($, Crypt, Pinpad, Constants, LocalStore, Block, NetConfig, Login, Test, nThen, Netflux) { var Nacl = window.nacl; var signMsg = function (msg, privKey) { @@ -96,9 +97,16 @@ define([ if (data.cmd === 'PING') { ret.res = 'PONG'; } else if (data.cmd === 'LOGIN') { - UI.alert('okok'); + Login.loginOrRegister(data.data.name, data.data.password, false, false, function (err, res) { + if (err) { + ret.error = 'LOGIN_ERROR' + srcWindow.postMessage(JSON.stringify(ret), domain); + return; + } + loadProxy(LocalStore.getUserHash()); + srcWindow.postMessage(JSON.stringify(ret), domain); + }); return; - // XXX Display login modal.... } else if (data.cmd === 'SIGN') { if (!AUTHORIZED_DOMAINS.filter(function (x) { return x.test(domain); }).length) { ret.error = "UNAUTH_DOMAIN"; From ba74fc8d4fb8b5016e69b58db843c38512d25d1c Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 4 Dec 2019 10:49:51 +0100 Subject: [PATCH 4/5] Send warning to accounts when we can't update pin limit --- www/auth/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/auth/main.js b/www/auth/main.js index 878393012..68eded8fe 100644 --- a/www/auth/main.js +++ b/www/auth/main.js @@ -126,12 +126,12 @@ define([ } else if (data.cmd === 'UPDATE_LIMIT') { return void whenReady(function () { if (rpcError) { - // XXX // Tell the user on accounts that there was an issue and they need to wait maximum 24h or contact an admin + ret.warning = true; } rpc.updatePinLimits(function (e, limit, plan, note) { if (e) { - // XXX same as above + ret.warning = true; } ret.res = [limit, plan, note]; srcWindow.postMessage(JSON.stringify(ret), domain); From 187eb4afde388ea473d5115f27bc83157edb5dc4 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 4 Dec 2019 11:13:05 +0100 Subject: [PATCH 5/5] Load only one network to get history keeper ID from cryptget --- www/auth/main.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/www/auth/main.js b/www/auth/main.js index 68eded8fe..1b3667377 100644 --- a/www/auth/main.js +++ b/www/auth/main.js @@ -38,6 +38,16 @@ define([ var loadProxy = function (hash) { nThen(function (waitFor) { + var wsUrl = NetConfig.getWebsocketURL(); + var w = waitFor(); + Netflux.connect(wsUrl).then(function (_network) { + network = _network; + w(); + }, function (err) { + rpcError = err; + console.error(err); + }); + }).nThen(function (waitFor) { Crypt.get(hash, waitFor(function (err, val) { if (err) { waitFor.abort(); @@ -50,19 +60,11 @@ define([ } catch (e) { console.log("Can't parse user drive", e); } - })); - }).nThen(function (waitFor) { - var wsUrl = NetConfig.getWebsocketURL(); - var w = waitFor(); - Netflux.connect(wsUrl).then(function (_network) { - network = _network; - w(); - }, function (err) { - rpcError = err; - console.error(err); - waitFor.abort(); + }), { + network: network }); }).nThen(function (waitFor) { + if (!network) { return void waitFor.abort(); } Pinpad.create(network, proxy, waitFor(function (e, call) { if (e) { rpcError = e; @@ -97,9 +99,9 @@ define([ if (data.cmd === 'PING') { ret.res = 'PONG'; } else if (data.cmd === 'LOGIN') { - Login.loginOrRegister(data.data.name, data.data.password, false, false, function (err, res) { + Login.loginOrRegister(data.data.name, data.data.password, false, false, function (err) { if (err) { - ret.error = 'LOGIN_ERROR' + ret.error = 'LOGIN_ERROR'; srcWindow.postMessage(JSON.stringify(ret), domain); return; } @@ -128,6 +130,8 @@ define([ if (rpcError) { // Tell the user on accounts that there was an issue and they need to wait maximum 24h or contact an admin ret.warning = true; + srcWindow.postMessage(JSON.stringify(ret), domain); + return; } rpc.updatePinLimits(function (e, limit, plan, note) { if (e) {