2023-10-20 22:35:26 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2017-02-09 23:31:57 +08:00
|
|
|
define([
|
2017-04-18 18:14:32 +08:00
|
|
|
'jquery',
|
2021-09-03 22:29:32 +08:00
|
|
|
'chainpad-listmap',
|
2023-01-07 16:18:44 +08:00
|
|
|
'/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',
|
2023-06-27 22:04:32 +08:00
|
|
|
'/common/common-login.js',
|
2019-09-27 18:04:31 +08:00
|
|
|
'/common/common-credential.js',
|
2023-01-07 16:18:44 +08:00
|
|
|
'/components/chainpad/chainpad.dist.js',
|
2018-02-07 01:45:12 +08:00
|
|
|
'/common/common-realtime.js',
|
|
|
|
'/common/common-constants.js',
|
|
|
|
'/common/common-interface.js',
|
|
|
|
'/common/common-feedback.js',
|
2023-09-09 00:10:02 +08:00
|
|
|
'/common/hyperscript.js',
|
2018-02-07 01:45:12 +08:00
|
|
|
'/common/outer/local-store.js',
|
|
|
|
'/customize/messages.js',
|
2023-01-07 19:49:46 +08:00
|
|
|
'/components/nthen/index.js',
|
2018-06-21 22:52:04 +08:00
|
|
|
'/common/outer/login-block.js',
|
2018-07-06 17:36:48 +08:00
|
|
|
'/common/common-hash.js',
|
2023-05-06 17:09:23 +08:00
|
|
|
'/common/outer/http-command.js',
|
2024-06-21 23:01:33 +08:00
|
|
|
'/api/config',
|
2017-11-24 22:12:00 +08:00
|
|
|
|
2023-01-07 00:32:23 +08:00
|
|
|
'/components/tweetnacl/nacl-fast.min.js',
|
2023-01-07 19:25:02 +08:00
|
|
|
'/components/scrypt-async/scrypt-async.min.js', // better load speed
|
2023-06-27 22:04:32 +08:00
|
|
|
], function ($, Listmap, Crypto, Util, NetConfig, Login, Cred, ChainPad, Realtime, Constants, UI,
|
2024-06-21 23:01:33 +08:00
|
|
|
Feedback, h, LocalStore, Messages, nThen, Block, Hash, ServerCommand,
|
|
|
|
ApiConfig) {
|
2017-02-09 23:31:57 +08:00
|
|
|
var Exports = {
|
|
|
|
Cred: Cred,
|
2021-03-02 23:05:18 +08:00
|
|
|
Block: Block,
|
2018-06-22 17:37:48 +08:00
|
|
|
// this is depended on by non-customizable files
|
|
|
|
// be careful when modifying login.js
|
2023-06-27 22:04:32 +08:00
|
|
|
requiredBytes: Login.requiredBytes,
|
2017-02-09 23:31:57 +08:00
|
|
|
};
|
|
|
|
|
2017-03-16 01:57:13 +08:00
|
|
|
var Nacl = window.nacl;
|
2020-10-30 22:00:12 +08:00
|
|
|
|
|
|
|
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 22:04:32 +08:00
|
|
|
if (window.location.hash) { setRedirectTo(); }
|
2017-02-09 23:31:57 +08:00
|
|
|
|
2023-06-27 22:04:32 +08:00
|
|
|
Exports.ssoAuth = function (provider, cb) {
|
2023-06-23 23:46:14 +08:00
|
|
|
var keys = Nacl.sign.keyPair();
|
2023-12-20 00:59:56 +08:00
|
|
|
var inviteToken = window.location.hash.slice(1);
|
2023-06-23 23:46:14 +08:00
|
|
|
localStorage.CP_sso_auth = JSON.stringify({
|
|
|
|
s: Nacl.util.encodeBase64(keys.secretKey),
|
2023-12-20 00:59:56 +08:00
|
|
|
p: Nacl.util.encodeBase64(keys.publicKey),
|
|
|
|
token: inviteToken
|
2017-02-09 23:31:57 +08:00
|
|
|
});
|
2023-06-23 23:46:14 +08:00
|
|
|
ServerCommand(keys, {
|
|
|
|
command: 'SSO_AUTH',
|
|
|
|
provider: provider,
|
|
|
|
register: true
|
|
|
|
}, cb);
|
2017-02-09 23:31:57 +08:00
|
|
|
};
|
2023-06-23 23:46:14 +08:00
|
|
|
Exports.ssoLogin = function () {
|
2017-02-09 23:31:57 +08:00
|
|
|
|
2018-07-06 17:36:48 +08:00
|
|
|
};
|
|
|
|
|
2023-06-27 22:04:32 +08:00
|
|
|
Exports.allocateBytes = Login.allocateBytes;
|
|
|
|
Exports.loadUserObject = Login.loadUserObject;
|
2018-07-10 21:16:43 +08:00
|
|
|
|
2023-06-27 22:04:32 +08:00
|
|
|
var setMergeAnonDrive = function (value) {
|
|
|
|
Exports.mergeAnonDrive = Boolean(value);
|
2017-02-09 23:31:57 +08:00
|
|
|
};
|
2018-07-06 17:36:48 +08:00
|
|
|
|
2018-02-08 00:14:15 +08:00
|
|
|
Exports.redirect = function () {
|
2020-10-30 22:00:12 +08:00
|
|
|
if (redirectTo) {
|
|
|
|
var h = redirectTo;
|
|
|
|
var loginOpts = {};
|
|
|
|
if (Exports.mergeAnonDrive) {
|
|
|
|
loginOpts.mergeAnonDrive = 1;
|
|
|
|
}
|
|
|
|
h = Hash.getLoginURL(h, loginOpts);
|
|
|
|
|
2018-02-08 00:14:15 +08: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 23:31:57 +08:00
|
|
|
|
2018-02-26 17:41:37 +08:00
|
|
|
var hashing;
|
2023-12-01 22:44:20 +08:00
|
|
|
|
|
|
|
Exports.loginOrRegisterUI = function (config) {
|
2023-12-19 22:03:40 +08:00
|
|
|
let { uname, token, shouldImport, cb } = config;
|
2018-02-26 17:41:37 +08:00
|
|
|
if (hashing) { return void console.log("hashing is already in progress"); }
|
|
|
|
hashing = true;
|
2018-02-07 01:45:12 +08:00
|
|
|
|
2023-06-27 22:04:32 +08:00
|
|
|
setMergeAnonDrive(shouldImport);
|
|
|
|
|
2018-02-07 01:45:12 +08:00
|
|
|
var proceed = function (result) {
|
2018-02-08 00:14:15 +08:00
|
|
|
hashing = false;
|
2023-12-01 22:44:20 +08:00
|
|
|
if (cb && typeof cb === "function" && cb(result)) { return; }
|
2021-08-10 21:16:10 +08:00
|
|
|
LocalStore.clearLoginToken();
|
2018-02-07 01:45:12 +08:00
|
|
|
Realtime.whenRealtimeSyncs(result.realtime, function () {
|
2018-02-08 00:14:15 +08:00
|
|
|
Exports.redirect();
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
2018-04-19 20:21:26 +08:00
|
|
|
|
2018-02-07 01:45:12 +08:00
|
|
|
// We need a setTimeout(cb, 0) otherwise the loading screen is only displayed
|
|
|
|
// after hashing the password
|
|
|
|
window.setTimeout(function () {
|
2023-12-19 22:03:40 +08:00
|
|
|
Login.loginOrRegister(config, function (err, result) {
|
2023-12-01 22:44:20 +08:00
|
|
|
var proxy = {};
|
2018-02-07 01:45:12 +08:00
|
|
|
if (result) { proxy = result.proxy; }
|
|
|
|
|
|
|
|
if (err) {
|
2023-12-01 22:44:20 +08:00
|
|
|
console.warn(err);
|
2018-02-07 01:45:12 +08:00
|
|
|
switch (err) {
|
|
|
|
case 'NO_SUCH_USER':
|
|
|
|
UI.removeLoadingScreen(function () {
|
|
|
|
UI.alert(Messages.login_noSuchUser, function () {
|
|
|
|
hashing = false;
|
2020-02-11 21:03:06 +08:00
|
|
|
$('#password').focus();
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'INVAL_USER':
|
|
|
|
UI.removeLoadingScreen(function () {
|
2023-09-01 19:35:27 +08:00
|
|
|
UI.alert(Messages.login_notFilledUser , function () {
|
2018-02-07 01:45:12 +08:00
|
|
|
hashing = false;
|
2020-02-11 21:03:06 +08:00
|
|
|
$('#password').focus();
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
2023-09-14 23:49:16 +08:00
|
|
|
/*
|
|
|
|
case 'HAS_PLACEHOLDER':
|
|
|
|
UI.errorLoadingScreen('UNAVAILABLE', true, true);
|
|
|
|
break;
|
|
|
|
*/
|
2023-09-09 00:10:02 +08:00
|
|
|
case 'DELETED_USER':
|
2023-09-14 23:49:16 +08:00
|
|
|
UI.errorLoadingScreen(
|
|
|
|
UI.getDestroyedPlaceholder(result.reason, true), true, () => {
|
|
|
|
window.location.reload();
|
|
|
|
});
|
2023-09-09 00:10:02 +08:00
|
|
|
break;
|
2018-02-07 01:45:12 +08:00
|
|
|
case 'INVAL_PASS':
|
|
|
|
UI.removeLoadingScreen(function () {
|
2023-09-01 19:35:27 +08:00
|
|
|
UI.alert(Messages.login_notFilledPass, function () {
|
2018-02-07 01:45:12 +08:00
|
|
|
hashing = false;
|
2020-02-11 21:03:06 +08:00
|
|
|
$('#password').focus();
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'PASS_TOO_SHORT':
|
|
|
|
UI.removeLoadingScreen(function () {
|
|
|
|
var warning = Messages._getKey('register_passwordTooShort', [
|
|
|
|
Cred.MINIMUM_PASSWORD_LENGTH
|
|
|
|
]);
|
|
|
|
UI.alert(warning, function () {
|
|
|
|
hashing = false;
|
2020-02-11 21:03:06 +08:00
|
|
|
$('#password').focus();
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'ALREADY_REGISTERED':
|
|
|
|
UI.removeLoadingScreen(function () {
|
|
|
|
UI.confirm(Messages.register_alreadyRegistered, function (yes) {
|
2018-04-12 20:33:47 +08:00
|
|
|
if (!yes) {
|
|
|
|
hashing = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-02-07 01:45:12 +08:00
|
|
|
proxy.login_name = uname;
|
|
|
|
|
|
|
|
if (!proxy[Constants.displayNameKey]) {
|
|
|
|
proxy[Constants.displayNameKey] = uname;
|
|
|
|
}
|
2018-07-09 20:20:38 +08:00
|
|
|
|
2023-06-06 22:07:00 +08:00
|
|
|
var block = result.blockHash;
|
|
|
|
var user = block ? undefined : result.userHash;
|
|
|
|
LocalStore.login(user, block, result.userName, function () {
|
2018-06-21 16:40:09 +08:00
|
|
|
setTimeout(function () { proceed(result); });
|
|
|
|
});
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
2021-04-26 21:01:33 +08:00
|
|
|
case 'E_RESTRICTED':
|
2023-12-01 22:44:20 +08:00
|
|
|
if (token) {
|
|
|
|
return UI.errorLoadingScreen(Messages.register_invalidToken);
|
|
|
|
}
|
2021-05-04 16:15:44 +08:00
|
|
|
UI.errorLoadingScreen(Messages.register_registrationIsClosed);
|
2021-04-26 21:01:33 +08:00
|
|
|
break;
|
2018-02-07 01:45:12 +08:00
|
|
|
default: // UNHANDLED ERROR
|
|
|
|
hashing = false;
|
|
|
|
UI.errorLoadingScreen(Messages.login_unhandledError);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-18 02:31:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-21 23:01:33 +08:00
|
|
|
if (ApiConfig && Array.isArray(ApiConfig.adminKeys) &&
|
|
|
|
ApiConfig.adminKeys.includes(proxy.edPublic)) {
|
|
|
|
localStorage.CP_admin = "1";
|
|
|
|
}
|
|
|
|
|
2018-09-18 02:31:33 +08:00
|
|
|
setTimeout(function () {
|
|
|
|
Realtime.whenRealtimeSyncs(result.realtime, function () {
|
|
|
|
proceed(result);
|
|
|
|
});
|
|
|
|
});
|
2018-02-07 01:45:12 +08:00
|
|
|
});
|
2018-02-26 17:41:37 +08:00
|
|
|
}, 500);
|
2018-02-07 01:45:12 +08:00
|
|
|
}, 200);
|
|
|
|
};
|
|
|
|
|
2017-02-09 23:31:57 +08:00
|
|
|
return Exports;
|
|
|
|
});
|