cryptpad/customize.dist/login.js

233 lines
9.3 KiB
JavaScript
Raw Normal View History

2023-10-20 14:35:26 +00:00
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2017-02-09 15:31:57 +00:00
define([
'jquery',
'chainpad-listmap',
'/components/chainpad-crypto/crypto.js',
2017-11-13 15:32:40 +00:00
'/common/common-util.js',
2017-11-23 11:28:49 +00:00
'/common/outer/network-config.js',
2023-06-27 14:04:32 +00:00
'/common/common-login.js',
'/common/common-credential.js',
'/components/chainpad/chainpad.dist.js',
'/common/common-realtime.js',
'/common/common-constants.js',
'/common/common-interface.js',
'/common/common-feedback.js',
2023-09-08 16:10:02 +00:00
'/common/hyperscript.js',
'/common/outer/local-store.js',
'/customize/messages.js',
'/components/nthen/index.js',
2018-06-21 14:52:04 +00:00
'/common/outer/login-block.js',
2018-07-06 09:36:48 +00:00
'/common/common-hash.js',
'/common/outer/http-command.js',
2017-11-24 14:12:00 +00:00
'/components/tweetnacl/nacl-fast.min.js',
'/components/scrypt-async/scrypt-async.min.js', // better load speed
2023-06-27 14:04:32 +00:00
], function ($, Listmap, Crypto, Util, NetConfig, Login, Cred, ChainPad, Realtime, Constants, UI,
2023-09-08 16:10:02 +00:00
Feedback, h, LocalStore, Messages, nThen, Block, Hash, ServerCommand) {
2017-02-09 15:31:57 +00:00
var Exports = {
Cred: Cred,
Block: Block,
// this is depended on by non-customizable files
// be careful when modifying login.js
2023-06-27 14:04:32 +00:00
requiredBytes: Login.requiredBytes,
2017-02-09 15:31:57 +00:00
};
var Nacl = window.nacl;
var redirectTo = '/drive/';
var setRedirectTo = function () {
var parsed = Hash.parsePadUrl(window.location.href);
if (parsed.hashData && parsed.hashData.newPadOpts) {
var newPad = Hash.decodeDataOptions(parsed.hashData.newPadOpts);
redirectTo = newPad.href;
}
};
2023-06-27 14:04:32 +00:00
if (window.location.hash) { setRedirectTo(); }
2023-06-27 14:04:32 +00:00
Exports.ssoAuth = function (provider, cb) {
2023-06-23 15:46:14 +00:00
var keys = Nacl.sign.keyPair();
localStorage.CP_sso_auth = JSON.stringify({
s: Nacl.util.encodeBase64(keys.secretKey),
p: Nacl.util.encodeBase64(keys.publicKey)
});
ServerCommand(keys, {
command: 'SSO_AUTH',
provider: provider,
register: true
}, cb);
};
Exports.ssoLogin = function () {
};
2023-06-27 14:04:32 +00:00
Exports.allocateBytes = Login.allocateBytes;
Exports.loadUserObject = Login.loadUserObject;
2018-07-06 09:36:48 +00:00
2023-06-27 14:04:32 +00:00
var setMergeAnonDrive = function (value) {
Exports.mergeAnonDrive = Boolean(value);
2018-07-06 09:36:48 +00:00
};
2018-02-07 16:14:15 +00:00
Exports.redirect = function () {
if (redirectTo) {
var h = redirectTo;
var loginOpts = {};
if (Exports.mergeAnonDrive) {
loginOpts.mergeAnonDrive = 1;
}
h = Hash.getLoginURL(h, loginOpts);
2018-02-07 16:14:15 +00:00
var parser = document.createElement('a');
parser.href = h;
if (parser.origin === window.location.origin) {
window.location.href = h;
return;
}
}
window.location.href = '/drive/';
};
2017-02-09 15:31:57 +00:00
var hashing;
2023-06-06 10:34:24 +00:00
Exports.loginOrRegisterUI = function (uname, passwd, isRegister, shouldImport, onOTP, testing, test) {
if (hashing) { return void console.log("hashing is already in progress"); }
hashing = true;
2023-06-27 14:04:32 +00:00
setMergeAnonDrive(shouldImport);
var proceed = function (result) {
2018-02-07 16:14:15 +00:00
hashing = false;
2022-12-20 15:56:41 +00:00
// NOTE: test is also use as a cb for the install page
if (test && typeof test === "function" && test(result)) { return; }
LocalStore.clearLoginToken();
Realtime.whenRealtimeSyncs(result.realtime, function () {
2018-02-07 16:14:15 +00: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 () {
2023-10-18 13:39:54 +00:00
Login.loginOrRegister({
uname,
2023-06-27 14:04:32 +00:00
passwd,
isRegister,
onOTP
}, 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;
$('#password').focus();
});
});
break;
case 'INVAL_USER':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_notFilledUser , function () {
hashing = false;
$('#password').focus();
});
});
break;
/*
case 'HAS_PLACEHOLDER':
UI.errorLoadingScreen('UNAVAILABLE', true, true);
break;
*/
2023-09-08 16:10:02 +00:00
case 'DELETED_USER':
UI.errorLoadingScreen(
UI.getDestroyedPlaceholder(result.reason, true), true, () => {
window.location.reload();
});
2023-09-08 16:10:02 +00:00
break;
case 'INVAL_PASS':
UI.removeLoadingScreen(function () {
UI.alert(Messages.login_notFilledPass, function () {
hashing = false;
$('#password').focus();
});
});
break;
case 'PASS_TOO_SHORT':
UI.removeLoadingScreen(function () {
var warning = Messages._getKey('register_passwordTooShort', [
Cred.MINIMUM_PASSWORD_LENGTH
]);
UI.alert(warning, function () {
hashing = false;
$('#password').focus();
});
});
break;
case 'ALREADY_REGISTERED':
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;
}
var block = result.blockHash;
var user = block ? undefined : result.userHash;
LocalStore.login(user, block, result.userName, function () {
2018-06-21 08:40:09 +00:00
setTimeout(function () { proceed(result); });
});
});
});
break;
2021-04-26 13:01:33 +00:00
case 'E_RESTRICTED':
2021-05-04 08:15:44 +00:00
UI.errorLoadingScreen(Messages.register_registrationIsClosed);
2021-04-26 13:01:33 +00:00
break;
default: // UNHANDLED ERROR
hashing = false;
UI.errorLoadingScreen(Messages.login_unhandledError);
}
return;
}
//if (testing) { return void proceed(result); }
if (!(proxy.curvePrivate && proxy.curvePublic &&
proxy.edPrivate && proxy.edPublic)) {
console.log("recovering derived public/private keypairs");
// **** reset keys ****
proxy.curvePrivate = result.curvePrivate;
proxy.curvePublic = result.curvePublic;
proxy.edPrivate = result.edPrivate;
proxy.edPublic = result.edPublic;
}
setTimeout(function () {
Realtime.whenRealtimeSyncs(result.realtime, function () {
proceed(result);
});
});
});
}, 500);
}, 200);
};
2017-02-09 15:31:57 +00:00
return Exports;
});