cryptpad/customize.dist/login.js

240 lines
9.6 KiB
JavaScript
Raw Normal View History

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([
'jquery',
'chainpad-listmap',
'/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',
'/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-09 00:10:02 +08:00
'/common/hyperscript.js',
'/common/outer/local-store.js',
'/customize/messages.js',
'/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',
'/common/outer/http-command.js',
'/api/config',
2017-11-24 22:12:00 +08:00
'/components/tweetnacl/nacl-fast.min.js',
'/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,
Feedback, h, LocalStore, Messages, nThen, Block, Hash, ServerCommand,
ApiConfig) {
2017-02-09 23:31:57 +08:00
var Exports = {
Cred: Cred,
Block: Block,
// 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
};
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 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 () {
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
var hashing;
Exports.loginOrRegisterUI = function (config) {
let { uname, token, shouldImport, cb } = config;
if (hashing) { return void console.log("hashing is already in progress"); }
hashing = true;
2023-06-27 22:04:32 +08:00
setMergeAnonDrive(shouldImport);
var proceed = function (result) {
2018-02-08 00:14:15 +08:00
hashing = false;
if (cb && typeof cb === "function" && cb(result)) { return; }
LocalStore.clearLoginToken();
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 () {
Login.loginOrRegister(config, function (err, result) {
var proxy = {};
if (result) { proxy = result.proxy; }
if (err) {
console.warn(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-09 00:10:02 +08:00
case 'DELETED_USER':
UI.errorLoadingScreen(
UI.getDestroyedPlaceholder(result.reason, true), true, () => {
window.location.reload();
});
2023-09-09 00:10:02 +08: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 16:40:09 +08:00
setTimeout(function () { proceed(result); });
});
});
});
break;
2021-04-26 21:01:33 +08:00
case 'E_RESTRICTED':
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;
default: // UNHANDLED ERROR
hashing = false;
UI.errorLoadingScreen(Messages.login_unhandledError);
}
return;
}
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;
}
if (ApiConfig && Array.isArray(ApiConfig.adminKeys) &&
ApiConfig.adminKeys.includes(proxy.edPublic)) {
localStorage.CP_admin = "1";
}
setTimeout(function () {
Realtime.whenRealtimeSyncs(result.realtime, function () {
proceed(result);
});
});
});
}, 500);
}, 200);
};
2017-02-09 23:31:57 +08:00
return Exports;
});