2015-06-23 15:41:25 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-06-21 13:04:43 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd.
|
2019-12-05 15:20:30 +00:00
|
|
|
Copyright 2017, 2018, 2019 New Vector Ltd
|
2020-01-15 21:13:56 +00:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2015-06-23 15:41:25 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-10-09 15:59:56 +00:00
|
|
|
import { ICreateClientOpts } from 'matrix-js-sdk/src/matrix';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk/src/client';
|
|
|
|
import { MemoryStore } from 'matrix-js-sdk/src/store/memory';
|
2019-12-20 02:08:43 +00:00
|
|
|
import * as utils from 'matrix-js-sdk/src/utils';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { EventTimeline } from 'matrix-js-sdk/src/models/event-timeline';
|
|
|
|
import { EventTimelineSet } from 'matrix-js-sdk/src/models/event-timeline-set';
|
2021-07-02 16:00:07 +00:00
|
|
|
import * as sdk from './index';
|
2017-06-13 11:46:49 +00:00
|
|
|
import createMatrixClient from './utils/createMatrixClient';
|
2017-11-17 17:48:42 +00:00
|
|
|
import SettingsStore from './settings/SettingsStore';
|
2017-12-07 17:10:45 +00:00
|
|
|
import MatrixActionCreators from './actions/MatrixActionCreators';
|
2018-11-16 11:31:46 +00:00
|
|
|
import Modal from './Modal';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { verificationMethods } from 'matrix-js-sdk/src/crypto';
|
2019-02-22 23:33:20 +00:00
|
|
|
import MatrixClientBackedSettingsHandler from "./settings/handlers/MatrixClientBackedSettingsHandler";
|
2019-04-04 10:59:53 +00:00
|
|
|
import * as StorageManager from './utils/StorageManager';
|
2019-08-22 13:44:09 +00:00
|
|
|
import IdentityAuthClient from './IdentityAuthClient';
|
2020-10-02 00:23:12 +00:00
|
|
|
import { crossSigningCallbacks, tryToUnlockSecretStorageWithDehydrationKey } from './SecurityManager';
|
2021-06-29 12:11:58 +00:00
|
|
|
import { SHOW_QR_CODE_METHOD } from "matrix-js-sdk/src/crypto/verification/QRCode";
|
2020-11-04 15:51:45 +00:00
|
|
|
import SecurityCustomisations from "./customisations/Security";
|
2015-07-20 12:19:47 +00:00
|
|
|
|
2020-05-25 21:59:15 +00:00
|
|
|
export interface IMatrixClientCreds {
|
2020-06-18 13:32:43 +00:00
|
|
|
homeserverUrl: string;
|
|
|
|
identityServerUrl: string;
|
|
|
|
userId: string;
|
2020-10-07 11:14:36 +00:00
|
|
|
deviceId?: string;
|
2020-06-18 13:32:43 +00:00
|
|
|
accessToken: string;
|
2020-10-06 11:42:28 +00:00
|
|
|
guest?: boolean;
|
2020-06-18 13:32:43 +00:00
|
|
|
pickleKey?: string;
|
2020-09-30 04:52:47 +00:00
|
|
|
freshLogin?: boolean;
|
2016-08-03 09:46:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
// TODO: Move this to the js-sdk
|
2020-05-25 22:06:05 +00:00
|
|
|
export interface IOpts {
|
2020-05-25 21:52:05 +00:00
|
|
|
initialSyncLimit?: number;
|
|
|
|
pendingEventOrdering?: "detached" | "chronological";
|
|
|
|
lazyLoadMembers?: boolean;
|
2020-06-01 21:22:01 +00:00
|
|
|
clientWellKnownPollPeriod?: number;
|
2020-05-25 21:52:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
export interface IMatrixClientPeg {
|
|
|
|
opts: IOpts;
|
2017-04-06 10:13:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the script href passed to the IndexedDB web worker
|
|
|
|
* If set, a separate web worker will be started to run the IndexedDB
|
|
|
|
* queries on.
|
|
|
|
*
|
|
|
|
* @param {string} script href to the script to be passed to the web worker
|
|
|
|
*/
|
2020-05-25 22:06:05 +00:00
|
|
|
setIndexedDbWorkerScript(script: string): void;
|
2015-12-04 10:37:53 +00:00
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
/**
|
|
|
|
* Return the server name of the user's homeserver
|
|
|
|
* Throws an error if unable to deduce the homeserver name
|
|
|
|
* (eg. if the user is not logged in)
|
|
|
|
*
|
|
|
|
* @returns {string} The homeserver name, if present.
|
|
|
|
*/
|
|
|
|
getHomeserverName(): string;
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
get(): MatrixClient;
|
|
|
|
unset(): void;
|
|
|
|
assign(): Promise<any>;
|
|
|
|
start(): Promise<any>;
|
2017-12-07 17:10:45 +00:00
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
getCredentials(): IMatrixClientCreds;
|
2015-09-16 12:48:24 +00:00
|
|
|
|
2019-09-18 08:27:43 +00:00
|
|
|
/**
|
2019-06-14 14:31:19 +00:00
|
|
|
* If we've registered a user ID we set this to the ID of the
|
|
|
|
* user we've just registered. If they then go & log in, we
|
|
|
|
* can send them to the welcome user (obviously this doesn't
|
|
|
|
* guarentee they'll get a chat with the welcome user).
|
|
|
|
*
|
|
|
|
* @param {string} uid The user ID of the user we've just registered
|
|
|
|
*/
|
2020-05-25 22:06:05 +00:00
|
|
|
setJustRegisteredUserId(uid: string): void;
|
2019-06-14 14:31:19 +00:00
|
|
|
|
2019-09-18 08:27:43 +00:00
|
|
|
/**
|
2019-06-14 14:31:19 +00:00
|
|
|
* Returns true if the current user has just been registered by this
|
|
|
|
* client as determined by setJustRegisteredUserId()
|
|
|
|
*
|
|
|
|
* @returns {bool} True if user has just been registered
|
|
|
|
*/
|
2020-05-25 22:06:05 +00:00
|
|
|
currentUserIsJustRegistered(): boolean;
|
|
|
|
|
2020-11-02 17:25:48 +00:00
|
|
|
/**
|
|
|
|
* If the current user has been registered by this device then this
|
|
|
|
* returns a boolean of whether it was within the last N hours given.
|
|
|
|
*/
|
|
|
|
userRegisteredWithinLastHours(hours: number): boolean;
|
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
/**
|
|
|
|
* Replace this MatrixClientPeg's client with a client instance that has
|
|
|
|
* homeserver / identity server URLs and active credentials
|
|
|
|
*
|
|
|
|
* @param {IMatrixClientCreds} creds The new credentials to use.
|
|
|
|
*/
|
|
|
|
replaceUsingCreds(creds: IMatrixClientCreds): void;
|
|
|
|
}
|
|
|
|
|
2016-07-22 14:47:47 +00:00
|
|
|
/**
|
|
|
|
* Wrapper object for handling the js-sdk Matrix Client object in the react-sdk
|
|
|
|
* Handles the creation/initialisation of client objects.
|
|
|
|
* This module provides a singleton instance of this class so the 'current'
|
|
|
|
* Matrix Client object is available easily.
|
|
|
|
*/
|
2020-05-25 22:06:05 +00:00
|
|
|
class _MatrixClientPeg implements IMatrixClientPeg {
|
2020-05-25 21:52:05 +00:00
|
|
|
// These are the default options used when when the
|
|
|
|
// client is started in 'start'. These can be altered
|
|
|
|
// at any time up to after the 'will_start_client'
|
|
|
|
// event is finished processing.
|
2020-05-25 21:59:15 +00:00
|
|
|
public opts: IOpts = {
|
2020-05-25 21:52:05 +00:00
|
|
|
initialSyncLimit: 20,
|
|
|
|
};
|
|
|
|
|
2020-05-25 22:14:51 +00:00
|
|
|
private matrixClient: MatrixClient = null;
|
2020-05-25 21:59:15 +00:00
|
|
|
private justRegisteredUserId: string;
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
// the credentials used to init the current client object.
|
|
|
|
// used if we tear it down & recreate it with a different store
|
2020-05-25 21:59:15 +00:00
|
|
|
private currentClientCreds: IMatrixClientCreds;
|
2020-05-25 21:52:05 +00:00
|
|
|
|
2016-07-22 14:47:47 +00:00
|
|
|
constructor() {
|
2017-04-06 10:13:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public setIndexedDbWorkerScript(script: string): void {
|
2017-06-21 16:43:34 +00:00
|
|
|
createMatrixClient.indexedDbWorkerScript = script;
|
2015-12-04 10:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public get(): MatrixClient {
|
2016-07-21 16:57:55 +00:00
|
|
|
return this.matrixClient;
|
2015-09-28 16:46:49 +00:00
|
|
|
}
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public unset(): void {
|
2016-07-21 16:57:55 +00:00
|
|
|
this.matrixClient = null;
|
2017-12-07 17:10:45 +00:00
|
|
|
|
|
|
|
MatrixActionCreators.stop();
|
2015-09-28 16:46:49 +00:00
|
|
|
}
|
2015-09-16 12:48:24 +00:00
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public setJustRegisteredUserId(uid: string): void {
|
|
|
|
this.justRegisteredUserId = uid;
|
2020-11-02 17:25:48 +00:00
|
|
|
if (uid) {
|
|
|
|
window.localStorage.setItem("mx_registration_time", String(new Date().getTime()));
|
|
|
|
}
|
2019-06-14 14:31:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public currentUserIsJustRegistered(): boolean {
|
2019-06-14 14:31:19 +00:00
|
|
|
return (
|
|
|
|
this.matrixClient &&
|
2020-05-25 21:52:05 +00:00
|
|
|
this.matrixClient.credentials.userId === this.justRegisteredUserId
|
2019-06-14 14:31:19 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-02 17:25:48 +00:00
|
|
|
public userRegisteredWithinLastHours(hours: number): boolean {
|
|
|
|
try {
|
|
|
|
const date = new Date(window.localStorage.getItem("mx_registration_time"));
|
|
|
|
return ((new Date().getTime() - date.getTime()) / 36e5) <= hours;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:59:15 +00:00
|
|
|
public replaceUsingCreds(creds: IMatrixClientCreds): void {
|
2020-05-25 21:52:05 +00:00
|
|
|
this.currentClientCreds = creds;
|
2020-05-25 22:14:51 +00:00
|
|
|
this.createClient(creds);
|
2016-07-25 15:28:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public async assign(): Promise<any> {
|
2018-10-04 12:40:56 +00:00
|
|
|
for (const dbType of ['indexeddb', 'memory']) {
|
|
|
|
try {
|
|
|
|
const promise = this.matrixClient.store.startup();
|
|
|
|
console.log("MatrixClientPeg: waiting for MatrixClient store to initialise");
|
|
|
|
await promise;
|
|
|
|
break;
|
|
|
|
} catch (err) {
|
|
|
|
if (dbType === 'indexeddb') {
|
|
|
|
console.error('Error starting matrixclient store - falling back to memory store', err);
|
2019-09-18 08:27:43 +00:00
|
|
|
this.matrixClient.store = new MemoryStore({
|
2020-05-25 21:52:05 +00:00
|
|
|
localStorage: localStorage,
|
2018-10-04 12:40:56 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error('Failed to start memory store!', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 10:38:27 +00:00
|
|
|
StorageManager.trackStores(this.matrixClient);
|
|
|
|
|
2017-07-18 22:46:03 +00:00
|
|
|
// try to initialise e2e on the new client
|
|
|
|
try {
|
|
|
|
// check that we have a version of the js-sdk which includes initCrypto
|
2019-02-08 16:44:03 +00:00
|
|
|
if (!SettingsStore.getValue("lowBandwidth") && this.matrixClient.initCrypto) {
|
2017-07-18 22:46:03 +00:00
|
|
|
await this.matrixClient.initCrypto();
|
2020-03-25 18:38:12 +00:00
|
|
|
this.matrixClient.setCryptoTrustCrossSignedDevices(
|
|
|
|
!SettingsStore.getValue('e2ee.manuallyVerifyAllSessions'),
|
|
|
|
);
|
2020-10-02 00:23:12 +00:00
|
|
|
await tryToUnlockSecretStorageWithDehydrationKey(this.matrixClient);
|
2019-05-15 12:47:48 +00:00
|
|
|
StorageManager.setCryptoInitialised(true);
|
2017-07-18 22:46:03 +00:00
|
|
|
}
|
2017-11-16 13:19:36 +00:00
|
|
|
} catch (e) {
|
2019-04-02 11:31:18 +00:00
|
|
|
if (e && e.name === 'InvalidCryptoStoreError') {
|
2018-11-16 11:31:46 +00:00
|
|
|
// The js-sdk found a crypto DB too new for it to use
|
2021-07-02 16:00:07 +00:00
|
|
|
// FIXME: Using an import will result in test failures
|
|
|
|
const CryptoStoreTooNewDialog =
|
|
|
|
sdk.getComponent("views.dialogs.CryptoStoreTooNewDialog");
|
2020-05-25 09:59:31 +00:00
|
|
|
Modal.createDialog(CryptoStoreTooNewDialog);
|
2018-11-16 11:31:46 +00:00
|
|
|
}
|
2017-07-18 22:46:03 +00:00
|
|
|
// this can happen for a number of reasons, the most likely being
|
|
|
|
// that the olm library was missing. It's not fatal.
|
2019-04-02 11:31:18 +00:00
|
|
|
console.warn("Unable to initialise e2e", e);
|
2017-07-18 22:46:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 15:45:23 +00:00
|
|
|
const opts = utils.deepCopy(this.opts);
|
2016-08-03 16:23:09 +00:00
|
|
|
// the react sdk doesn't work without this, so don't allow
|
2016-08-03 15:39:47 +00:00
|
|
|
opts.pendingEventOrdering = "detached";
|
2019-02-07 18:24:07 +00:00
|
|
|
opts.lazyLoadMembers = true;
|
2020-06-03 11:25:57 +00:00
|
|
|
opts.clientWellKnownPollPeriod = 2 * 60 * 60; // 2 hours
|
2017-02-02 14:27:27 +00:00
|
|
|
|
2019-02-22 23:33:20 +00:00
|
|
|
// Connect the matrix client to the dispatcher and setting handlers
|
2017-12-07 17:10:45 +00:00
|
|
|
MatrixActionCreators.start(this.matrixClient);
|
2019-02-22 23:33:20 +00:00
|
|
|
MatrixClientBackedSettingsHandler.matrixClient = this.matrixClient;
|
2017-12-07 17:10:45 +00:00
|
|
|
|
2019-07-04 22:45:40 +00:00
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public async start(): Promise<any> {
|
2019-07-04 22:45:40 +00:00
|
|
|
const opts = await this.assign();
|
|
|
|
|
2017-07-05 15:20:21 +00:00
|
|
|
console.log(`MatrixClientPeg: really starting MatrixClient`);
|
2018-08-02 17:38:41 +00:00
|
|
|
await this.get().startClient(opts);
|
2017-07-05 15:20:21 +00:00
|
|
|
console.log(`MatrixClientPeg: MatrixClient started`);
|
2016-08-03 15:41:22 +00:00
|
|
|
}
|
2016-08-03 15:39:47 +00:00
|
|
|
|
2020-05-25 21:59:15 +00:00
|
|
|
public getCredentials(): IMatrixClientCreds {
|
2016-08-02 13:04:20 +00:00
|
|
|
return {
|
|
|
|
homeserverUrl: this.matrixClient.baseUrl,
|
|
|
|
identityServerUrl: this.matrixClient.idBaseUrl,
|
|
|
|
userId: this.matrixClient.credentials.userId,
|
2016-08-11 13:21:52 +00:00
|
|
|
deviceId: this.matrixClient.getDeviceId(),
|
2016-08-02 13:04:20 +00:00
|
|
|
accessToken: this.matrixClient.getAccessToken(),
|
|
|
|
guest: this.matrixClient.isGuest(),
|
|
|
|
};
|
2016-07-21 16:57:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:05 +00:00
|
|
|
public getHomeserverName(): string {
|
2021-03-15 22:13:16 +00:00
|
|
|
const matches = /^@[^:]+:(.+)$/.exec(this.matrixClient.credentials.userId);
|
2016-09-27 18:38:10 +00:00
|
|
|
if (matches === null || matches.length < 1) {
|
2019-02-01 00:52:39 +00:00
|
|
|
throw new Error("Failed to derive homeserver name from user ID!");
|
2016-09-27 18:38:10 +00:00
|
|
|
}
|
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:14:51 +00:00
|
|
|
private createClient(creds: IMatrixClientCreds): void {
|
2020-10-09 15:59:56 +00:00
|
|
|
const opts: ICreateClientOpts = {
|
2016-08-11 12:50:38 +00:00
|
|
|
baseUrl: creds.homeserverUrl,
|
|
|
|
idBaseUrl: creds.identityServerUrl,
|
|
|
|
accessToken: creds.accessToken,
|
2020-09-30 04:52:47 +00:00
|
|
|
userId: creds.userId,
|
|
|
|
deviceId: creds.deviceId,
|
2020-05-28 04:05:45 +00:00
|
|
|
pickleKey: creds.pickleKey,
|
2016-07-21 16:57:55 +00:00
|
|
|
timelineSupport: true,
|
2020-07-29 17:03:43 +00:00
|
|
|
forceTURN: !SettingsStore.getValue('webRtcAllowPeerToPeer'),
|
2019-08-14 13:02:25 +00:00
|
|
|
fallbackICEServerAllowed: !!SettingsStore.getValue('fallbackICEServerAllowed'),
|
2021-02-16 15:48:58 +00:00
|
|
|
// Gather up to 20 ICE candidates when a call arrives: this should be more than we'd
|
|
|
|
// ever normally need, so effectively this should make all the gathering happen when
|
|
|
|
// the call arrives.
|
|
|
|
iceCandidatePoolSize: 20,
|
2020-01-29 15:02:52 +00:00
|
|
|
verificationMethods: [
|
|
|
|
verificationMethods.SAS,
|
|
|
|
SHOW_QR_CODE_METHOD,
|
|
|
|
verificationMethods.RECIPROCATE_QR_CODE,
|
|
|
|
],
|
2019-07-17 15:56:15 +00:00
|
|
|
unstableClientRelationAggregation: true,
|
2019-08-23 10:17:51 +00:00
|
|
|
identityServer: new IdentityAuthClient(),
|
2020-05-25 21:52:05 +00:00
|
|
|
cryptoCallbacks: {},
|
2016-07-21 16:57:55 +00:00
|
|
|
};
|
|
|
|
|
2020-01-15 21:13:56 +00:00
|
|
|
// These are always installed regardless of the labs flag so that
|
|
|
|
// cross-signing features can toggle on without reloading and also be
|
|
|
|
// accessed immediately after login.
|
2021-03-22 23:52:09 +00:00
|
|
|
Object.assign(opts.cryptoCallbacks, crossSigningCallbacks);
|
|
|
|
if (SecurityCustomisations.getDehydrationKey) {
|
|
|
|
opts.cryptoCallbacks.getDehydrationKey =
|
|
|
|
SecurityCustomisations.getDehydrationKey;
|
|
|
|
}
|
2019-11-15 10:57:20 +00:00
|
|
|
|
2020-09-03 20:28:42 +00:00
|
|
|
this.matrixClient = createMatrixClient(opts);
|
|
|
|
|
2016-07-21 16:57:55 +00:00
|
|
|
// we're going to add eventlisteners for each matrix event tile, so the
|
|
|
|
// potential number of event listeners is quite high.
|
|
|
|
this.matrixClient.setMaxListeners(500);
|
2016-07-25 15:20:03 +00:00
|
|
|
|
2016-08-11 12:50:38 +00:00
|
|
|
this.matrixClient.setGuest(Boolean(creds.guest));
|
2016-09-08 02:02:26 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const notifTimelineSet = new EventTimelineSet(null, {
|
|
|
|
timelineSupport: true,
|
2016-09-08 02:02:26 +00:00
|
|
|
});
|
|
|
|
// XXX: what is our initial pagination token?! it somehow needs to be synchronised with /sync.
|
|
|
|
notifTimelineSet.getLiveTimeline().setPaginationToken("", EventTimeline.BACKWARDS);
|
|
|
|
this.matrixClient.setNotifTimelineSet(notifTimelineSet);
|
2016-07-21 16:57:55 +00:00
|
|
|
}
|
2015-09-28 16:46:49 +00:00
|
|
|
}
|
2015-06-09 16:40:42 +00:00
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
if (!window.mxMatrixClientPeg) {
|
|
|
|
window.mxMatrixClientPeg = new _MatrixClientPeg();
|
2015-09-28 16:46:49 +00:00
|
|
|
}
|
2019-12-16 23:07:58 +00:00
|
|
|
|
2020-05-25 22:06:05 +00:00
|
|
|
export const MatrixClientPeg = window.mxMatrixClientPeg;
|