Change user account password function

pull/1/head
yflory 7 years ago
parent 6fde027a6c
commit a146f6acc9

@ -613,6 +613,7 @@ define(function () {
out.settings_changePasswordNew = "New password"; // XXX out.settings_changePasswordNew = "New password"; // XXX
out.settings_changePasswordNewConfirm = "Confirm new password"; // XXX out.settings_changePasswordNewConfirm = "Confirm new password"; // XXX
out.settings_changePasswordConfirm = "Are you sure?"; // XXX out.settings_changePasswordConfirm = "Are you sure?"; // XXX
out.settings_changePasswordError = "Error {0}"; // XXX
out.upload_title = "File upload"; out.upload_title = "File upload";
out.upload_modal_title = "File upload options"; out.upload_modal_title = "File upload options";

@ -12,6 +12,7 @@ define(function () {
oldStorageKey: 'CryptPad_RECENTPADS', oldStorageKey: 'CryptPad_RECENTPADS',
storageKey: 'filesData', storageKey: 'filesData',
tokenKey: 'loginToken', tokenKey: 'loginToken',
displayPadCreationScreen: 'displayPadCreationScreen' displayPadCreationScreen: 'displayPadCreationScreen',
deprecatedKey: 'deprecated'
}; };
}); });

@ -699,25 +699,43 @@ define([
}); });
}; };
common.ownUserDrive = function (Crypt, edPublic, cb) { common.changeUserPassword = function (Crypt, edPublic, data, cb) {
var hash = LocalStore.getUserHash(); if (!edPublic) {
//var href = '/drive/#' + hash; return void cb({
error: 'E_NOT_LOGGED_IN'
});
}
var accountName = LocalStore.getAccountName();
var hash = LocalStore.getUserHash(); // To load your old drive
var password = data.password; // To remove your old block
var newPassword = data.newPassword; // To create your new block
var secret = Hash.getSecrets('drive', hash); var secret = Hash.getSecrets('drive', hash);
var newHash, newHref, newSecret; var newHash, newHref, newSecret, newBlockSeed;
var oldIsOwned = false;
// XXX ansuz: check that the old password is correct
throw new Error("XXX");
var blockHash = LocalStore.getBlockHash();
var Cred, Block;
Nthen(function (waitFor) { Nthen(function (waitFor) {
require([
'/customize/credential.js',
'/common/outer/login-block.js'
], waitFor(function (_Cred, _Block) {
Cred = _Cred;
Block = _Block;
}));
}).nThen(function (waitFor) {
// Check if our drive is already owned // Check if our drive is already owned
common.anonRpcMsg('GET_METADATA', secret.channel, waitFor(function (err, obj) { common.anonRpcMsg('GET_METADATA', secret.channel, waitFor(function (err, obj) {
if (err || obj.error) { return; } if (err || obj.error) { return; }
if (obj.owners && Array.isArray(obj.owners) && if (obj.owners && Array.isArray(obj.owners) &&
obj.owners.indexOf(edPublic) !== -1) { obj.owners.indexOf(edPublic) !== -1) {
waitFor.abort(); oldIsOwned = true;
cb({
error: 'ALREADY_OWNED'
});
} }
})); }));
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
waitFor.abort(); // TODO remove this line
// Create a new user hash // Create a new user hash
// Get the current content, store it in the new user file // Get the current content, store it in the new user file
// and make sure the new user drive is owned // and make sure the new user drive is owned
@ -742,26 +760,67 @@ define([
}), optsPut); }), optsPut);
})); }));
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
// Migration success // Drive content copied: get the new block location
// TODO: Replace user hash in login block Cred.deriveFromPassphrase(accountName, newPassword, 192, waitFor(function (bytes) {
newBlockSeed = null; // XXX
}));
}).nThen(function (waitFor) {
// Write the new login block
var keys = Block.genkeys(newBlockSeed);
var content = Block.serialize(JSON.stringify({
User_name: accountName,
User_hash: newHash
}), keys);
common.writeLoginBlock(content, waitFor(function (obj) {
var newBlockHash = Block.getBlockHash(keys);
LocalStore.setBlockHash(newBlockHash);
if (obj && obj.error) {
waitFor.abort();
return void cb(obj);
}
}));
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
// New drive hash is in login block, unpin the old one and pin the new one // New drive hash is in login block, unpin the old one and pin the new one
common.unpinPads([secret.channel], waitFor()); common.unpinPads([secret.channel], waitFor());
common.pinPads([newSecret.channel], waitFor()); common.pinPads([newSecret.channel], waitFor());
}).nThen(function (waitFor) { }).nThen(function (waitFor) {
// Login block updated // Remove block hash
// TODO: logout everywhere if (blockHash) {
// * It should wipe localStorage.User_hash, ... var removeData = Block.remove(keys);
// * login will get the new value from loginBlock and store it in localStorage common.removeLoginBlock(removeData, waitFor(function (obj) {
// * SharedWorker will reconnect with the new value in other locations if (obj && obj.error) { return void console.error(obj.error); }
// TODO: then DISCONNECT here }));
}
}).nThen(function (waitFor) {
if (oldIsOwned) {
common.removeOwnedChannel(secret.channel, waitFor(function (obj) {
if (obj && obj.error) {
// Deal with it as if it was not owned
oldIsOwned = false;
return;
}
common.logoutFromAll(waitFor(function () { common.logoutFromAll(waitFor(function () {
postMessage("DISCONNECT"); postMessage("DISCONNECT");
})); }));
}));
}
}).nThen(function (waitFor) {
if (!oldIsOwned) {
postMessage("SET", {
key: [Constants.deprecatedKey],
value: true
}, waitFor(function (obj) {
if (obj && obj.error) {
console.error(obj.error);
}
common.logoutFromAll(waitFor(function () {
postMessage("DISCONNECT");
}));
}));
}
}).nThen(function () { }).nThen(function () {
// We have the new drive, with the new login block // We have the new drive, with the new login block
// TODO: maybe reload automatically? window.location.reload();
cb({ state: true });
}); });
}; };

