2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2021-08-23 18:47:36 +02:00
|
|
|
Copyright 2020, 2021 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02: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-04-20 21:26:39 +02:00
|
|
|
import {createEnum} from "../utils/enum.js";
|
2021-08-23 19:26:39 +02:00
|
|
|
import {lookupHomeserver} from "./well-known.js";
|
2021-08-23 15:54:06 +02:00
|
|
|
import {AbortableOperation} from "../utils/AbortableOperation";
|
2021-09-30 02:30:21 +02:00
|
|
|
import {ObservableValue} from "../observable/ObservableValue";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {HomeServerApi} from "./net/HomeServerApi.js";
|
2020-04-20 19:48:21 +02:00
|
|
|
import {Reconnector, ConnectionStatus} from "./net/Reconnector.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {ExponentialRetryDelay} from "./net/ExponentialRetryDelay.js";
|
2020-09-22 13:40:38 +02:00
|
|
|
import {MediaRepository} from "./net/MediaRepository.js";
|
2020-09-22 16:39:41 +02:00
|
|
|
import {RequestScheduler} from "./net/RequestScheduler.js";
|
2020-04-20 19:48:21 +02:00
|
|
|
import {Sync, SyncStatus} from "./Sync.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {Session} from "./Session.js";
|
2021-07-30 19:53:52 +02:00
|
|
|
import {PasswordLoginMethod} from "./login/PasswordLoginMethod.js";
|
2021-08-15 08:54:37 +02:00
|
|
|
import {TokenLoginMethod} from "./login/TokenLoginMethod.js";
|
2021-08-17 11:29:38 +02:00
|
|
|
import {SSOLoginHelper} from "./login/SSOLoginHelper.js";
|
2021-10-27 10:26:36 +02:00
|
|
|
import {getDehydratedDevice} from "./e2ee/Dehydration.js";
|
2020-04-09 23:19:49 +02:00
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
export const LoadStatus = createEnum(
|
|
|
|
"NotLoading",
|
|
|
|
"Login",
|
|
|
|
"LoginFailed",
|
2021-10-26 18:47:46 +02:00
|
|
|
"QueryAccount", // check for dehydrated device after login
|
2021-10-27 15:08:53 +02:00
|
|
|
"AccountSetup", // asked to restore from dehydrated device if present, call sc.accountSetup.finish() to progress to the next stage
|
2020-04-09 23:19:49 +02:00
|
|
|
"Loading",
|
2020-09-08 17:16:34 +02:00
|
|
|
"SessionSetup", // upload e2ee keys, ...
|
2021-10-27 10:26:36 +02:00
|
|
|
"Migrating", // not used atm, but would fit here
|
2020-04-19 19:52:26 +02:00
|
|
|
"FirstSync",
|
2020-04-09 23:19:49 +02:00
|
|
|
"Error",
|
|
|
|
"Ready",
|
|
|
|
);
|
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
export const LoginFailure = createEnum(
|
2020-04-20 19:48:21 +02:00
|
|
|
"Connection",
|
2020-04-18 19:16:16 +02:00
|
|
|
"Credentials",
|
|
|
|
"Unknown",
|
|
|
|
);
|
|
|
|
|
|
|
|
export class SessionContainer {
|
2020-10-26 15:44:11 +01:00
|
|
|
constructor({platform, olmPromise, workerPromise}) {
|
|
|
|
this._platform = platform;
|
2020-09-22 16:39:41 +02:00
|
|
|
this._sessionStartedByReconnector = false;
|
2020-04-18 19:16:16 +02:00
|
|
|
this._status = new ObservableValue(LoadStatus.NotLoading);
|
|
|
|
this._error = null;
|
|
|
|
this._loginFailure = null;
|
|
|
|
this._reconnector = null;
|
|
|
|
this._session = null;
|
|
|
|
this._sync = null;
|
2020-04-20 22:26:04 +02:00
|
|
|
this._sessionId = null;
|
|
|
|
this._storage = null;
|
2020-09-22 16:39:41 +02:00
|
|
|
this._requestScheduler = null;
|
2020-08-27 13:24:55 +02:00
|
|
|
this._olmPromise = olmPromise;
|
2020-09-10 18:41:23 +02:00
|
|
|
this._workerPromise = workerPromise;
|
2021-10-27 15:08:53 +02:00
|
|
|
this._accountSetup = undefined;
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:49:14 +02:00
|
|
|
createNewSessionId() {
|
2020-10-26 15:44:11 +01:00
|
|
|
return (Math.floor(this._platform.random() * Number.MAX_SAFE_INTEGER)).toString();
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:03:38 +02:00
|
|
|
get sessionId() {
|
|
|
|
return this._sessionId;
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
async startWithExistingSession(sessionId) {
|
|
|
|
if (this._status.get() !== LoadStatus.NotLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._status.set(LoadStatus.Loading);
|
2021-02-24 09:58:50 +01:00
|
|
|
await this._platform.logger.run("load session", async log => {
|
2021-02-23 19:22:25 +01:00
|
|
|
log.set("id", sessionId);
|
|
|
|
try {
|
|
|
|
const sessionInfo = await this._platform.sessionInfoStorage.get(sessionId);
|
|
|
|
if (!sessionInfo) {
|
|
|
|
throw new Error("Invalid session id: " + sessionId);
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
await this._loadSessionInfo(sessionInfo, null, log);
|
2021-02-23 19:22:25 +01:00
|
|
|
log.set("status", this._status.get());
|
|
|
|
} catch (err) {
|
|
|
|
log.catch(err);
|
|
|
|
this._error = err;
|
|
|
|
this._status.set(LoadStatus.Error);
|
2020-04-20 22:49:14 +02:00
|
|
|
}
|
2021-02-23 19:22:25 +01:00
|
|
|
});
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 19:26:39 +02:00
|
|
|
_parseLoginOptions(options, homeserver) {
|
2021-08-15 08:54:37 +02:00
|
|
|
/*
|
|
|
|
Take server response and return new object which has two props password and sso which
|
2021-07-30 10:15:35 +02:00
|
|
|
implements LoginMethod
|
|
|
|
*/
|
|
|
|
const flows = options.flows;
|
2021-08-23 19:26:39 +02:00
|
|
|
const result = {homeserver};
|
2021-07-30 10:15:35 +02:00
|
|
|
for (const flow of flows) {
|
|
|
|
if (flow.type === "m.login.password") {
|
2021-08-23 19:26:39 +02:00
|
|
|
result.password = (username, password) => new PasswordLoginMethod({homeserver, username, password});
|
2021-07-30 10:15:35 +02:00
|
|
|
}
|
2021-08-15 08:54:37 +02:00
|
|
|
else if (flow.type === "m.login.sso" && flows.find(flow => flow.type === "m.login.token")) {
|
2021-08-23 19:26:39 +02:00
|
|
|
result.sso = new SSOLoginHelper(homeserver);
|
2021-08-15 08:54:37 +02:00
|
|
|
}
|
|
|
|
else if (flow.type === "m.login.token") {
|
2021-08-23 19:26:39 +02:00
|
|
|
result.token = loginToken => new TokenLoginMethod({homeserver, loginToken});
|
2021-08-15 08:54:37 +02:00
|
|
|
}
|
2021-07-30 10:15:35 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:26:39 +02:00
|
|
|
queryLogin(homeserver) {
|
2021-08-23 15:54:06 +02:00
|
|
|
return new AbortableOperation(async setAbortable => {
|
2021-08-23 19:26:39 +02:00
|
|
|
homeserver = await lookupHomeserver(homeserver, (url, options) => {
|
2021-08-23 18:47:36 +02:00
|
|
|
return setAbortable(this._platform.request(url, options));
|
|
|
|
});
|
2021-08-23 19:26:39 +02:00
|
|
|
const hsApi = new HomeServerApi({homeserver, request: this._platform.request});
|
2021-08-23 15:54:06 +02:00
|
|
|
const response = await setAbortable(hsApi.getLoginFlows()).response();
|
2021-08-23 19:26:39 +02:00
|
|
|
return this._parseLoginOptions(response, homeserver);
|
2021-08-23 15:54:06 +02:00
|
|
|
});
|
2021-07-27 15:53:46 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 02:28:01 +01:00
|
|
|
async startWithLogin(loginMethod, {inspectAccountSetup} = {}) {
|
2021-08-20 16:35:36 +02:00
|
|
|
const currentStatus = this._status.get();
|
2021-08-23 11:42:40 +02:00
|
|
|
if (currentStatus !== LoadStatus.LoginFailed &&
|
|
|
|
currentStatus !== LoadStatus.NotLoading &&
|
|
|
|
currentStatus !== LoadStatus.Error) {
|
2020-04-18 19:16:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-08-20 16:35:36 +02:00
|
|
|
this._resetStatus();
|
2021-02-24 09:58:50 +01:00
|
|
|
await this._platform.logger.run("login", async log => {
|
2021-02-23 19:22:25 +01:00
|
|
|
this._status.set(LoadStatus.Login);
|
|
|
|
const clock = this._platform.clock;
|
|
|
|
let sessionInfo;
|
|
|
|
try {
|
|
|
|
const request = this._platform.request;
|
2021-08-23 19:26:39 +02:00
|
|
|
const hsApi = new HomeServerApi({homeserver: loginMethod.homeserver, request});
|
2021-07-30 10:15:35 +02:00
|
|
|
const loginData = await loginMethod.login(hsApi, "Hydrogen", log);
|
2021-02-23 19:22:25 +01:00
|
|
|
const sessionId = this.createNewSessionId();
|
|
|
|
sessionInfo = {
|
|
|
|
id: sessionId,
|
|
|
|
deviceId: loginData.device_id,
|
|
|
|
userId: loginData.user_id,
|
2021-08-23 19:26:39 +02:00
|
|
|
homeServer: loginMethod.homeserver, // deprecate this over time
|
|
|
|
homeserver: loginMethod.homeserver,
|
2021-02-23 19:22:25 +01:00
|
|
|
accessToken: loginData.access_token,
|
|
|
|
lastUsed: clock.now()
|
|
|
|
};
|
|
|
|
log.set("id", sessionId);
|
|
|
|
} catch (err) {
|
|
|
|
this._error = err;
|
2021-04-09 16:30:53 +02:00
|
|
|
if (err.name === "HomeServerError") {
|
2021-02-23 19:22:25 +01:00
|
|
|
if (err.errcode === "M_FORBIDDEN") {
|
|
|
|
this._loginFailure = LoginFailure.Credentials;
|
|
|
|
} else {
|
|
|
|
this._loginFailure = LoginFailure.Unknown;
|
|
|
|
}
|
|
|
|
log.set("loginFailure", this._loginFailure);
|
|
|
|
this._status.set(LoadStatus.LoginFailed);
|
2021-04-09 16:30:53 +02:00
|
|
|
} else if (err.name === "ConnectionError") {
|
2021-02-23 19:22:25 +01:00
|
|
|
this._loginFailure = LoginFailure.Connection;
|
|
|
|
this._status.set(LoadStatus.LoginFailed);
|
2020-04-18 19:16:16 +02:00
|
|
|
} else {
|
2021-02-23 19:22:25 +01:00
|
|
|
this._status.set(LoadStatus.Error);
|
2020-04-18 19:16:16 +02:00
|
|
|
}
|
2021-02-23 19:22:25 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-03 02:28:01 +01:00
|
|
|
let dehydratedDevice;
|
|
|
|
if (inspectAccountSetup) {
|
|
|
|
dehydratedDevice = await this._inspectAccountAfterLogin(sessionInfo, log);
|
|
|
|
if (dehydratedDevice) {
|
|
|
|
sessionInfo.deviceId = dehydratedDevice.deviceId;
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
}
|
|
|
|
await this._platform.sessionInfoStorage.add(sessionInfo);
|
2021-02-23 19:22:25 +01:00
|
|
|
// loading the session can only lead to
|
|
|
|
// LoadStatus.Error in case of an error,
|
|
|
|
// so separate try/catch
|
|
|
|
try {
|
2021-10-26 18:47:46 +02:00
|
|
|
await this._loadSessionInfo(sessionInfo, dehydratedDevice, log);
|
2021-02-23 19:22:25 +01:00
|
|
|
log.set("status", this._status.get());
|
|
|
|
} catch (err) {
|
|
|
|
log.catch(err);
|
2021-10-28 11:52:32 +02:00
|
|
|
// free olm Account that might be contained
|
|
|
|
dehydratedDevice?.dispose();
|
2021-02-23 19:22:25 +01:00
|
|
|
this._error = err;
|
2020-04-18 19:16:16 +02:00
|
|
|
this._status.set(LoadStatus.Error);
|
|
|
|
}
|
2021-02-23 19:22:25 +01:00
|
|
|
});
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 18:47:46 +02:00
|
|
|
async _loadSessionInfo(sessionInfo, dehydratedDevice, log) {
|
2021-03-15 16:55:14 +01:00
|
|
|
log.set("appVersion", this._platform.version);
|
2020-10-26 15:44:11 +01:00
|
|
|
const clock = this._platform.clock;
|
2020-09-22 16:39:41 +02:00
|
|
|
this._sessionStartedByReconnector = false;
|
2020-04-18 19:16:16 +02:00
|
|
|
this._status.set(LoadStatus.Loading);
|
|
|
|
this._reconnector = new Reconnector({
|
2020-10-26 15:44:11 +01:00
|
|
|
onlineStatus: this._platform.onlineStatus,
|
|
|
|
retryDelay: new ExponentialRetryDelay(clock.createTimeout),
|
|
|
|
createMeasure: clock.createMeasure
|
2020-04-18 19:16:16 +02:00
|
|
|
});
|
|
|
|
const hsApi = new HomeServerApi({
|
2021-08-23 19:26:39 +02:00
|
|
|
homeserver: sessionInfo.homeServer,
|
2020-04-18 19:16:16 +02:00
|
|
|
accessToken: sessionInfo.accessToken,
|
2020-10-26 15:44:11 +01:00
|
|
|
request: this._platform.request,
|
2020-04-18 19:16:16 +02:00
|
|
|
reconnector: this._reconnector,
|
|
|
|
});
|
2020-04-20 22:26:04 +02:00
|
|
|
this._sessionId = sessionInfo.id;
|
2021-08-27 19:34:53 +02:00
|
|
|
this._storage = await this._platform.storageFactory.create(sessionInfo.id, log);
|
2020-04-18 19:16:16 +02:00
|
|
|
// no need to pass access token to session
|
|
|
|
const filteredSessionInfo = {
|
2021-03-18 20:44:16 +01:00
|
|
|
id: sessionInfo.id,
|
2020-04-18 19:16:16 +02:00
|
|
|
deviceId: sessionInfo.deviceId,
|
|
|
|
userId: sessionInfo.userId,
|
2021-08-23 19:26:39 +02:00
|
|
|
homeserver: sessionInfo.homeServer,
|
2020-04-18 19:16:16 +02:00
|
|
|
};
|
2020-08-27 13:24:55 +02:00
|
|
|
const olm = await this._olmPromise;
|
2020-09-11 10:43:17 +02:00
|
|
|
let olmWorker = null;
|
2020-09-10 18:41:23 +02:00
|
|
|
if (this._workerPromise) {
|
2020-09-11 10:43:17 +02:00
|
|
|
olmWorker = await this._workerPromise;
|
2020-09-10 18:41:23 +02:00
|
|
|
}
|
2020-10-26 15:44:11 +01:00
|
|
|
this._requestScheduler = new RequestScheduler({hsApi, clock});
|
2020-09-22 16:39:41 +02:00
|
|
|
this._requestScheduler.start();
|
2020-10-23 17:18:11 +02:00
|
|
|
const mediaRepository = new MediaRepository({
|
2021-08-23 19:26:39 +02:00
|
|
|
homeserver: sessionInfo.homeServer,
|
2020-10-30 15:18:27 +01:00
|
|
|
platform: this._platform,
|
2020-10-23 17:18:11 +02:00
|
|
|
});
|
2020-09-22 13:40:38 +02:00
|
|
|
this._session = new Session({
|
|
|
|
storage: this._storage,
|
|
|
|
sessionInfo: filteredSessionInfo,
|
2020-09-22 16:39:41 +02:00
|
|
|
hsApi: this._requestScheduler.hsApi,
|
2020-09-22 13:40:38 +02:00
|
|
|
olm,
|
|
|
|
olmWorker,
|
2020-10-26 17:08:29 +01:00
|
|
|
mediaRepository,
|
2020-10-26 15:44:11 +01:00
|
|
|
platform: this._platform,
|
2020-09-22 13:40:38 +02:00
|
|
|
});
|
2021-02-23 19:22:25 +01:00
|
|
|
await this._session.load(log);
|
2021-10-26 18:47:46 +02:00
|
|
|
if (dehydratedDevice) {
|
2021-10-27 10:26:36 +02:00
|
|
|
await log.wrap("dehydrateIdentity", log => this._session.dehydrateIdentity(dehydratedDevice, log));
|
2021-11-05 20:53:04 +01:00
|
|
|
await this._session.setupDehydratedDevice(dehydratedDevice.key, log);
|
2021-10-26 18:47:46 +02:00
|
|
|
} else if (!this._session.hasIdentity) {
|
2020-10-23 12:22:52 +02:00
|
|
|
this._status.set(LoadStatus.SessionSetup);
|
2021-02-23 19:22:25 +01:00
|
|
|
await log.wrap("createIdentity", log => this._session.createIdentity(log));
|
2020-10-23 12:22:52 +02:00
|
|
|
}
|
2020-04-18 19:16:16 +02:00
|
|
|
|
2021-02-12 18:56:26 +01:00
|
|
|
this._sync = new Sync({hsApi: this._requestScheduler.hsApi, storage: this._storage, session: this._session, logger: this._platform.logger});
|
2020-04-18 19:16:16 +02:00
|
|
|
// notify sync and session when back online
|
|
|
|
this._reconnectSubscription = this._reconnector.connectionStatus.subscribe(state => {
|
|
|
|
if (state === ConnectionStatus.Online) {
|
2021-02-23 19:22:25 +01:00
|
|
|
this._platform.logger.runDetached("reconnect", async log => {
|
|
|
|
// needs to happen before sync and session or it would abort all requests
|
|
|
|
this._requestScheduler.start();
|
|
|
|
this._sync.start();
|
|
|
|
this._sessionStartedByReconnector = true;
|
2021-10-29 19:17:31 +02:00
|
|
|
const d = dehydratedDevice;
|
|
|
|
dehydratedDevice = undefined;
|
|
|
|
await log.wrap("session start", log => this._session.start(this._reconnector.lastVersionsResponse, d, log));
|
2021-02-23 19:22:25 +01:00
|
|
|
});
|
2020-04-18 19:16:16 +02:00
|
|
|
}
|
|
|
|
});
|
2021-02-24 10:14:26 +01:00
|
|
|
await log.wrap("wait first sync", () => this._waitForFirstSync());
|
2021-10-26 12:49:31 +02:00
|
|
|
if (this._isDisposed) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-19 19:02:10 +02:00
|
|
|
this._status.set(LoadStatus.Ready);
|
|
|
|
|
2020-04-20 19:48:21 +02:00
|
|
|
// if the sync failed, and then the reconnector
|
|
|
|
// restored the connection, it would have already
|
|
|
|
// started to session, so check first
|
|
|
|
// to prevent an extra /versions request
|
2020-09-22 16:39:41 +02:00
|
|
|
if (!this._sessionStartedByReconnector) {
|
2021-02-23 19:22:25 +01:00
|
|
|
const lastVersionsResponse = await hsApi.versions({timeout: 10000, log}).response();
|
2021-10-26 12:49:31 +02:00
|
|
|
if (this._isDisposed) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-29 19:17:31 +02:00
|
|
|
const d = dehydratedDevice;
|
|
|
|
dehydratedDevice = undefined;
|
2021-02-23 19:22:25 +01:00
|
|
|
// log as ref as we don't want to await it
|
2021-10-29 19:17:31 +02:00
|
|
|
await log.wrap("session start", log => this._session.start(lastVersionsResponse, d, log));
|
2020-04-20 19:48:21 +02:00
|
|
|
}
|
2020-04-19 19:02:10 +02:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:14:26 +01:00
|
|
|
async _waitForFirstSync() {
|
2021-04-09 16:28:14 +02:00
|
|
|
this._sync.start();
|
|
|
|
this._status.set(LoadStatus.FirstSync);
|
2020-04-18 19:16:16 +02:00
|
|
|
// only transition into Ready once the first sync has succeeded
|
2021-04-09 16:28:14 +02:00
|
|
|
this._waitForFirstSyncHandle = this._sync.status.waitFor(s => {
|
|
|
|
if (s === SyncStatus.Stopped) {
|
|
|
|
// keep waiting if there is a ConnectionError
|
|
|
|
// as the reconnector above will call
|
|
|
|
// sync.start again to retry in this case
|
2021-04-09 19:50:22 +02:00
|
|
|
return this._sync.error?.name !== "ConnectionError";
|
2021-04-09 16:28:14 +02:00
|
|
|
}
|
|
|
|
return s === SyncStatus.Syncing;
|
|
|
|
});
|
2020-04-19 19:02:10 +02:00
|
|
|
try {
|
|
|
|
await this._waitForFirstSyncHandle.promise;
|
2021-04-09 19:50:22 +02:00
|
|
|
if (this._sync.status.get() === SyncStatus.Stopped && this._sync.error) {
|
2020-11-05 21:22:29 +01:00
|
|
|
throw this._sync.error;
|
2020-06-26 23:26:24 +02:00
|
|
|
}
|
2020-04-19 19:02:10 +02:00
|
|
|
} catch (err) {
|
|
|
|
// if dispose is called from stop, bail out
|
2021-04-09 16:30:53 +02:00
|
|
|
if (err.name === "AbortError") {
|
2020-04-19 19:02:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
this._waitForFirstSyncHandle = null;
|
|
|
|
}
|
2020-04-18 19:16:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 10:26:36 +02:00
|
|
|
_inspectAccountAfterLogin(sessionInfo, log) {
|
|
|
|
return log.wrap("inspectAccount", async log => {
|
|
|
|
this._status.set(LoadStatus.QueryAccount);
|
|
|
|
const hsApi = new HomeServerApi({
|
|
|
|
homeserver: sessionInfo.homeServer,
|
|
|
|
accessToken: sessionInfo.accessToken,
|
|
|
|
request: this._platform.request,
|
|
|
|
});
|
|
|
|
const olm = await this._olmPromise;
|
2021-11-25 15:38:13 +01:00
|
|
|
let encryptedDehydratedDevice;
|
|
|
|
try {
|
|
|
|
encryptedDehydratedDevice = await getDehydratedDevice(hsApi, olm, this._platform, log);
|
|
|
|
} catch (err) {
|
2021-11-25 15:42:36 +01:00
|
|
|
if (err.name === "HomeServerError") {
|
2021-11-25 15:38:13 +01:00
|
|
|
log.set("not_supported", true);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:26:36 +02:00
|
|
|
if (encryptedDehydratedDevice) {
|
|
|
|
let resolveStageFinish;
|
|
|
|
const promiseStageFinish = new Promise(r => resolveStageFinish = r);
|
|
|
|
this._accountSetup = new AccountSetup(encryptedDehydratedDevice, resolveStageFinish);
|
2021-10-27 15:08:53 +02:00
|
|
|
this._status.set(LoadStatus.AccountSetup);
|
2021-10-27 10:26:36 +02:00
|
|
|
await promiseStageFinish;
|
|
|
|
const dehydratedDevice = this._accountSetup?._dehydratedDevice;
|
|
|
|
this._accountSetup = null;
|
|
|
|
return dehydratedDevice;
|
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get accountSetup() {
|
|
|
|
return this._accountSetup;
|
|
|
|
}
|
2020-04-18 19:16:16 +02:00
|
|
|
|
|
|
|
get loadStatus() {
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get loadError() {
|
|
|
|
return this._error;
|
|
|
|
}
|
|
|
|
|
2021-08-20 11:49:42 +02:00
|
|
|
get loginFailure() {
|
|
|
|
return this._loginFailure;
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
/** only set at loadStatus InitialSync, CatchupSync or Ready */
|
2020-04-09 23:19:49 +02:00
|
|
|
get sync() {
|
|
|
|
return this._sync;
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
/** only set at loadStatus InitialSync, CatchupSync or Ready */
|
2020-04-09 23:19:49 +02:00
|
|
|
get session() {
|
|
|
|
return this._session;
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
get reconnector() {
|
|
|
|
return this._reconnector;
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:49:31 +02:00
|
|
|
get _isDisposed() {
|
|
|
|
return !this._reconnector;
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:47:31 +02:00
|
|
|
logout() {
|
|
|
|
return this._platform.logger.run("logout", async log => {
|
|
|
|
try {
|
|
|
|
await this._session?.logout(log);
|
|
|
|
} catch (err) {}
|
|
|
|
await this.deleteSession(log);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-18 13:11:10 +02:00
|
|
|
dispose() {
|
2020-08-28 13:58:42 +02:00
|
|
|
if (this._reconnectSubscription) {
|
|
|
|
this._reconnectSubscription();
|
|
|
|
this._reconnectSubscription = null;
|
|
|
|
}
|
2021-10-26 12:49:31 +02:00
|
|
|
this._reconnector = null;
|
2020-09-22 16:39:41 +02:00
|
|
|
if (this._requestScheduler) {
|
|
|
|
this._requestScheduler.stop();
|
2021-10-28 11:48:25 +02:00
|
|
|
this._requestScheduler = null;
|
2020-09-22 16:39:41 +02:00
|
|
|
}
|
2020-08-28 13:58:42 +02:00
|
|
|
if (this._sync) {
|
|
|
|
this._sync.stop();
|
2021-10-28 11:48:25 +02:00
|
|
|
this._sync = null;
|
2020-08-28 13:58:42 +02:00
|
|
|
}
|
|
|
|
if (this._session) {
|
2020-09-18 13:11:10 +02:00
|
|
|
this._session.dispose();
|
2021-10-28 11:48:25 +02:00
|
|
|
this._session = null;
|
2020-08-28 13:58:42 +02:00
|
|
|
}
|
2020-04-19 19:02:10 +02:00
|
|
|
if (this._waitForFirstSyncHandle) {
|
|
|
|
this._waitForFirstSyncHandle.dispose();
|
|
|
|
this._waitForFirstSyncHandle = null;
|
|
|
|
}
|
2020-04-20 22:26:04 +02:00
|
|
|
if (this._storage) {
|
|
|
|
this._storage.close();
|
2020-04-20 22:49:14 +02:00
|
|
|
this._storage = null;
|
2020-04-20 22:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:49:31 +02:00
|
|
|
async deleteSession(log) {
|
2020-04-20 22:26:04 +02:00
|
|
|
if (this._sessionId) {
|
2021-10-26 12:49:31 +02:00
|
|
|
// need to dispose first, so the storage is closed,
|
|
|
|
// and also first sync finishing won't call Session.start anymore,
|
|
|
|
// which assumes that the storage works.
|
|
|
|
this.dispose();
|
2020-04-20 22:26:04 +02:00
|
|
|
// if one fails, don't block the other from trying
|
|
|
|
// also, run in parallel
|
|
|
|
await Promise.all([
|
2021-10-26 12:49:31 +02:00
|
|
|
log.wrap("storageFactory", () => this._platform.storageFactory.delete(this._sessionId)),
|
|
|
|
log.wrap("sessionInfoStorage", () => this._platform.sessionInfoStorage.delete(this._sessionId)),
|
2020-04-20 22:26:04 +02:00
|
|
|
]);
|
|
|
|
this._sessionId = null;
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
2021-08-20 11:49:42 +02:00
|
|
|
|
2021-08-20 16:35:36 +02:00
|
|
|
_resetStatus() {
|
2021-08-20 11:49:42 +02:00
|
|
|
this._status.set(LoadStatus.NotLoading);
|
|
|
|
this._error = null;
|
|
|
|
this._loginFailure = null;
|
|
|
|
}
|
2020-04-18 19:16:16 +02:00
|
|
|
}
|
2021-10-26 18:47:46 +02:00
|
|
|
|
|
|
|
class AccountSetup {
|
|
|
|
constructor(encryptedDehydratedDevice, finishStage) {
|
|
|
|
this._encryptedDehydratedDevice = encryptedDehydratedDevice;
|
|
|
|
this._dehydratedDevice = undefined;
|
|
|
|
this._finishStage = finishStage;
|
|
|
|
}
|
|
|
|
|
|
|
|
get encryptedDehydratedDevice() {
|
|
|
|
return this._encryptedDehydratedDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish(dehydratedDevice) {
|
|
|
|
this._dehydratedDevice = dehydratedDevice;
|
|
|
|
this._finishStage();
|
|
|
|
}
|
|
|
|
}
|