Merge branch 'users'

pull/1/head
ansuz 8 years ago
commit 97415d8158

@ -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,9 +110,20 @@ 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);
});
});
};
var prepareStore = User.prepareStore = function (proxy) {

@ -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,7 +41,8 @@ define([
*/
var authorize = common.authorize = function (cb) {
console.log("Authorizing");
var secret = User.session();
User.session(void 0, function (err, secret) {
if (!secret) {
// user is not authenticated
cb('user is not authenticated', void 0);
@ -43,6 +52,24 @@ define([
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;
});
};
Store.ready(function (err, Store) {
@ -391,20 +418,40 @@ define([
var env = {};
var cb = function () {
state--;
if (!state) {
f(void 0, env);
}
};
state = 2;
Store.ready(function (err, store) {
common.store = env.store = store;
cb();
});
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
}
@ -415,6 +462,8 @@ define([
userProxy = env.proxy = proxy;
userStore = env.userStore = User.prepareStore(proxy);
cb();
});
});
};

Loading…
Cancel
Save