cryptpad/customize.dist/login.js

318 lines
12 KiB
JavaScript
Raw Normal View History

2017-02-09 23:31:57 +08:00
define([
'jquery',
2017-02-09 23:31:57 +08:00
'/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js',
2017-11-13 23:32:40 +08:00
'/common/common-util.js',
2017-11-23 19:28:49 +08:00
'/common/outer/network-config.js',
'/customize/credential.js',
2017-11-24 22:12:00 +08:00
'/bower_components/chainpad/chainpad.dist.js',
'/common/common-realtime.js',
'/common/common-constants.js',
'/common/common-interface.js',
'/common/common-feedback.js',
'/common/outer/local-store.js',
'/customize/messages.js',
2018-06-21 16:26:23 +08:00
'/bower_components/nthen/index.js',
2018-06-21 22:52:04 +08:00
'/common/outer/login-block.js',
2017-11-24 22:12:00 +08:00
2017-02-09 23:31:57 +08:00
'/bower_components/tweetnacl/nacl-fast.min.js',
'/bower_components/scrypt-async/scrypt-async.min.js', // better load speed
], function ($, Listmap, Crypto, Util, NetConfig, Cred, ChainPad, Realtime, Constants, UI,
2018-06-21 22:52:04 +08:00
Feedback, LocalStore, Messages, nThen, Block) {
2017-02-09 23:31:57 +08:00
var Exports = {
Cred: Cred,
// this is depended on by non-customizable files
// be careful when modifying login.js
requiredBytes: 192,
2017-02-09 23:31:57 +08:00
};
var Nacl = window.nacl;
var allocateBytes = Exports.allocateBytes = function (bytes) {
2017-02-09 23:31:57 +08:00
var dispense = Cred.dispenser(bytes);
var opt = {};
// dispense 18 bytes of entropy for your encryption key
var encryptionSeed = dispense(18);
// 16 bytes for a deterministic channel key
var channelSeed = dispense(16);
// 32 bytes for a curve key
var curveSeed = dispense(32);
var curvePair = Nacl.box.keyPair.fromSecretKey(new Uint8Array(curveSeed));
opt.curvePrivate = Nacl.util.encodeBase64(curvePair.secretKey);
opt.curvePublic = Nacl.util.encodeBase64(curvePair.publicKey);
2017-02-09 23:31:57 +08:00
// 32 more for a signing key
var edSeed = opt.edSeed = dispense(32);
2018-06-21 16:26:46 +08:00
// 32 more bytes to seed an additional signing key
opt.blockSeed = dispense(64);
2018-06-21 16:26:46 +08:00
// derive a private key from the ed seed
var signingKeypair = Nacl.sign.keyPair.fromSeed(new Uint8Array(edSeed));
opt.edPrivate = Nacl.util.encodeBase64(signingKeypair.secretKey);
opt.edPublic = Nacl.util.encodeBase64(signingKeypair.publicKey);
2017-02-09 23:31:57 +08:00
var keys = opt.keys = Crypto.createEditCryptor(null, encryptionSeed);
// 24 bytes of base64
keys.editKeyStr = keys.editKeyStr.replace(/\//g, '-');
// 32 bytes of hex
2017-11-13 23:32:40 +08:00
var channelHex = opt.channelHex = Util.uint8ArrayToHex(channelSeed);
2017-02-09 23:31:57 +08:00
// should never happen
if (channelHex.length !== 32) { throw new Error('invalid channel id'); }
2017-11-13 23:32:40 +08:00
opt.channel64 = Util.hexToBase64(channelHex);
2017-02-09 23:31:57 +08:00
2017-05-04 22:16:09 +08:00
opt.userHash = '/1/edit/' + [opt.channel64, opt.keys.editKeyStr].join('/');
2017-02-09 23:31:57 +08:00
return opt;
};
var loadUserObject = function (opt, cb) {
var config = {
2017-11-23 19:28:49 +08:00
websocketURL: NetConfig.getWebsocketURL(),
2017-02-09 23:31:57 +08:00
channel: opt.channelHex,
data: {},
validateKey: opt.keys.validateKey, // derived validation key
crypto: Crypto.createEncryptor(opt.keys),
logLevel: 1,
2017-11-24 22:12:00 +08:00
classic: true,
ChainPad: ChainPad,
2018-03-14 23:32:17 +08:00
owners: [opt.edPublic]
2017-02-09 23:31:57 +08:00
};
var rt = opt.rt = Listmap.create(config);
rt.proxy
2017-05-04 22:16:09 +08:00
.on('ready', function () {
2018-02-08 19:24:34 +08:00
setTimeout(function () { cb(void 0, rt); });
2017-02-09 23:31:57 +08:00
})
.on('disconnect', function (info) {
cb('E_DISCONNECT', info);
});
};
var isProxyEmpty = function (proxy) {
return Object.keys(proxy).length === 0;
};
2018-02-08 00:14:15 +08:00
Exports.loginOrRegister = function (uname, passwd, isRegister, shouldImport, cb) {
2017-02-09 23:31:57 +08:00
if (typeof(cb) !== 'function') { return; }
2017-02-13 18:01:30 +08:00
// Usernames are all lowercase. No going back on this one
uname = uname.toLowerCase();
2017-02-09 23:31:57 +08:00
// validate inputs
if (!Cred.isValidUsername(uname)) { return void cb('INVAL_USER'); }
if (!Cred.isValidPassword(passwd)) { return void cb('INVAL_PASS'); }
if (isRegister && !Cred.isLongEnoughPassword(passwd)) {
2017-09-11 20:04:40 +08:00
return void cb('PASS_TOO_SHORT');
}
2017-02-09 23:31:57 +08:00
2018-06-21 16:26:23 +08:00
// results...
var res = {
register: isRegister,
};
2017-02-09 23:31:57 +08:00
2018-06-21 16:26:23 +08:00
var RT;
2017-02-09 23:31:57 +08:00
2018-06-21 16:26:23 +08:00
nThen(function (waitFor) {
Cred.deriveFromPassphrase(uname, passwd, Exports.requiredBytes, waitFor(function (bytes) {
2018-06-21 16:26:23 +08:00
// run scrypt to derive the user's keys
res.opt = allocateBytes(bytes);
}));
2018-06-21 22:52:04 +08:00
// TODO consider checking the block here
}).nThen(function (/* waitFor */) {
// check for blocks
Block = Block; // jshint
2018-06-21 16:26:23 +08:00
}).nThen(function (waitFor) {
var opt = res.opt;
2017-02-09 23:31:57 +08:00
// use the derived key to generate an object
2018-06-21 16:26:23 +08:00
loadUserObject(opt, waitFor(function (err, rt) {
2017-02-09 23:31:57 +08:00
if (err) { return void cb(err); }
2018-06-21 16:26:23 +08:00
RT = rt;
2017-02-09 23:31:57 +08:00
res.proxy = rt.proxy;
res.realtime = rt.realtime;
res.network = rt.network;
// they're registering...
res.userHash = opt.userHash;
res.userName = uname;
// export their signing key
res.edPrivate = opt.edPrivate;
res.edPublic = opt.edPublic;
res.curvePrivate = opt.curvePrivate;
res.curvePublic = opt.curvePublic;
2017-02-09 23:31:57 +08:00
// they tried to just log in but there's no such user
if (!isRegister && isProxyEmpty(rt.proxy)) {
rt.network.disconnect(); // clean up after yourself
2018-06-21 16:26:23 +08:00
waitFor.abort();
2017-02-09 23:31:57 +08:00
return void cb('NO_SUCH_USER', res);
}
// they tried to register, but those exact credentials exist
if (isRegister && !isProxyEmpty(rt.proxy)) {
rt.network.disconnect();
2018-06-21 16:26:23 +08:00
waitFor.abort();
return void cb('ALREADY_REGISTERED', res);
}
2017-02-09 23:31:57 +08:00
2018-02-08 00:14:15 +08:00
if (isRegister) {
var proxy = rt.proxy;
proxy.edPublic = res.edPublic;
proxy.edPrivate = res.edPrivate;
proxy.curvePublic = res.curvePublic;
proxy.curvePrivate = res.curvePrivate;
proxy.login_name = uname;
proxy[Constants.displayNameKey] = uname;
sessionStorage.createReadme = 1;
2018-05-04 21:48:55 +08:00
if (!shouldImport) { proxy.version = 6; }
2018-02-08 00:14:15 +08:00
Feedback.send('REGISTRATION', true);
} else {
Feedback.send('LOGIN', true);
}
if (shouldImport) {
sessionStorage.migrateAnonDrive = 1;
}
2018-06-21 16:26:23 +08:00
}));
}).nThen(function () {
// We have to call whenRealtimeSyncs asynchronously here because in the current
// version of listmap, onLocal calls `chainpad.contentUpdate(newValue)`
// asynchronously.
// The following setTimeout is here to make sure whenRealtimeSyncs is called after
// `contentUpdate` so that we have an update userDoc in chainpad.
setTimeout(function () {
Realtime.whenRealtimeSyncs(RT.realtime, function () {
LocalStore.login(res.userHash, res.userName, function () {
setTimeout(function () { cb(void 0, res); });
2018-02-08 00:14:15 +08:00
});
});
2017-02-09 23:31:57 +08:00
});
});
};
2018-02-08 00:14:15 +08:00
Exports.redirect = function () {
if (sessionStorage.redirectTo) {
var h = sessionStorage.redirectTo;
var parser = document.createElement('a');
parser.href = h;
if (parser.origin === window.location.origin) {
delete sessionStorage.redirectTo;
window.location.href = h;
return;
}
}
window.location.href = '/drive/';
};
2017-02-09 23:31:57 +08:00
var hashing;
Exports.loginOrRegisterUI = function (uname, passwd, isRegister, shouldImport, testing, test) {
if (hashing) { return void console.log("hashing is already in progress"); }
hashing = true;
var proceed = function (result) {
2018-02-08 00:14:15 +08:00
hashing = false;
if (test && typeof test === "function" && test()) { return; }
Realtime.whenRealtimeSyncs(result.realtime, function () {
2018-02-08 00:14:15 +08:00
Exports.redirect();
});
};
// setTimeout 100ms to remove the keyboard on mobile devices before the loading screen
// pops up
window.setTimeout(function () {
UI.addLoadingScreen({
loadingText: Messages.login_hashing,
hideTips: true,
});
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed
// after hashing the password
window.setTimeout(function () {
2018-02-08 00:14:15 +08:00
Exports.loginOrRegister(uname, passwd, isRegister, shouldImport, function (err, result) {
var proxy;
if (result) { proxy = result.proxy; }
if (err) {
switch (err) {
case 'NO_SUCH_USER':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_noSuchUser, function () {
hashing = false;
});
});
break;
case 'INVAL_USER':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_invalUser, function () {
hashing = false;
});
});
break;
case 'INVAL_PASS':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_invalPass, function () {
hashing = false;
});
});
break;
case 'PASS_TOO_SHORT':
UI.removeLoadingScreen(function () {
var warning = Messages._getKey('register_passwordTooShort', [
Cred.MINIMUM_PASSWORD_LENGTH
]);
UI.alert(warning, function () {
hashing = false;
});
});
break;
case 'ALREADY_REGISTERED':
// logMeIn should reset registering = false
UI.removeLoadingScreen(function () {
UI.confirm(Messages.register_alreadyRegistered, function (yes) {
if (!yes) {
hashing = false;
return;
}
proxy.login_name = uname;
if (!proxy[Constants.displayNameKey]) {
proxy[Constants.displayNameKey] = uname;
}
LocalStore.eraseTempSessionValues();
2018-06-21 16:40:09 +08:00
LocalStore.login(result.userHash, result.userName, function () {
setTimeout(function () { proceed(result); });
});
});
});
break;
default: // UNHANDLED ERROR
hashing = false;
UI.errorLoadingScreen(Messages.login_unhandledError);
}
return;
}
if (testing) { return void proceed(result); }
proceed(result);
});
}, 500);
}, 200);
};
2017-02-09 23:31:57 +08:00
return Exports;
});