2020-01-21 03:52:11 +01:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
Copyright 2018, 2019 New Vector Ltd
|
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-10-12 19:59:10 +02:00
|
|
|
// To ensure we load the browser-matrix version first
|
|
|
|
import "matrix-js-sdk/src/browser-index";
|
2022-03-25 05:23:17 +01:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
import React, { ReactElement } from "react";
|
|
|
|
import PlatformPeg from "matrix-react-sdk/src/PlatformPeg";
|
|
|
|
import AutoDiscoveryUtils from "matrix-react-sdk/src/utils/AutoDiscoveryUtils";
|
2023-04-25 10:36:17 +02:00
|
|
|
import { AutoDiscovery, ClientConfig } from "matrix-js-sdk/src/autodiscovery";
|
2020-01-21 03:52:11 +01:00
|
|
|
import * as Lifecycle from "matrix-react-sdk/src/Lifecycle";
|
2021-10-21 16:35:05 +02:00
|
|
|
import SdkConfig, { parseSsoRedirectOptions } from "matrix-react-sdk/src/SdkConfig";
|
2022-03-18 17:12:44 +01:00
|
|
|
import { IConfigOptions } from "matrix-react-sdk/src/IConfigOptions";
|
2021-10-15 17:04:34 +02:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-12-09 23:57:46 +01:00
|
|
|
import { createClient } from "matrix-js-sdk/src/matrix";
|
2022-03-18 17:12:44 +01:00
|
|
|
import { SnakedObject } from "matrix-react-sdk/src/utils/SnakedObject";
|
2022-03-02 22:10:42 +01:00
|
|
|
import MatrixChat from "matrix-react-sdk/src/components/structures/MatrixChat";
|
2023-04-25 10:36:17 +02:00
|
|
|
import { ValidatedServerConfig } from "matrix-react-sdk/src/utils/ValidatedServerConfig";
|
2023-08-30 08:52:41 +02:00
|
|
|
import { WrapperLifecycle, WrapperOpts } from "@matrix-org/react-sdk-module-api/lib/lifecycles/WrapperLifecycle";
|
|
|
|
import { ModuleRunner } from "matrix-react-sdk/src/modules/ModuleRunner";
|
2020-01-21 03:52:11 +01:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
import { parseQs } from "./url_utils";
|
2020-07-03 00:16:03 +02:00
|
|
|
import VectorBasePlatform from "./platform/VectorBasePlatform";
|
2023-07-11 04:19:15 +02:00
|
|
|
import { getInitialScreenAfterLogin, getScreenFromLocation, init as initRouting, onNewScreen } from "./routing";
|
2023-08-22 17:32:03 +02:00
|
|
|
import { UserFriendlyError } from "../languageHandler";
|
2021-12-09 23:57:46 +01:00
|
|
|
|
|
|
|
// add React and ReactPerf to the global namespace, to make them easier to access via the console
|
|
|
|
// this incidentally means we can forget our React imports in JSX files without penalty.
|
|
|
|
window.React = React;
|
2020-07-03 00:16:03 +02:00
|
|
|
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log(`Application is running in ${process.env.NODE_ENV} mode`);
|
2021-08-23 12:35:43 +02:00
|
|
|
|
2021-10-15 17:04:34 +02:00
|
|
|
window.matrixLogger = logger;
|
|
|
|
|
2022-11-23 17:24:36 +01:00
|
|
|
function onTokenLoginCompleted(): void {
|
2020-01-21 03:52:11 +01:00
|
|
|
// if we did a token login, we're now left with the token, hs and is
|
2023-06-28 01:27:45 +02:00
|
|
|
// url as query params in the url;
|
|
|
|
// if we did an oidc authorization code flow login, we're left with the auth code and state
|
|
|
|
// as query params in the url;
|
|
|
|
// a little nasty but let's redirect to clear them.
|
2021-02-11 15:47:50 +01:00
|
|
|
const url = new URL(window.location.href);
|
2021-02-09 10:08:40 +01:00
|
|
|
|
2021-02-11 17:27:55 +01:00
|
|
|
url.searchParams.delete("loginToken");
|
2023-06-28 01:27:45 +02:00
|
|
|
url.searchParams.delete("state");
|
|
|
|
url.searchParams.delete("code");
|
2021-02-09 10:08:40 +01:00
|
|
|
|
2023-06-28 01:27:45 +02:00
|
|
|
logger.log(`Redirecting to ${url.href} to drop delegated authentication params from queryparams`);
|
2021-02-11 15:47:50 +01:00
|
|
|
window.history.replaceState(null, "", url.href);
|
2020-01-21 03:52:11 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 03:13:30 +02:00
|
|
|
export async function loadApp(fragParams: {}, matrixChatRef: React.Ref<MatrixChat>): Promise<ReactElement> {
|
2022-03-16 13:20:06 +01:00
|
|
|
initRouting();
|
2020-01-21 03:52:11 +01:00
|
|
|
const platform = PlatformPeg.get();
|
|
|
|
|
|
|
|
const params = parseQs(window.location);
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
const urlWithoutQuery = window.location.protocol + "//" + window.location.host + window.location.pathname;
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Vector starting at " + urlWithoutQuery);
|
2020-04-08 22:41:22 +02:00
|
|
|
|
2020-07-03 00:16:03 +02:00
|
|
|
(platform as VectorBasePlatform).startUpdater();
|
2020-04-08 22:41:22 +02:00
|
|
|
|
2020-04-09 11:32:04 +02:00
|
|
|
// Don't bother loading the app until the config is verified
|
|
|
|
const config = await verifyServerConfig();
|
2022-03-18 17:12:44 +01:00
|
|
|
const snakedConfig = new SnakedObject<IConfigOptions>(config);
|
2021-04-07 03:39:26 +02:00
|
|
|
|
|
|
|
// Before we continue, let's see if we're supposed to do an SSO redirect
|
|
|
|
const [userId] = await Lifecycle.getStoredSessionOwner();
|
|
|
|
const hasPossibleToken = !!userId;
|
|
|
|
const isReturningFromSso = !!params.loginToken;
|
2021-10-21 16:35:05 +02:00
|
|
|
const ssoRedirects = parseSsoRedirectOptions(config);
|
|
|
|
let autoRedirect = ssoRedirects.immediate === true;
|
|
|
|
// XXX: This path matching is a bit brittle, but better to do it early instead of in the app code.
|
2023-06-01 12:56:30 +02:00
|
|
|
const isWelcomeOrLanding =
|
|
|
|
window.location.hash === "#/welcome" || window.location.hash === "#" || window.location.hash === "";
|
2021-10-21 16:35:05 +02:00
|
|
|
if (!autoRedirect && ssoRedirects.on_welcome_page && isWelcomeOrLanding) {
|
|
|
|
autoRedirect = true;
|
|
|
|
}
|
2021-04-07 03:39:26 +02:00
|
|
|
if (!hasPossibleToken && !isReturningFromSso && autoRedirect) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Bypassing app load to redirect to SSO");
|
2021-04-07 03:39:26 +02:00
|
|
|
const tempCli = createClient({
|
2023-04-25 10:36:17 +02:00
|
|
|
baseUrl: config.validated_server_config!.hsUrl,
|
|
|
|
idBaseUrl: config.validated_server_config!.isUrl,
|
2021-04-07 03:39:26 +02:00
|
|
|
});
|
2023-04-25 10:36:17 +02:00
|
|
|
PlatformPeg.get()!.startSingleSignOn(tempCli, "sso", `/${getScreenFromLocation(window.location).screen}`);
|
2021-04-07 03:43:24 +02:00
|
|
|
|
|
|
|
// We return here because startSingleSignOn() will asynchronously redirect us. We don't
|
|
|
|
// care to wait for it, and don't want to show any UI while we wait (not even half a welcome
|
|
|
|
// page). As such, just don't even bother loading the MatrixChat component.
|
2023-04-25 10:36:17 +02:00
|
|
|
return <React.Fragment />;
|
2021-04-07 03:39:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-25 10:36:17 +02:00
|
|
|
const defaultDeviceName =
|
|
|
|
snakedConfig.get("default_device_display_name") ?? platform?.getDefaultDeviceDisplayName();
|
2022-12-09 13:28:29 +01:00
|
|
|
|
2023-07-11 04:19:15 +02:00
|
|
|
const initialScreenAfterLogin = getInitialScreenAfterLogin(window.location);
|
|
|
|
|
2023-08-30 08:52:41 +02:00
|
|
|
const wrapperOpts: WrapperOpts = { Wrapper: React.Fragment };
|
|
|
|
ModuleRunner.instance.invoke(WrapperLifecycle.Wrapper, wrapperOpts);
|
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
return (
|
2023-08-30 08:52:41 +02:00
|
|
|
<wrapperOpts.Wrapper>
|
|
|
|
<MatrixChat
|
2023-10-19 03:13:30 +02:00
|
|
|
ref={matrixChatRef}
|
2023-08-30 08:52:41 +02:00
|
|
|
onNewScreen={onNewScreen}
|
|
|
|
config={config}
|
|
|
|
realQueryParams={params}
|
|
|
|
startingFragmentQueryParams={fragParams}
|
|
|
|
enableGuest={!config.disable_guests}
|
|
|
|
onTokenLoginCompleted={onTokenLoginCompleted}
|
|
|
|
initialScreenAfterLogin={initialScreenAfterLogin}
|
|
|
|
defaultDeviceDisplayName={defaultDeviceName}
|
|
|
|
/>
|
|
|
|
</wrapperOpts.Wrapper>
|
2022-12-09 13:28:29 +01:00
|
|
|
);
|
2020-01-21 03:52:11 +01:00
|
|
|
}
|
|
|
|
|
2022-03-02 22:10:42 +01:00
|
|
|
async function verifyServerConfig(): Promise<IConfigOptions> {
|
2023-04-25 10:36:17 +02:00
|
|
|
let validatedConfig: ValidatedServerConfig;
|
2020-01-21 03:52:11 +01:00
|
|
|
try {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Verifying homeserver configuration");
|
2020-01-21 03:52:11 +01:00
|
|
|
|
|
|
|
// Note: the query string may include is_url and hs_url - we only respect these in the
|
|
|
|
// context of email validation. Because we don't respect them otherwise, we do not need
|
|
|
|
// to parse or consider them here.
|
|
|
|
|
|
|
|
// Note: Although we throw all 3 possible configuration options through a .well-known-style
|
|
|
|
// verification, we do not care if the servers are online at this point. We do moderately
|
|
|
|
// care if they are syntactically correct though, so we shove them through the .well-known
|
|
|
|
// validators for that purpose.
|
|
|
|
|
|
|
|
const config = SdkConfig.get();
|
2022-12-09 13:28:29 +01:00
|
|
|
let wkConfig = config["default_server_config"]; // overwritten later under some conditions
|
|
|
|
const serverName = config["default_server_name"];
|
|
|
|
const hsUrl = config["default_hs_url"];
|
|
|
|
const isUrl = config["default_is_url"];
|
2020-01-21 03:52:11 +01:00
|
|
|
|
2022-12-09 13:28:29 +01:00
|
|
|
const incompatibleOptions = [wkConfig, serverName, hsUrl].filter((i) => !!i);
|
2023-07-10 10:56:24 +02:00
|
|
|
if (hsUrl && (wkConfig || serverName)) {
|
2020-01-21 03:52:11 +01:00
|
|
|
// noinspection ExceptionCaughtLocallyJS
|
2023-09-05 18:17:25 +02:00
|
|
|
throw new UserFriendlyError("error|invalid_configuration_mixed_server");
|
2020-01-21 03:52:11 +01:00
|
|
|
}
|
|
|
|
if (incompatibleOptions.length < 1) {
|
|
|
|
// noinspection ExceptionCaughtLocallyJS
|
2023-09-05 18:17:25 +02:00
|
|
|
throw new UserFriendlyError("error|invalid_configuration_no_server");
|
2020-01-21 03:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hsUrl) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Config uses a default_hs_url - constructing a default_server_config using this information");
|
2021-10-15 17:00:43 +02:00
|
|
|
logger.warn(
|
2020-01-21 03:52:11 +01:00
|
|
|
"DEPRECATED CONFIG OPTION: In the future, default_hs_url will not be accepted. Please use " +
|
2022-12-09 13:28:29 +01:00
|
|
|
"default_server_config instead.",
|
2020-01-21 03:52:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
wkConfig = {
|
|
|
|
"m.homeserver": {
|
2022-12-09 13:28:29 +01:00
|
|
|
base_url: hsUrl,
|
2020-01-21 03:52:11 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
if (isUrl) {
|
|
|
|
wkConfig["m.identity_server"] = {
|
2022-12-09 13:28:29 +01:00
|
|
|
base_url: isUrl,
|
2020-01-21 03:52:11 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 10:36:17 +02:00
|
|
|
let discoveryResult: ClientConfig | undefined;
|
2023-07-10 10:56:24 +02:00
|
|
|
if (!serverName && wkConfig) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Config uses a default_server_config - validating object");
|
2020-01-21 03:52:11 +01:00
|
|
|
discoveryResult = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverName) {
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Config uses a default_server_name - doing .well-known lookup");
|
2021-10-15 17:00:43 +02:00
|
|
|
logger.warn(
|
2020-01-21 03:52:11 +01:00
|
|
|
"DEPRECATED CONFIG OPTION: In the future, default_server_name will not be accepted. Please " +
|
2022-12-09 13:28:29 +01:00
|
|
|
"use default_server_config instead.",
|
2020-01-21 03:52:11 +01:00
|
|
|
);
|
|
|
|
discoveryResult = await AutoDiscovery.findClientConfig(serverName);
|
2023-07-10 10:56:24 +02:00
|
|
|
if (discoveryResult["m.homeserver"].base_url === null && wkConfig) {
|
|
|
|
logger.log("Finding base_url failed but a default_server_config was found - using it as a fallback");
|
|
|
|
discoveryResult = await AutoDiscovery.fromDiscoveryConfig(wkConfig);
|
|
|
|
}
|
2020-01-21 03:52:11 +01:00
|
|
|
}
|
|
|
|
|
2024-02-23 17:43:07 +01:00
|
|
|
validatedConfig = await AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(serverName, discoveryResult, true);
|
2020-01-21 03:52:11 +01:00
|
|
|
} catch (e) {
|
2021-06-30 14:28:31 +02:00
|
|
|
const { hsUrl, isUrl, userId } = await Lifecycle.getStoredSessionVars();
|
2020-01-21 03:52:11 +01:00
|
|
|
if (hsUrl && userId) {
|
2021-10-15 16:56:22 +02:00
|
|
|
logger.error(e);
|
2021-10-15 17:00:43 +02:00
|
|
|
logger.warn("A session was found - suppressing config error and using the session's homeserver");
|
2020-01-21 03:52:11 +01:00
|
|
|
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using pre-existing hsUrl and isUrl: ", { hsUrl, isUrl });
|
2020-01-21 03:52:11 +01:00
|
|
|
validatedConfig = await AutoDiscoveryUtils.validateServerConfigWithStaticUrls(hsUrl, isUrl, true);
|
|
|
|
} else {
|
|
|
|
// the user is not logged in, so scream
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validatedConfig.isDefault = true;
|
|
|
|
|
|
|
|
// Just in case we ever have to debug this
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Using homeserver config:", validatedConfig);
|
2020-01-21 03:52:11 +01:00
|
|
|
|
|
|
|
// Add the newly built config to the actual config for use by the app
|
2021-10-15 16:59:13 +02:00
|
|
|
logger.log("Updating SdkConfig with validated discovery information");
|
2022-12-09 13:28:29 +01:00
|
|
|
SdkConfig.add({ validated_server_config: validatedConfig });
|
2020-01-21 03:52:11 +01:00
|
|
|
|
|
|
|
return SdkConfig.get();
|
|
|
|
}
|