@ -661,8 +661,8 @@ define([
Cryptpad.changePadPassword(Cryptget, href, data.password, edPublic, cb); Cryptpad.changePadPassword(Cryptget, href, data.password, edPublic, cb);
}); });
sframeChan.on('Q_OWN_USER_DRIVE', function (data, cb) { sframeChan.on('Q_CHANGE_USER_PASSWORD', function (data, cb) {
Cryptpad.ownUserDrive(Cryptget, edPublic, cb); Cryptpad.changeUserPassword(Cryptget, edPublic, data, cb);
}); });
sframeChan.on('Q_WRITE_LOGIN_BLOCK', function (data, cb) { sframeChan.on('Q_WRITE_LOGIN_BLOCK', function (data, cb) {

@ -239,7 +239,7 @@ define({
'Q_PAD_PASSWORD_CHANGE': true, 'Q_PAD_PASSWORD_CHANGE': true,
// Migrate drive to owned drive // Migrate drive to owned drive
'Q_OWN_USER_DRIVE': true, 'Q_CHANGE_USER_PASSWORD': true,
// Loading events to display in the loading screen // Loading events to display in the loading screen
'EV_LOADING_INFO': true, 'EV_LOADING_INFO': true,

@ -53,7 +53,7 @@ define([
'cp-settings-thumbnails', 'cp-settings-thumbnails',
'cp-settings-userfeedback', 'cp-settings-userfeedback',
'cp-settings-change-password', 'cp-settings-change-password',
'cp-settings-migrate', //'cp-settings-migrate',
'cp-settings-delete' 'cp-settings-delete'
], ],
'creation': [ 'creation': [
@ -407,43 +407,11 @@ define([
$(form).appendTo($div); $(form).appendTo($div);
var updateBlock = function (data, cb) { var updateBlock = function (data, cb) {
sframeChan.query('Q_WRITE_LOGIN_BLOCK', data, function (err, obj) { sframeChan.query('Q_CHANGE_USER_PASSWORD', data, function (err, obj) {
if (err || obj.error) { return void cb ({error: err || obj.error}); } if (err || obj.error) { return void cb ({error: err || obj.error}); }
cb (obj); cb (obj);
}); });
}; };
/*
var removeBlock = function (data, cb) {
sframeChan.query('Q_REMOVE_LOGIN_BLOCK', data, function (err, obj) {
if (err || obj.error) { return void cb ({error: err || obj.error}); }
cb (obj);
});
};*/
// XXX
if (false) { // STUBBED, just for development purposes
console.error("TRYING TO WRITE A BLOCK");
var keys = Block.genkeys(Block.seed());
var data = Block.serialize(JSON.stringify({
a: 5,
b: 6,
User_hash: "XXX", /// TODO encode newly derived User_hash here
}), keys);
updateBlock(data, function (err, thing) {
console.log(err, thing);
console.log(Block.getBlockHash(keys));
return;
/*
removeBlock(Block.remove(keys), function (err, obj) {
console.log(err, obj);
});*/
});
}
var todo = function () { var todo = function () {
var oldPassword = $(form).find('#cp-settings-change-password-current').val(); var oldPassword = $(form).find('#cp-settings-change-password-current').val();
@ -466,8 +434,15 @@ define([
UI.confirm(Messages.settings_changePasswordConfirm, UI.confirm(Messages.settings_changePasswordConfirm,
function (yes) { function (yes) {
if (!yes) { return; } if (!yes) { return; }
updateBlock({
password: oldPassword,
newPassword: newPassword
}, function (obj) {
if (obj && obj.error) {
// TODO // TODO
console.log(oldPassword, newPassword, newPasswordConfirm); UI.alert(Messages.settings_changePasswordError);
}
});
}, { }, {
ok: Messages.register_writtenPassword, ok: Messages.register_writtenPassword,
cancel: Messages.register_cancel, cancel: Messages.register_cancel,
@ -496,6 +471,7 @@ define([
}; };
create['migrate'] = function () { create['migrate'] = function () {
return;
// TODO // TODO
// if (!loginBlock) { return; } // if (!loginBlock) { return; }
// if (alreadyMigrated) { return; } // if (alreadyMigrated) { return; }

Loading…
Cancel
Save