Merge branch 'users'

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

@ -3,34 +3,38 @@ define([
'/customize/messages.js', '/customize/messages.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js', '/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-crypto/crypto.js',
'/customize/store.js',
'/bower_components/scrypt-async/scrypt-async.min.js', '/bower_components/scrypt-async/scrypt-async.min.js',
'/bower_components/tweetnacl/nacl-fast.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 Scrypt = window.scrypt;
var Nacl = window.nacl; var Nacl = window.nacl;
var localKey = 'cryptpad_user_session';
var User = {}; 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 isArray = function (o) { return Object.prototype.toString.call(o) === '[object Array]'; };
var session = User.session = function (secret) { var session = User.session = function (secret, cb) {
// TODO use store.js, not localStorage?
if (secret) { if (secret) {
localStorage.setItem(localKey, JSON.stringify(secret)); store.set(localKey, secret, cb);
return; return;
} }
if (secret === null) { if (secret === null) {
localStorage.removeItem(localKey); store.remove(localKey, cb);
return;
}
var temp = localStorage.getItem(localKey);
try {
return JSON.parse(temp);
} catch (err) {
return null;
} }
store.get(localKey, cb);
}; };
/* 64 uint8s symmetric keys /* 64 uint8s symmetric keys
@ -91,11 +95,12 @@ define([
initialize(rt.proxy, secret, cb); 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 genSecret = User.genSecret = function (uname, pw, cb) {
*/
var login = User.login = function (uname, pw, cb) {
Scrypt(pw, Scrypt(pw,
uname, uname,
15, // memory cost parameter 15, // memory cost parameter
@ -105,8 +110,19 @@ define([
function (bytes) { function (bytes) {
var secret = parse128(bytes); var secret = parse128(bytes);
secret.username = uname; 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); connect(secret, cb);
});
}); });
}; };

@ -17,11 +17,19 @@ define([
*/ */
var $ = window.jQuery; var $ = window.jQuery;
var common = {}; var common = {
User: User,
};
var store; var store;
var userProxy; var userProxy;
var userStore; 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) { var getStore = common.getStore = function (legacy) {
if (!legacy && userStore) { return userStore; } if (!legacy && userStore) { return userStore; }
if (store) { return store; } if (store) { return store; }
@ -33,15 +41,34 @@ define([
*/ */
var authorize = common.authorize = function (cb) { var authorize = common.authorize = function (cb) {
console.log("Authorizing"); 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.session(void 0, function (err, secret) {
User.connect(secret, function (err, proxy) { if (!secret) {
cb(void 0, proxy); // 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 env = {};
var cb = function () { var cb = function () {
state--; f(void 0, env);
if (!state) {
f(void 0, env);
}
}; };
state = 2;
Store.ready(function (err, store) { Store.ready(function (err, store) {
common.store = env.store = store; common.store = env.store = store;
cb();
});
// HERE authorize(function (err, proxy) {
authorize(function (err, proxy) { /*
if (err) { TODO
// not logged in listen for log(in|out) events
} update information accordingly
if (!proxy) { */
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(); cb();
return;
} });
userProxy = env.proxy = proxy;
userStore = env.userStore = User.prepareStore(proxy);
cb();
}); });
}; };

Loading…
Cancel
Save