From 1682d23c1c055d5011e8da49a352f1cdcccc2bec Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 21 Dec 2016 18:06:05 +0100 Subject: [PATCH 1/4] remove old user prototype --- www/common/cryptpad-common.js | 88 +---------------------------------- 1 file changed, 2 insertions(+), 86 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index 09f44d3d9..f02e1747f 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -6,12 +6,10 @@ define([ '/bower_components/alertifyjs/dist/js/alertify.js', '/bower_components/spin.js/spin.min.js', '/common/clipboard.js', - '/customize/fsStore.js', - '/customize/user.js', '/bower_components/jquery/dist/jquery.min.js', -], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, FS, User) { +], function (Config, Messages, Store, Crypto, Alertify, Spinner, Clipboard, FS) { /* This file exposes functionality which is specific to Cryptpad, but not to any particular pad type. This includes functions for committing metadata about pads to your local storage for future use and improved usability. @@ -26,14 +24,11 @@ define([ var storeToUse = USE_FS_STORE ? FS : Store; - var common = { - User: User, + var common = window.Cryptpad = { Messages: Messages, }; var store; var fsStore; - var userProxy; - var userStore; var find = common.find = function (map, path) { return (map && path.reduce(function (p, n) { @@ -42,7 +37,6 @@ define([ }; var getStore = common.getStore = function (legacy) { - if (!legacy && userStore) { return userStore; } if ((!USE_FS_STORE || legacy) && store) { return store; } if (USE_FS_STORE && !legacy && fsStore) { return fsStore; } throw new Error("Store is not ready!"); @@ -60,42 +54,6 @@ define([ return url; }; - /* - * cb(err, proxy); - */ - var authorize = common.authorize = function (cb) { - console.log("Authorizing"); - - User.session(void 0, function (err, secret) { - if (!secret) { - // user is not authenticated - cb('user is not authenticated', void 0); - } - - // for now we assume that things always work - User.connect(secret, function (err, proxy) { - cb(void 0, proxy); - }); - }); - }; - - // HERE - var deauthorize = common.deauthorize = function (cb) { - console.log("Deauthorizing"); - - // erase session data from storage - User.session(null, function (err) { - if (err) { - console.error(err); - } - /* - TODO better abort for this stuff... - */ - userStore = undefined; - userProxy = undefined; - }); - }; - var userHashKey = common.userHashKey = 'User_hash'; var fileHashKey = common.fileHashKey = 'FS_hash'; @@ -677,48 +635,6 @@ define([ } cb(); }); - return; -/* - authorize(function (err, proxy) { - /* - TODO - listen for log(in|out) events - update information accordingly - * / - - store.change(function (data) { - if (data.key === User.localKey) { - // HERE - if (!data.newValue) { - deauthorize(function (err) { - console.log("Deauthorized!!"); - }); - } else { - authorize(function (err, proxy) { - if (err) { - // not logged in - } - if (!proxy) { - userProxy = proxy; - userStore = User.prepareStore(proxy); - } - }); - } - } - }); - - if (err) { - // not logged in - } - if (!proxy) { - cb(); - return; - } - userProxy = env.proxy = proxy; - userStore = env.userStore = User.prepareStore(proxy); - cb(); - - }); */ }, common); }; From 86cda4357b2de0869abcfa14d60edbf8c5c45bef Mon Sep 17 00:00:00 2001 From: ansuz Date: Wed, 21 Dec 2016 18:24:48 +0100 Subject: [PATCH 2/4] handle more possible cases --- www/login/main.js | 79 ++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/www/login/main.js b/www/login/main.js index 239ca9c6b..5f23f26cf 100644 --- a/www/login/main.js +++ b/www/login/main.js @@ -124,9 +124,24 @@ define([ /* if the user is registering, we expect that the userDoc will be empty */ + + var proxykeys = Object.keys(proxy); + if (opt.register) { - if (Object.keys(proxy).length) { + if (proxyKeys.length) { + // user is trying to register, but the userDoc is not empty + // tell them they are already registered. + + alreadyExists(); + } else { + // trying to register, and the object is empty, as expected + } + } else { + if (proxyKeys.length) { + + } else { + } } @@ -196,39 +211,41 @@ define([ $register[0].checked = false; }; - if (Cryptpad.getUserHash()) { - //Cryptpad.alert("You are already logged in!"); - $logoutBox.slideDown(); - } else { - revealLogin(); - } - - $login.click(function () { - var uname = $username.val(); - var passwd = $password.val(); - var confirm = $confirm.val(); - var remember = $remember[0].checked; - var register = $register[0].checked; - - if (!Cred.isValidUsername(uname)) { - return void Cryptpad.alert('invalid username'); - } - if (!Cred.isValidPassword(passwd)) { - return void Cryptpad.alert('invalid password'); - } - if (register && !Cred.passwordsMatch(passwd, confirm)) { - return mismatchedPasswords(); + Cryptpad.ready(function () { + if (Cryptpad.getUserHash()) { + //Cryptpad.alert("You are already logged in!"); + $logoutBox.slideDown(); + } else { + revealLogin(); } - resetUI(); + $login.click(function () { + var uname = $username.val(); + var passwd = $password.val(); + var confirm = $confirm.val(); + var remember = $remember[0].checked; + var register = $register[0].checked; + + if (!Cred.isValidUsername(uname)) { + return void Cryptpad.alert('invalid username'); + } + if (!Cred.isValidPassword(passwd)) { + return void Cryptpad.alert('invalid password'); + } + if (register && !Cred.passwordsMatch(passwd, confirm)) { + return mismatchedPasswords(); + } + + resetUI(); - // consume 128 bytes, to be divided later - // we can safely increase this size, but we don't need much right now - hashFromCreds(uname, passwd, 128, function (bytes) { - useBytes(bytes, { - remember: remember, - register: register, - name: uname, + // consume 128 bytes, to be divided later + // we can safely increase this size, but we don't need much right now + hashFromCreds(uname, passwd, 128, function (bytes) { + useBytes(bytes, { + remember: remember, + register: register, + name: uname, + }); }); }); }); From 84d5e4b35ddec565d177dd55d82543927a8f8828 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 22 Dec 2016 11:03:44 +0100 Subject: [PATCH 3/4] better tools for playing with template code --- www/read/index.html | 7 +++++-- www/read/main.js | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/www/read/index.html b/www/read/index.html index 32f6f5a06..326e46158 100644 --- a/www/read/index.html +++ b/www/read/index.html @@ -30,6 +30,9 @@ pre { - -

+
+ + + + diff --git a/www/read/main.js b/www/read/main.js index 4a8d36eeb..9bce1ebe7 100644 --- a/www/read/main.js +++ b/www/read/main.js @@ -9,8 +9,8 @@ define([ var useDoc = function (err, doc) { if (err) { return console.error(err); } - console.log(doc); - $dest.text(doc); + //console.log(doc); + $('#putter').val(doc); }; $('#get').click(function () { @@ -19,5 +19,17 @@ define([ Crypt.get(val, useDoc); }); + $('#put').click(function () { + var hash = $target.val().trim(); + Crypt.put(hash, $('#putter').val(), function (e) { + if (e) { console.error(e); } + $('#get').click(); + }); + }); + + $('#open').click(function () { + window.open('/code/#' + $target.val()); + }); + if (window.location.hash) { Crypt.get(void 0, useDoc); } }); From 29d406b7a9496f07ed662fbfe7a7ebaeb7e59c69 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 22 Dec 2016 16:00:13 +0100 Subject: [PATCH 4/4] implement cryptput --- www/common/cryptget.js | 100 ++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 36 deletions(-) diff --git a/www/common/cryptget.js b/www/common/cryptget.js index 144c9dff9..31e48e8ce 100644 --- a/www/common/cryptget.js +++ b/www/common/cryptget.js @@ -1,59 +1,87 @@ define([ - '/customize/messages.js?app=read', + '/customize/messages.js?app=cryptget', '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/common/cryptpad-common.js', -], function (Messages, Crypto, Realtime, Cryptpad) { - var Crypt = {}; + '/bower_components/textpatcher/TextPatcher.js', + '/bower_components/jquery/dist/jquery.min.js', +], function (Messages, Crypto, Realtime, Cryptpad, TextPatcher) { + var noop = function () {}; + var finish = function (S, err, doc) { + if (S.done) { return; } + S.cb(err, doc); + S.done = true; - var finish = function (Session, err, doc) { - if (Session.done) { return; } - Session.cb(err, doc); - Session.done = true; - - var abort = Cryptpad.find(Session, - ['realtime', 'realtime', 'abort']); - if (typeof(abort) === 'function') { abort(); } - }; - - var get = Crypt.get = function (hash, cb, opt) { - if (typeof(cb) !== 'function') { - throw new Error('Crypt.get expects a callback'); + var abort = Cryptpad.find(S, ['realtime', 'realtime', 'abort']); + if (typeof(abort) === 'function') { + S.realtime.realtime.sync(); + abort(); } + }; - var Session = { - cb: cb, - }; - opt = opt || {}; - var secret = Session.secret = Cryptpad.getSecrets(hash); - + var makeConfig = function (hash) { + var secret = Cryptpad.getSecrets(hash); + if (!secret.keys) { secret.keys = secret.key; } // support old hashses var config = { websocketURL: Cryptpad.getWebsocketURL(), channel: secret.channel, + validateKey: secret.keys.validateKey || undefined, crypto: Crypto.createEncryptor(secret.keys), logLevel: 0, }; + return config; + }; - var onError = config.onError = function () { - finish(Session, Messages.websocketError); - }; - var onAbort = config.onAbort = function () { - finish(Session, Messages.disconnectAlert); - }; + var isObject = function (o) { + return typeof(o) === 'object'; + }; + + var overwrite = function (a, b) { + if (!(isObject(a) && isObject(b))) { return; } + Object.keys(b).forEach(function (k) { a[k] = b[k]; }); + }; + + var get = function (hash, cb, opt) { + if (typeof(cb) !== 'function') { + throw new Error('Cryptget expects a callback'); + } + var Session = { cb: cb, }; + var config = makeConfig(hash); var onReady = config.onReady = function (info) { - var realtime = Session.realtime = info.realtime; - finish(Session, void 0, realtime.getUserDoc()); + var rt = Session.session = info.realtime; + finish(Session, void 0, rt.getUserDoc()); }; + overwrite(config, opt); + + var realtime = Session.realtime = Realtime.start(config); + }; - var onConnectionChange = config.onConnectionChange = function (info) { - if (info.state) { return; } - finish(Session, Messages.disconnectAlert); + var put = function (hash, doc, cb, opt) { + if (typeof(cb) !== 'function') { + throw new Error('Cryptput expects a callback'); + } + + var config = makeConfig(hash); + var Session = { cb: cb, }; + config.onReady = function (info) { + var realtime = Session.session = info.realtime; + + TextPatcher.create({ + realtime: realtime, + })(doc); + realtime.sync(); + realtime.abort(); + + finish(Session, void 0); }; - Object.keys(opt).forEach(function (k) { config[k] = opt[k]; }); + overwrite(config, opt); - return (Session.realtime = Realtime.start(config)); + var realtime = Session.session = Realtime.start(config); }; - return Crypt; + return { + get: get, + put: put, + }; });