Skip to content

Commit 1459bfe

Browse files
authored
Merge pull request element-hq#9892 from vector-im/travis/check-login-for-config
Use the user's pre-existing HS when config validation fails
2 parents ada47d4 + 81ec75f commit 1459bfe

2 files changed

Lines changed: 72 additions & 43 deletions

File tree

‎src/i18n/strings/en_EN.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"Unexpected error preparing the app. See console for details.": "Unexpected error preparing the app. See console for details.",
3+
"This installation of Riot seems to have an invalid server configuration. If you are the administrator, please correct the error below": "This installation of Riot seems to have an invalid server configuration. If you are the administrator, please correct the error below",
34
"Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.": "Invalid configuration: can only specify one of default_server_config, default_server_name, or default_hs_url.",
45
"Invalid configuration: no default server specified.": "Invalid configuration: no default server specified.",
56
"Riot Desktop on %(platformName)s": "Riot Desktop on %(platformName)s",

‎src/vector/index.js‎

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import * as languageHandler from 'matrix-react-sdk/lib/languageHandler';
4848
import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler';
4949
import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils';
5050
import {AutoDiscovery} from "matrix-js-sdk/lib/autodiscovery";
51+
import * as Lifecycle from "matrix-react-sdk/lib/Lifecycle";
5152

5253
import url from 'url';
5354

@@ -365,8 +366,17 @@ async function loadApp() {
365366
}).catch(err => {
366367
console.error(err);
367368

368-
const errorMessage = err.translatedMessage
369+
let errorMessage = err.translatedMessage
369370
|| _t("Unexpected error preparing the app. See console for details.");
371+
errorMessage = <span>
372+
{_t(
373+
"This installation of Riot seems to have an invalid server configuration. " +
374+
"If you are the administrator, please correct the error below",
375+
)}
376+
<br />
377+
<br />
378+
{errorMessage}
379+
</span>;
370380

371381
// Like the compatibility page, AWOOOOOGA at the user
372382
const GenericErrorPage = sdk.getComponent("structures.GenericErrorPage");
@@ -447,57 +457,75 @@ async function loadLanguage() {
447457
}
448458

449459
async function verifyServerConfig() {
450-
console.log("Verifying homeserver configuration");
451-
452-
// Note: the query string may include is_url and hs_url - we only respect these in the
453-
// context of email validation. Because we don't respect them otherwise, we do not need
454-
// to parse or consider them here.
455-
456-
const config = SdkConfig.get();
457-
let wkConfig = config['default_server_config']; // overwritten later under some conditions
458-
const serverName = config['default_server_name'];
459-
const hsUrl = config['default_hs_url'];
460-
const isUrl = config['default_is_url'];
461-
462-
const incompatibleOptions = [wkConfig, serverName, hsUrl].filter(i => !!i);
463-
if (incompatibleOptions.length > 1) {
464-
throw newTranslatableError(_td(
465-
"Invalid configuration: can only specify one of default_server_config, default_server_name, " +
466-
"or default_hs_url.",
467-
));
468-
}
469-
if (incompatibleOptions.length < 1) {
470-
throw newTranslatableError(_td("Invalid configuration: no default server specified."));
471-
}
460+
let validatedConfig;
461+
try {
462+
console.log("Verifying homeserver configuration");
463+
464+
// Note: the query string may include is_url and hs_url - we only respect these in the
465+
// context of email validation. Because we don't respect them otherwise, we do not need
466+
// to parse or consider them here.
467+
468+
const config = SdkConfig.get();
469+
let wkConfig = config['default_server_config']; // overwritten later under some conditions
470+
const serverName = config['default_server_name'];
471+
const hsUrl = config['default_hs_url'];
472+
const isUrl = config['default_is_url'];
473+
474+
const incompatibleOptions = [wkConfig, serverName, hsUrl].filter(i => !!i);
475+
if (incompatibleOptions.length > 1) {
476+
// noinspection ExceptionCaughtLocallyJS
477+
throw newTranslatableError(_td(
478+
"Invalid configuration: can only specify one of default_server_config, default_server_name, " +
479+
"or default_hs_url.",
480+
));
481+
}
482+
if (incompatibleOptions.length < 1) {
483+
// noinspection ExceptionCaughtLocallyJS
484+
throw newTranslatableError(_td("Invalid configuration: no default server specified."));
485+
}
472486

473-
if (hsUrl) {
474-
console.log("Config uses a default_hs_url - constructing a default_server_config using this information");
487+
if (hsUrl) {
488+
console.log("Config uses a default_hs_url - constructing a default_server_config using this information");
475489

476-
wkConfig = {
477-
"m.homeserver": {
478-
"base_url": hsUrl,
479-
},
480-
};
481-
if (isUrl) {
482-
wkConfig["m.identity_server"] = {
483-
"base_url": isUrl,
490+
wkConfig = {
491+
"m.homeserver": {
492+
"base_url": hsUrl,
493+
},
484494
};
495+
if (isUrl) {
496+
wkConfig["m.identity_server"] = {
497+
"base_url": isUrl,
498+
};
499+
}
485500
}
486-
}
487501

488-
let result = null;
502+
let discoveryResult = null;
503+
if (wkConfig) {
504+
console.log("Config uses a default_server_config - validating object");
505+
discoveryResult = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
506+
}
489507

490-
if (wkConfig) {
491-
console.log("Config uses a default_server_config - validating object");
492-
result = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
493-
}
508+
if (serverName) {
509+
console.log("Config uses a default_server_name - doing .well-known lookup");
510+
discoveryResult = await AutoDiscovery.findClientConfig(serverName);
511+
}
512+
513+
validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult);
514+
} catch (e) {
515+
const {hsUrl, isUrl, userId} = Lifecycle.getLocalStorageSessionVars();
516+
if (hsUrl && userId) {
517+
console.error(e);
518+
console.warn("A session was found - suppressing config error and using the session's homeserver");
494519

495-
if (serverName) {
496-
console.log("Config uses a default_server_name - doing .well-known lookup");
497-
result = await AutoDiscovery.findClientConfig(serverName);
520+
console.log("Using pre-existing hsUrl and isUrl: ", {hsUrl, isUrl});
521+
validatedConfig = await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(hsUrl, isUrl);
522+
} else {
523+
// the user is not logged in, so scream
524+
throw e;
525+
}
498526
}
499527

500-
const validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, result);
528+
501529
validatedConfig.isDefault = true;
502530

503531
// Just in case we ever have to debug this

0 commit comments

Comments
 (0)