From 2f2aeef0e59a3fd8bd9d5460717d84db8e9ea179 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 1 Sep 2016 12:06:21 +0200 Subject: [PATCH 1/3] add missing semicolon --- www/login/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/www/login/main.js b/www/login/main.js index 85021b015..b7d3d4242 100644 --- a/www/login/main.js +++ b/www/login/main.js @@ -65,7 +65,7 @@ define([ var read = function (proxy) { console.log("Proxy ready!"); - var otime = +new Date(proxy.atime) + var otime = +new Date(proxy.atime); var atime = proxy.atime = ('' + new Date()); From f74236d729195e4bde538108ab9aebe592a8def4 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 1 Sep 2016 12:15:09 +0200 Subject: [PATCH 2/3] export more, use finer granularity for methods --- customize.dist/user.js | 54 +++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/customize.dist/user.js b/customize.dist/user.js index c75a83653..f2ac25dce 100644 --- a/customize.dist/user.js +++ b/customize.dist/user.js @@ -3,34 +3,38 @@ define([ '/customize/messages.js', '/bower_components/chainpad-listmap/chainpad-listmap.js', '/bower_components/chainpad-crypto/crypto.js', + '/customize/store.js', + '/bower_components/scrypt-async/scrypt-async.min.js', '/bower_components/tweetnacl/nacl-fast.min.js', -], function (Config, Messages, Listmap, Crypto) { +], function (Config, Messages, Listmap, Crypto, Store) { var Scrypt = window.scrypt; var Nacl = window.nacl; - var localKey = 'cryptpad_user_session'; - var User = {}; + var localKey = User.localKey = 'cryptpad_user_session'; + var store; + + Store.ready(function (err, s) { + if (err) { + console.error(err); + return; + } + store = s; + }); var isArray = function (o) { return Object.prototype.toString.call(o) === '[object Array]'; }; - var session = User.session = function (secret) { - // TODO use store.js, not localStorage? + var session = User.session = function (secret, cb) { if (secret) { - localStorage.setItem(localKey, JSON.stringify(secret)); + store.set(localKey, secret, cb); return; } if (secret === null) { - localStorage.removeItem(localKey); - return; - } - var temp = localStorage.getItem(localKey); - try { - return JSON.parse(temp); - } catch (err) { - return null; + store.remove(localKey, cb); } + + store.get(localKey, cb); }; /* 64 uint8s symmetric keys @@ -91,11 +95,12 @@ define([ initialize(rt.proxy, secret, cb); }; - /* Asynchronously derive 128 random uint8s given a uname and password + var disconnect = User.disconnect = function (cb) { + var err = "User.disconnect is not implemented yet"; + cb(err); + }; - cb(proxy, secret) - */ - var login = User.login = function (uname, pw, cb) { + var genSecret = User.genSecret = function (uname, pw, cb) { Scrypt(pw, uname, 15, // memory cost parameter @@ -105,8 +110,19 @@ define([ function (bytes) { var secret = parse128(bytes); secret.username = uname; - session(secret); + cb(void 0, secret); + }); + }; + + /* Asynchronously derive 128 random uint8s given a uname and password + + cb(proxy, secret) + */ + var login = User.login = function (uname, pw, cb) { + genSecret(uname, pw, function (err, secret) { + session(secret, function (err) { connect(secret, cb); + }); }); }; From 4941c1493e7de01c010af87a18735cfedae82414 Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 1 Sep 2016 12:15:30 +0200 Subject: [PATCH 3/3] better user integration. a few more common utils --- www/common/cryptpad-common.js | 103 +++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index a4b949629..4da9816af 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -17,11 +17,19 @@ define([ */ var $ = window.jQuery; - var common = {}; + var common = { + User: User, + }; var store; var userProxy; var userStore; + var find = common.find = function (map, path) { + return (map && path.reduce(function (p, n) { + return typeof(p[n]) !== 'undefined' && p[n]; + }, map)); + }; + var getStore = common.getStore = function (legacy) { if (!legacy && userStore) { return userStore; } if (store) { return store; } @@ -33,15 +41,34 @@ define([ */ var authorize = common.authorize = function (cb) { console.log("Authorizing"); - var secret = User.session(); - 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); + 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; }); }; @@ -391,30 +418,52 @@ define([ var env = {}; var cb = function () { - state--; - if (!state) { - f(void 0, env); - } + f(void 0, env); }; - state = 2; Store.ready(function (err, store) { common.store = env.store = store; - cb(); - }); - // HERE - authorize(function (err, proxy) { - if (err) { - // not logged in - } - if (!proxy) { + 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(); - return; - } - userProxy = env.proxy = proxy; - userStore = env.userStore = User.prepareStore(proxy); - cb(); + + }); }); };