You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
256 lines
8.0 KiB
JavaScript
256 lines
8.0 KiB
JavaScript
8 years ago
|
define([
|
||
|
'/api/config?cb=' + Math.random().toString(16).substring(2),
|
||
|
'/bower_components/chainpad-listmap/chainpad-listmap.js',
|
||
|
'/bower_components/chainpad-crypto/crypto.js',
|
||
|
'/common/cryptpad-common.js',
|
||
8 years ago
|
'/login/credential.js',
|
||
8 years ago
|
'/bower_components/tweetnacl/nacl-fast.min.js',
|
||
8 years ago
|
'/bower_components/scrypt-async/scrypt-async.min.js',
|
||
|
'/bower_components/jquery/dist/jquery.min.js',
|
||
8 years ago
|
], function (Config, Listmap, Crypto, Cryptpad, Cred) {
|
||
8 years ago
|
var $ = window.jQuery;
|
||
|
var Scrypt = window.scrypt;
|
||
8 years ago
|
var Nacl = window.nacl;
|
||
8 years ago
|
|
||
|
var secret = {};
|
||
|
|
||
8 years ago
|
var APP = window.APP = {
|
||
8 years ago
|
Cryptpad: Cryptpad,
|
||
8 years ago
|
Crypto: Crypto,
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var $warning = $('#warning');
|
||
|
var $login = $('#login');
|
||
|
var $logout = $('#logout');
|
||
|
var $username = $('#username');
|
||
|
var $password = $('#password');
|
||
|
var $confirm = $('#confirm');
|
||
|
var $remember = $('#remember');
|
||
|
var $loginBox = $('#login-box');
|
||
|
var $logoutBox = $('#logout-box');
|
||
|
|
||
|
var revealLogin = function () {
|
||
|
$loginBox.slideDown();
|
||
|
};
|
||
|
|
||
8 years ago
|
$logout.click(function () {
|
||
8 years ago
|
Cryptpad.logout(function () {
|
||
|
$logoutBox.slideUp();
|
||
|
revealLogin();
|
||
|
});
|
||
|
});
|
||
|
|
||
8 years ago
|
var hashFromCreds = function (username, password, len, cb) {
|
||
|
Scrypt(password,
|
||
|
username,
|
||
|
8, // memoryCost (n)
|
||
|
1024, // block size parameter (r)
|
||
|
len || 128, // dkLen
|
||
|
200, // interruptStep
|
||
|
cb,
|
||
|
undefined); // format, could be 'base64'
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
var Events = APP.Events = {};
|
||
|
var alreadyExists = Events.alreadyExists = function () {
|
||
|
Cryptpad.alert("user account already exists.");
|
||
|
};
|
||
|
var mismatchedPasswords = Events.mismatchedPasswords = function () {
|
||
|
Cryptpad.alert("passwords don't match!");
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
var useBytes = function (bytes, opt) {
|
||
|
opt = opt || {};
|
||
|
if (opt.remember) {
|
||
|
console.log("user would like to stay logged in");
|
||
|
} else {
|
||
|
console.log("user would like to be forgotten");
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
var entropy = {
|
||
|
used: 0,
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
// crypto hygeine
|
||
|
var consume = function (n) {
|
||
|
// explode if you run out of bytes
|
||
|
if (entropy.used + n > bytes.length) {
|
||
|
throw new Error('exceeded available entropy');
|
||
|
}
|
||
|
if (typeof(n) !== 'number') { throw new Error('expected a number'); }
|
||
|
if (n <= 0) {
|
||
|
throw new Error('expected to consume a positive number of bytes');
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// grab an unused slice of the entropy
|
||
|
var A = bytes.slice(entropy.used, entropy.used + n);
|
||
8 years ago
|
|
||
8 years ago
|
// account for the bytes you used so you don't reuse bytes
|
||
|
entropy.used += n;
|
||
8 years ago
|
|
||
8 years ago
|
//console.info("%s bytes of entropy remaining", bytes.length - entropy.used);
|
||
|
return A;
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
// consume 18 bytes of entropy for your encryption key
|
||
|
var encryptionSeed = consume(18);
|
||
|
// 16 bytes for a deterministic channel key
|
||
|
var channelSeed = consume(16);
|
||
|
// 32 bytes for a curve key
|
||
|
var curveSeed = consume(32);
|
||
|
// 32 more for a signing key
|
||
|
var edSeed = consume(32);
|
||
8 years ago
|
|
||
8 years ago
|
|
||
8 years ago
|
var seed = {};
|
||
|
var keys = seed.keys = Crypto.createEditCryptor(null, encryptionSeed);
|
||
8 years ago
|
|
||
8 years ago
|
// 24 bytes of base64
|
||
|
keys.editKeyStr = keys.editKeyStr.replace(/\//g, '-');
|
||
8 years ago
|
|
||
8 years ago
|
// 32 bytes of hex
|
||
|
seed.channel = Cryptpad.uint8ArrayToHex(channelSeed);
|
||
8 years ago
|
|
||
|
var channelHex = seed.channel;
|
||
|
|
||
8 years ago
|
if (channelHex.length !== 32) {
|
||
|
throw new Error('invalid channel id');
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
var channel64 = Cryptpad.hexToBase64(channelHex);
|
||
8 years ago
|
|
||
8 years ago
|
seed.editHash = Cryptpad.getEditHashFromKeys(channelHex, keys.editKeyStr);
|
||
|
//console.log("edithash: %s", seed.editHash);
|
||
8 years ago
|
|
||
8 years ago
|
var secret = Cryptpad.getSecrets(seed.editHash);
|
||
8 years ago
|
|
||
|
var config = {
|
||
|
websocketURL: Cryptpad.getWebsocketURL(),
|
||
|
channel: channelHex,
|
||
|
data: {},
|
||
8 years ago
|
validateKey: keys.validateKey, // derived validation key
|
||
8 years ago
|
crypto: Crypto.createEncryptor(seed.keys),
|
||
|
};
|
||
|
|
||
|
var rt = APP.rt = Listmap.create(config);
|
||
|
|
||
|
rt.proxy.on('create', function (info) {
|
||
8 years ago
|
console.log("loading user profile");
|
||
8 years ago
|
})
|
||
|
.on('ready', function (info) {
|
||
8 years ago
|
console.log(info);
|
||
8 years ago
|
console.log('ready');
|
||
|
var proxy = rt.proxy;
|
||
|
|
||
8 years ago
|
/* if the user is registering, we expect that the userDoc will be empty
|
||
|
*/
|
||
8 years ago
|
|
||
|
var proxyKeys = Object.keys(proxy);
|
||
|
|
||
8 years ago
|
if (opt.register) {
|
||
8 years ago
|
if (proxyKeys.length) {
|
||
|
// user is trying to register, but the userDoc is not empty
|
||
|
// tell them they are already registered.
|
||
|
|
||
|
|
||
8 years ago
|
alreadyExists();
|
||
8 years ago
|
} else {
|
||
|
// trying to register, and the object is empty, as expected
|
||
|
}
|
||
|
} else {
|
||
|
if (proxyKeys.length) {
|
||
|
// user has already initialized the object, as expected
|
||
|
} else {
|
||
|
// user has logged in, but there is no object here
|
||
|
// they should confirm their password
|
||
|
// basically this means registering
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
var now = +(new Date());
|
||
|
if (!proxy.atime) {
|
||
|
console.log("first time visiting!");
|
||
|
proxy.atime = now;
|
||
8 years ago
|
|
||
|
var name = proxy['cryptpad.username'] = opt.name;
|
||
|
console.log("setting name to %s", name);
|
||
8 years ago
|
} else {
|
||
|
console.log("last visit was %ss ago", (now - proxy.atime) / 1000);
|
||
|
proxy.atime = now;
|
||
|
}
|
||
|
|
||
8 years ago
|
var userHash = '/1/edit/' + [channel64, keys.editKeyStr].join('/');
|
||
|
|
||
|
console.log("remembering your userhash");
|
||
|
Cryptpad.login(userHash, opt.remember);
|
||
8 years ago
|
console.log(userHash);
|
||
|
$('div#login-box').slideUp();
|
||
|
$('div#logout-box').slideDown();
|
||
8 years ago
|
//console.log(proxy);
|
||
8 years ago
|
})
|
||
|
.on('disconnect', function (info) {
|
||
|
console.log('disconnected');
|
||
|
console.log(info);
|
||
|
});
|
||
|
};
|
||
8 years ago
|
var $register = $('#register').click(function () {
|
||
|
if (!$register.length) { return; }
|
||
|
var e = $register[0];
|
||
|
if (e.checked) {
|
||
|
$confirm.slideDown();
|
||
|
$login.text(Cryptpad.Messages._getKey('login_register'));
|
||
|
}
|
||
|
else {
|
||
|
$confirm.slideUp();
|
||
|
$login.text(Cryptpad.Messages._getKey('login_login'));
|
||
|
}
|
||
|
});
|
||
|
|
||
|
var resetUI = function () {
|
||
|
$username.val("");
|
||
|
$password.val("");
|
||
|
$confirm.val("");
|
||
|
$remember[0].checked = false;
|
||
|
$register[0].checked = false;
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
Cryptpad.ready(function () {
|
||
|
if (Cryptpad.getUserHash()) {
|
||
|
//Cryptpad.alert("You are already logged in!");
|
||
|
$logoutBox.slideDown();
|
||
|
} else {
|
||
|
revealLogin();
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
$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();
|
||
8 years ago
|
|
||
8 years ago
|
// 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,
|
||
|
});
|
||
8 years ago
|
});
|
||
8 years ago
|
});
|
||
|
});
|
||
|
});
|