|
1 | 1 | /* |
2 | 2 | Copyright 2015, 2016 OpenMarket Ltd |
3 | 3 | Copyright 2017 Vector Creations Ltd |
4 | | -Copyright 2018 New Vector Ltd |
| 4 | +Copyright 2018, 2019 New Vector Ltd |
5 | 5 |
|
6 | 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | you may not use this file except in compliance with the License. |
@@ -45,6 +45,9 @@ import VectorConferenceHandler from 'matrix-react-sdk/lib/VectorConferenceHandle |
45 | 45 | import Promise from 'bluebird'; |
46 | 46 | import request from 'browser-request'; |
47 | 47 | import * as languageHandler from 'matrix-react-sdk/lib/languageHandler'; |
| 48 | +import {_t, _td, newTranslatableError} from 'matrix-react-sdk/lib/languageHandler'; |
| 49 | +import AutoDiscoveryUtils from 'matrix-react-sdk/lib/utils/AutoDiscoveryUtils'; |
| 50 | +import {AutoDiscovery} from "matrix-js-sdk/lib/autodiscovery"; |
48 | 51 |
|
49 | 52 | import url from 'url'; |
50 | 53 |
|
@@ -341,22 +344,37 @@ async function loadApp() { |
341 | 344 | const platform = PlatformPeg.get(); |
342 | 345 | platform.startUpdater(); |
343 | 346 |
|
344 | | - const MatrixChat = sdk.getComponent('structures.MatrixChat'); |
345 | | - window.matrixChat = ReactDOM.render( |
346 | | - <MatrixChat |
347 | | - onNewScreen={onNewScreen} |
348 | | - makeRegistrationUrl={makeRegistrationUrl} |
349 | | - ConferenceHandler={VectorConferenceHandler} |
350 | | - config={configJson} |
351 | | - realQueryParams={params} |
352 | | - startingFragmentQueryParams={fragparts.params} |
353 | | - enableGuest={!configJson.disable_guests} |
354 | | - onTokenLoginCompleted={onTokenLoginCompleted} |
355 | | - initialScreenAfterLogin={getScreenFromLocation(window.location)} |
356 | | - defaultDeviceDisplayName={platform.getDefaultDeviceDisplayName()} |
357 | | - />, |
358 | | - document.getElementById('matrixchat'), |
359 | | - ); |
| 347 | + // Don't bother loading the app until the config is verified |
| 348 | + verifyServerConfig().then((newConfig) => { |
| 349 | + const MatrixChat = sdk.getComponent('structures.MatrixChat'); |
| 350 | + window.matrixChat = ReactDOM.render( |
| 351 | + <MatrixChat |
| 352 | + onNewScreen={onNewScreen} |
| 353 | + makeRegistrationUrl={makeRegistrationUrl} |
| 354 | + ConferenceHandler={VectorConferenceHandler} |
| 355 | + config={newConfig} |
| 356 | + realQueryParams={params} |
| 357 | + startingFragmentQueryParams={fragparts.params} |
| 358 | + enableGuest={!configJson.disable_guests} |
| 359 | + onTokenLoginCompleted={onTokenLoginCompleted} |
| 360 | + initialScreenAfterLogin={getScreenFromLocation(window.location)} |
| 361 | + defaultDeviceDisplayName={platform.getDefaultDeviceDisplayName()} |
| 362 | + />, |
| 363 | + document.getElementById('matrixchat'), |
| 364 | + ); |
| 365 | + }).catch(err => { |
| 366 | + console.error(err); |
| 367 | + |
| 368 | + const errorMessage = err.translatedMessage |
| 369 | + || _t("Unexpected error preparing the app. See console for details."); |
| 370 | + |
| 371 | + // Like the compatibility page, AWOOOOOGA at the user |
| 372 | + const GenericErrorPage = sdk.getComponent("structures.GenericErrorPage"); |
| 373 | + window.matrixChat = ReactDOM.render( |
| 374 | + <GenericErrorPage message={errorMessage} />, |
| 375 | + document.getElementById('matrixchat'), |
| 376 | + ); |
| 377 | + }); |
360 | 378 | } else { |
361 | 379 | console.error("Browser is missing required features."); |
362 | 380 | // take to a different landing page to AWOOOOOGA at the user |
@@ -428,4 +446,68 @@ async function loadLanguage() { |
428 | 446 | } |
429 | 447 | } |
430 | 448 |
|
| 449 | +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 | + } |
| 472 | + |
| 473 | + if (hsUrl) { |
| 474 | + console.log("Config uses a default_hs_url - constructing a default_server_config using this information"); |
| 475 | + |
| 476 | + wkConfig = { |
| 477 | + "m.homeserver": { |
| 478 | + "base_url": hsUrl, |
| 479 | + }, |
| 480 | + }; |
| 481 | + if (isUrl) { |
| 482 | + wkConfig["m.identity_server"] = { |
| 483 | + "base_url": isUrl, |
| 484 | + }; |
| 485 | + } |
| 486 | + } |
| 487 | + |
| 488 | + let result = null; |
| 489 | + |
| 490 | + if (wkConfig) { |
| 491 | + console.log("Config uses a default_server_config - validating object"); |
| 492 | + result = await AutoDiscovery.fromDiscoveryConfig(wkConfig); |
| 493 | + } |
| 494 | + |
| 495 | + if (serverName) { |
| 496 | + console.log("Config uses a default_server_name - doing .well-known lookup"); |
| 497 | + result = await AutoDiscovery.findClientConfig(serverName); |
| 498 | + } |
| 499 | + |
| 500 | + const validatedConfig = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, result); |
| 501 | + validatedConfig.isDefault = true; |
| 502 | + |
| 503 | + // Just in case we ever have to debug this |
| 504 | + console.log("Using homeserver config:", validatedConfig); |
| 505 | + |
| 506 | + // Add the newly built config to the actual config for use by the app |
| 507 | + console.log("Updating SdkConfig with validated discovery information"); |
| 508 | + SdkConfig.add({"validated_server_config": validatedConfig}); |
| 509 | + |
| 510 | + return SdkConfig.get(); |
| 511 | +} |
| 512 | + |
431 | 513 | loadApp(); |
0 commit comments