2016-08-02 21:04:20 +08:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2017-02-16 02:44:15 +08:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-04-27 18:25:13 +08:00
|
|
|
Copyright 2018 New Vector Ltd
|
2016-08-02 21:04:20 +08: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.
|
|
|
|
*/
|
|
|
|
|
2017-07-12 20:58:14 +08:00
|
|
|
import Promise from 'bluebird';
|
2016-08-11 18:00:15 +08:00
|
|
|
import Matrix from 'matrix-js-sdk';
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2016-08-02 21:04:20 +08:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2017-06-01 00:28:46 +08:00
|
|
|
import createMatrixClient from './utils/createMatrixClient';
|
2017-05-28 03:47:09 +08:00
|
|
|
import Analytics from './Analytics';
|
2017-01-20 22:22:27 +08:00
|
|
|
import Notifier from './Notifier';
|
2016-08-02 21:04:20 +08:00
|
|
|
import UserActivity from './UserActivity';
|
|
|
|
import Presence from './Presence';
|
|
|
|
import dis from './dispatcher';
|
2016-09-27 01:02:14 +08:00
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2017-02-16 02:44:15 +08:00
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './index';
|
2018-07-24 23:21:43 +08:00
|
|
|
import ActiveWidgetStore from './stores/ActiveWidgetStore';
|
2018-09-26 23:25:21 +08:00
|
|
|
import PlatformPeg from "./PlatformPeg";
|
2019-03-26 01:43:53 +08:00
|
|
|
import { sendLoginRequest } from "./Login";
|
|
|
|
import * as StorageManager from './utils/StorageManager';
|
2019-02-09 00:44:03 +08:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2019-06-27 12:36:55 +08:00
|
|
|
import TypingStore from "./stores/TypingStore";
|
2016-08-02 21:04:20 +08:00
|
|
|
|
2016-08-10 18:33:58 +08:00
|
|
|
/**
|
|
|
|
* Called at startup, to attempt to build a logged-in Matrix session. It tries
|
|
|
|
* a number of things:
|
|
|
|
*
|
|
|
|
*
|
2017-06-16 21:33:14 +08:00
|
|
|
* 1. if we have a guest access token in the fragment query params, it uses
|
2016-08-11 18:00:15 +08:00
|
|
|
* that.
|
|
|
|
*
|
2017-06-16 21:33:14 +08:00
|
|
|
* 2. if an access token is stored in local storage (from a previous session),
|
2016-08-10 18:33:58 +08:00
|
|
|
* it uses that.
|
|
|
|
*
|
2017-06-16 21:33:14 +08:00
|
|
|
* 3. it attempts to auto-register as a guest user.
|
2016-08-10 18:33:58 +08:00
|
|
|
*
|
2017-06-01 00:28:46 +08:00
|
|
|
* If any of steps 1-4 are successful, it will call {_doSetLoggedIn}, which in
|
2016-08-10 18:33:58 +08:00
|
|
|
* turn will raise on_logged_in and will_start_client events.
|
|
|
|
*
|
2017-05-05 01:03:35 +08:00
|
|
|
* @param {object} opts
|
2016-08-10 18:33:58 +08:00
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* @param {object} opts.fragmentQueryParams: string->string map of the
|
|
|
|
* query-parameters extracted from the #-fragment of the starting URI.
|
2016-08-10 18:33:58 +08:00
|
|
|
*
|
|
|
|
* @param {boolean} opts.enableGuest: set to true to enable guest access tokens
|
|
|
|
* and auto-guest registrations.
|
|
|
|
*
|
2016-08-11 08:30:43 +08:00
|
|
|
* @params {string} opts.guestHsUrl: homeserver URL. Only used if enableGuest is
|
2016-08-10 18:33:58 +08:00
|
|
|
* true; defines the HS to register against.
|
|
|
|
*
|
2016-08-11 08:30:43 +08:00
|
|
|
* @params {string} opts.guestIsUrl: homeserver URL. Only used if enableGuest is
|
2016-08-10 18:33:58 +08:00
|
|
|
* true; defines the IS to use.
|
|
|
|
*
|
2019-08-02 18:22:42 +08:00
|
|
|
* @params {bool} opts.ignoreGuest: If the stored session is a guest account, ignore
|
|
|
|
* it and don't load it.
|
|
|
|
*
|
2017-05-05 01:03:35 +08:00
|
|
|
* @returns {Promise} a promise which resolves when the above process completes.
|
2017-06-16 19:20:52 +08:00
|
|
|
* Resolves to `true` if we ended up starting a session, or `false` if we
|
|
|
|
* failed.
|
2016-08-10 18:33:58 +08:00
|
|
|
*/
|
2018-04-28 00:49:53 +08:00
|
|
|
export async function loadSession(opts) {
|
|
|
|
try {
|
|
|
|
let enableGuest = opts.enableGuest || false;
|
|
|
|
const guestHsUrl = opts.guestHsUrl;
|
|
|
|
const guestIsUrl = opts.guestIsUrl;
|
|
|
|
const fragmentQueryParams = opts.fragmentQueryParams || {};
|
|
|
|
const defaultDeviceDisplayName = opts.defaultDeviceDisplayName;
|
2018-04-27 21:20:09 +08:00
|
|
|
|
2019-08-02 18:08:37 +08:00
|
|
|
if (enableGuest && !guestHsUrl) {
|
2018-04-27 18:25:13 +08:00
|
|
|
console.warn("Cannot enable guest access: can't determine HS URL to use");
|
|
|
|
enableGuest = false;
|
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2018-04-27 18:25:13 +08:00
|
|
|
if (enableGuest &&
|
|
|
|
fragmentQueryParams.guest_user_id &&
|
|
|
|
fragmentQueryParams.guest_access_token
|
|
|
|
) {
|
|
|
|
console.log("Using guest access credentials");
|
|
|
|
return _doSetLoggedIn({
|
|
|
|
userId: fragmentQueryParams.guest_user_id,
|
|
|
|
accessToken: fragmentQueryParams.guest_access_token,
|
|
|
|
homeserverUrl: guestHsUrl,
|
|
|
|
identityServerUrl: guestIsUrl,
|
|
|
|
guest: true,
|
|
|
|
}, true).then(() => true);
|
|
|
|
}
|
2019-08-02 23:44:49 +08:00
|
|
|
const success = await _restoreFromLocalStorage({
|
|
|
|
ignoreGuest: Boolean(opts.ignoreGuest),
|
|
|
|
});
|
2017-02-16 02:44:15 +08:00
|
|
|
if (success) {
|
2017-06-16 19:20:52 +08:00
|
|
|
return true;
|
2017-02-16 02:44:15 +08:00
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2017-02-16 02:44:15 +08:00
|
|
|
if (enableGuest) {
|
|
|
|
return _registerAsGuest(guestHsUrl, guestIsUrl, defaultDeviceDisplayName);
|
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2019-03-29 00:22:17 +08:00
|
|
|
// fall back to welcome screen
|
2017-06-16 19:20:52 +08:00
|
|
|
return false;
|
2018-04-28 00:49:53 +08:00
|
|
|
} catch (e) {
|
2019-03-29 00:22:17 +08:00
|
|
|
if (e instanceof AbortLoginAndRebuildStorage) {
|
|
|
|
// If we're aborting login because of a storage inconsistency, we don't
|
|
|
|
// need to show the general failure dialog. Instead, just go back to welcome.
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-27 18:25:13 +08:00
|
|
|
return _handleLoadSessionFailure(e);
|
2018-04-28 00:49:53 +08:00
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
}
|
|
|
|
|
2019-03-08 08:04:50 +08:00
|
|
|
/**
|
|
|
|
* Gets the user ID of the persisted session, if one exists. This does not validate
|
|
|
|
* that the user's credentials still work, just that they exist and that a user ID
|
|
|
|
* is associated with them. The session is not loaded.
|
|
|
|
* @returns {String} The persisted session's owner, if an owner exists. Null otherwise.
|
|
|
|
*/
|
|
|
|
export function getStoredSessionOwner() {
|
2019-05-30 02:09:04 +08:00
|
|
|
const {hsUrl, userId, accessToken} = getLocalStorageSessionVars();
|
2019-03-08 08:04:50 +08:00
|
|
|
return hsUrl && userId && accessToken ? userId : null;
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:59:38 +08:00
|
|
|
/**
|
|
|
|
* @returns {bool} True if the stored session is for a guest user or false if it is
|
|
|
|
* for a real user. If there is no stored session, return null.
|
|
|
|
*/
|
|
|
|
export function getStoredSessionIsGuest() {
|
2019-05-30 02:09:04 +08:00
|
|
|
const sessVars = getLocalStorageSessionVars();
|
2019-05-14 19:11:07 +08:00
|
|
|
return sessVars.hsUrl && sessVars.userId && sessVars.accessToken ? sessVars.isGuest : null;
|
2019-05-14 18:59:38 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 21:33:14 +08:00
|
|
|
/**
|
|
|
|
* @param {Object} queryParams string->string map of the
|
|
|
|
* query-parameters extracted from the real query-string of the starting
|
|
|
|
* URI.
|
|
|
|
*
|
|
|
|
* @param {String} defaultDeviceDisplayName
|
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to true if we completed the token
|
|
|
|
* login, else false
|
|
|
|
*/
|
|
|
|
export function attemptTokenLogin(queryParams, defaultDeviceDisplayName) {
|
|
|
|
if (!queryParams.loginToken) {
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve(false);
|
2017-06-16 21:33:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!queryParams.homeserver) {
|
|
|
|
console.warn("Cannot log in with token: can't determine HS URL to use");
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve(false);
|
2017-06-16 21:33:14 +08:00
|
|
|
}
|
|
|
|
|
2018-12-06 00:39:38 +08:00
|
|
|
return sendLoginRequest(
|
|
|
|
queryParams.homeserver,
|
|
|
|
queryParams.identityServer,
|
2016-08-11 23:15:42 +08:00
|
|
|
"m.login.token", {
|
|
|
|
token: queryParams.loginToken,
|
|
|
|
initial_device_display_name: defaultDeviceDisplayName,
|
|
|
|
},
|
2018-12-06 00:39:38 +08:00
|
|
|
).then(function(creds) {
|
2016-08-11 18:00:15 +08:00
|
|
|
console.log("Logged in with token");
|
2017-06-16 21:33:14 +08:00
|
|
|
return _clearStorage().then(() => {
|
2018-12-06 00:39:38 +08:00
|
|
|
_persistCredentialsToLocalStorage(creds);
|
2017-06-16 21:33:14 +08:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}).catch((err) => {
|
2016-08-11 18:00:15 +08:00
|
|
|
console.error("Failed to log in with login token: " + err + " " +
|
|
|
|
err.data);
|
2017-06-16 21:33:14 +08:00
|
|
|
return false;
|
2016-08-11 18:00:15 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:07:17 +08:00
|
|
|
export function handleInvalidStoreError(e) {
|
|
|
|
if (e.reason === Matrix.InvalidStoreError.TOGGLED_LAZY_LOADING) {
|
|
|
|
return Promise.resolve().then(() => {
|
|
|
|
const lazyLoadEnabled = e.value;
|
|
|
|
if (lazyLoadEnabled) {
|
|
|
|
const LazyLoadingResyncDialog =
|
|
|
|
sdk.getComponent("views.dialogs.LazyLoadingResyncDialog");
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
Modal.createDialog(LazyLoadingResyncDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// show warning about simultaneous use
|
|
|
|
// between LL/non-LL version on same host.
|
|
|
|
// as disabling LL when previously enabled
|
|
|
|
// is a strong indicator of this (/develop & /app)
|
|
|
|
const LazyLoadingDisabledDialog =
|
|
|
|
sdk.getComponent("views.dialogs.LazyLoadingDisabledDialog");
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
Modal.createDialog(LazyLoadingDisabledDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
host: window.location.host,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return MatrixClientPeg.get().store.deleteAllData();
|
|
|
|
}).then(() => {
|
|
|
|
PlatformPeg.get().reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:15:42 +08:00
|
|
|
function _registerAsGuest(hsUrl, isUrl, defaultDeviceDisplayName) {
|
2017-06-21 00:38:02 +08:00
|
|
|
console.log(`Doing guest login on ${hsUrl}`);
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2016-08-11 20:50:38 +08:00
|
|
|
// create a temporary MatrixClient to do the login
|
2017-05-05 01:03:35 +08:00
|
|
|
const client = Matrix.createClient({
|
2016-08-11 20:50:38 +08:00
|
|
|
baseUrl: hsUrl,
|
|
|
|
});
|
|
|
|
|
2016-08-11 23:15:42 +08:00
|
|
|
return client.registerGuest({
|
|
|
|
body: {
|
|
|
|
initial_device_display_name: defaultDeviceDisplayName,
|
|
|
|
},
|
|
|
|
}).then((creds) => {
|
2017-06-21 00:38:02 +08:00
|
|
|
console.log(`Registered as guest: ${creds.user_id}`);
|
2017-06-01 00:28:46 +08:00
|
|
|
return _doSetLoggedIn({
|
2016-08-10 18:33:58 +08:00
|
|
|
userId: creds.user_id,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: creds.device_id,
|
2016-08-10 18:33:58 +08:00
|
|
|
accessToken: creds.access_token,
|
|
|
|
homeserverUrl: hsUrl,
|
|
|
|
identityServerUrl: isUrl,
|
|
|
|
guest: true,
|
2017-06-16 19:20:52 +08:00
|
|
|
}, true).then(() => true);
|
2016-08-10 18:33:58 +08:00
|
|
|
}, (err) => {
|
2019-06-12 21:04:08 +08:00
|
|
|
console.error("Failed to register as guest", err);
|
2017-06-16 19:20:52 +08:00
|
|
|
return false;
|
2016-08-10 18:33:58 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:09:04 +08:00
|
|
|
/**
|
|
|
|
* Retrieves information about the stored session in localstorage. The session
|
|
|
|
* may not be valid, as it is not tested for consistency here.
|
|
|
|
* @returns {Object} Information about the session - see implementation for variables.
|
|
|
|
*/
|
|
|
|
export function getLocalStorageSessionVars() {
|
2019-03-08 08:04:50 +08:00
|
|
|
const hsUrl = localStorage.getItem("mx_hs_url");
|
|
|
|
const isUrl = localStorage.getItem("mx_is_url") || 'https://matrix.org';
|
|
|
|
const accessToken = localStorage.getItem("mx_access_token");
|
|
|
|
const userId = localStorage.getItem("mx_user_id");
|
|
|
|
const deviceId = localStorage.getItem("mx_device_id");
|
|
|
|
|
2019-05-14 18:59:38 +08:00
|
|
|
let isGuest;
|
|
|
|
if (localStorage.getItem("mx_is_guest") !== null) {
|
|
|
|
isGuest = localStorage.getItem("mx_is_guest") === "true";
|
|
|
|
} else {
|
|
|
|
// legacy key name
|
|
|
|
isGuest = localStorage.getItem("matrix-is-guest") === "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {hsUrl, isUrl, accessToken, userId, deviceId, isGuest};
|
2019-03-08 08:04:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-16 02:44:15 +08:00
|
|
|
// returns a promise which resolves to true if a session is found in
|
|
|
|
// localstorage
|
2017-05-16 18:58:37 +08:00
|
|
|
//
|
|
|
|
// N.B. Lifecycle.js should not maintain any further localStorage state, we
|
|
|
|
// are moving towards using SessionStore to keep track of state related
|
|
|
|
// to the current session (which is typically backed by localStorage).
|
|
|
|
//
|
|
|
|
// The plan is to gradually move the localStorage access done here into
|
|
|
|
// SessionStore to avoid bugs where the view becomes out-of-sync with
|
2019-01-26 06:10:54 +08:00
|
|
|
// localStorage (e.g. isGuest etc.)
|
2019-08-02 23:44:49 +08:00
|
|
|
async function _restoreFromLocalStorage(opts) {
|
|
|
|
const ignoreGuest = opts.ignoreGuest;
|
|
|
|
|
2018-04-28 00:49:53 +08:00
|
|
|
if (!localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:09:04 +08:00
|
|
|
const {hsUrl, isUrl, accessToken, userId, deviceId, isGuest} = getLocalStorageSessionVars();
|
2016-08-10 17:33:27 +08:00
|
|
|
|
2018-04-28 00:49:53 +08:00
|
|
|
if (accessToken && userId && hsUrl) {
|
2019-08-02 18:22:42 +08:00
|
|
|
if (ignoreGuest && isGuest) {
|
|
|
|
console.log("Ignoring stored guest account: " + userId);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-28 00:49:53 +08:00
|
|
|
console.log(`Restoring session for ${userId}`);
|
|
|
|
await _doSetLoggedIn({
|
|
|
|
userId: userId,
|
|
|
|
deviceId: deviceId,
|
|
|
|
accessToken: accessToken,
|
|
|
|
homeserverUrl: hsUrl,
|
|
|
|
identityServerUrl: isUrl,
|
|
|
|
guest: isGuest,
|
|
|
|
}, false);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
console.log("No previous session found.");
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-10 17:33:27 +08:00
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2018-04-27 18:25:13 +08:00
|
|
|
function _handleLoadSessionFailure(e) {
|
2019-03-29 00:22:17 +08:00
|
|
|
console.error("Unable to load session", e);
|
2017-02-16 02:44:15 +08:00
|
|
|
|
2017-07-12 21:04:20 +08:00
|
|
|
const def = Promise.defer();
|
2017-02-16 02:44:15 +08:00
|
|
|
const SessionRestoreErrorDialog =
|
|
|
|
sdk.getComponent('views.dialogs.SessionRestoreErrorDialog');
|
|
|
|
|
2017-08-10 22:17:52 +08:00
|
|
|
Modal.createTrackedDialog('Session Restore Error', '', SessionRestoreErrorDialog, {
|
2017-06-22 04:49:41 +08:00
|
|
|
error: e.message,
|
2017-02-16 02:44:15 +08:00
|
|
|
onFinished: (success) => {
|
|
|
|
def.resolve(success);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return def.promise.then((success) => {
|
|
|
|
if (success) {
|
|
|
|
// user clicked continue.
|
2017-06-01 00:28:46 +08:00
|
|
|
_clearStorage();
|
2017-02-16 02:44:15 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try, try again
|
2018-04-27 18:25:13 +08:00
|
|
|
return loadSession();
|
2017-02-16 02:44:15 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-03 01:52:56 +08:00
|
|
|
/**
|
2017-06-01 00:28:46 +08:00
|
|
|
* Transitions to a logged-in state using the given credentials.
|
|
|
|
*
|
|
|
|
* Starts the matrix client and all other react-sdk services that
|
|
|
|
* listen for events while a session is logged in.
|
|
|
|
*
|
|
|
|
* Also stops the old MatrixClient and clears old credentials/etc out of
|
|
|
|
* storage before starting the new client.
|
|
|
|
*
|
2016-08-03 23:31:42 +08:00
|
|
|
* @param {MatrixClientCreds} credentials The credentials to use
|
2017-06-19 17:22:18 +08:00
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
2016-08-03 01:52:56 +08:00
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function setLoggedIn(credentials) {
|
2017-06-01 00:28:46 +08:00
|
|
|
stopMatrixClient();
|
2017-06-19 17:22:18 +08:00
|
|
|
return _doSetLoggedIn(credentials, true);
|
2017-06-01 00:28:46 +08:00
|
|
|
}
|
2017-05-05 01:04:47 +08:00
|
|
|
|
2019-07-05 06:45:40 +08:00
|
|
|
/**
|
|
|
|
* Hydrates an existing session by using the credentials provided. This will
|
|
|
|
* not clear any local storage, unlike setLoggedIn().
|
|
|
|
*
|
|
|
|
* Stops the existing Matrix client (without clearing its data) and starts a
|
|
|
|
* new one in its place. This additionally starts all other react-sdk services
|
|
|
|
* which use the new Matrix client.
|
|
|
|
*
|
2019-07-06 04:45:34 +08:00
|
|
|
* If the credentials belong to a different user from the session already stored,
|
|
|
|
* the old session will be cleared automatically.
|
|
|
|
*
|
2019-07-05 06:45:40 +08:00
|
|
|
* @param {MatrixClientCreds} credentials The credentials to use
|
|
|
|
*
|
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
|
|
|
*/
|
|
|
|
export function hydrateSession(credentials) {
|
2019-07-06 04:45:34 +08:00
|
|
|
const oldUserId = MatrixClientPeg.get().getUserId();
|
2019-07-09 05:35:34 +08:00
|
|
|
const oldDeviceId = MatrixClientPeg.get().getDeviceId();
|
2019-07-06 04:45:34 +08:00
|
|
|
|
|
|
|
stopMatrixClient(); // unsets MatrixClientPeg.get()
|
2019-07-05 06:45:40 +08:00
|
|
|
localStorage.removeItem("mx_soft_logout");
|
|
|
|
_isLoggingOut = false;
|
2019-07-06 04:45:34 +08:00
|
|
|
|
2019-07-09 05:35:34 +08:00
|
|
|
const overwrite = credentials.userId !== oldUserId || credentials.deviceId !== oldDeviceId;
|
2019-07-06 04:45:34 +08:00
|
|
|
if (overwrite) {
|
2019-07-09 05:35:34 +08:00
|
|
|
console.warn("Clearing all data: Old session belongs to a different user/device");
|
2019-07-06 04:45:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return _doSetLoggedIn(credentials, overwrite);
|
2019-07-05 06:45:40 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 00:28:46 +08:00
|
|
|
/**
|
|
|
|
* fires on_logging_in, optionally clears localstorage, persists new credentials
|
|
|
|
* to localstorage, starts the new client.
|
|
|
|
*
|
|
|
|
* @param {MatrixClientCreds} credentials
|
|
|
|
* @param {Boolean} clearStorage
|
|
|
|
*
|
2017-06-19 17:22:18 +08:00
|
|
|
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started
|
2017-06-01 00:28:46 +08:00
|
|
|
*/
|
|
|
|
async function _doSetLoggedIn(credentials, clearStorage) {
|
|
|
|
credentials.guest = Boolean(credentials.guest);
|
2017-05-30 02:04:37 +08:00
|
|
|
|
2019-07-04 06:46:37 +08:00
|
|
|
const softLogout = isSoftLogout();
|
|
|
|
|
2017-05-05 01:04:47 +08:00
|
|
|
console.log(
|
2017-06-21 00:38:02 +08:00
|
|
|
"setLoggedIn: mxid: " + credentials.userId +
|
|
|
|
" deviceId: " + credentials.deviceId +
|
|
|
|
" guest: " + credentials.guest +
|
2019-07-04 06:46:37 +08:00
|
|
|
" hs: " + credentials.homeserverUrl +
|
|
|
|
" softLogout: " + softLogout,
|
2017-05-05 01:04:47 +08:00
|
|
|
);
|
2017-06-01 00:28:46 +08:00
|
|
|
|
2017-03-27 23:39:04 +08:00
|
|
|
// This is dispatched to indicate that the user is still in the process of logging in
|
2019-01-26 06:10:54 +08:00
|
|
|
// because async code may take some time to resolve, breaking the assumption that
|
2017-03-27 23:39:04 +08:00
|
|
|
// `setLoggedIn` takes an "instant" to complete, and dispatch `on_logged_in` a few ms
|
|
|
|
// later than MatrixChat might assume.
|
2017-07-12 00:09:06 +08:00
|
|
|
//
|
|
|
|
// we fire it *synchronously* to make sure it fires before on_logged_in.
|
|
|
|
// (dis.dispatch uses `setTimeout`, which does not guarantee ordering.)
|
|
|
|
dis.dispatch({action: 'on_logging_in'}, true);
|
2016-08-11 01:04:22 +08:00
|
|
|
|
2017-06-01 00:28:46 +08:00
|
|
|
if (clearStorage) {
|
|
|
|
await _clearStorage();
|
|
|
|
}
|
|
|
|
|
2019-03-29 00:22:17 +08:00
|
|
|
const results = await StorageManager.checkConsistency();
|
|
|
|
// If there's an inconsistency between account data in local storage and the
|
|
|
|
// crypto store, we'll be generally confused when handling encrypted data.
|
|
|
|
// Show a modal recommending a full reset of storage.
|
2019-05-15 20:47:48 +08:00
|
|
|
if (results.dataInLocalStorage && results.cryptoInited && !results.dataInCryptoStore) {
|
2019-03-29 00:22:17 +08:00
|
|
|
const signOut = await _showStorageEvictedDialog();
|
|
|
|
if (signOut) {
|
|
|
|
await _clearStorage();
|
|
|
|
// This error feels a bit clunky, but we want to make sure we don't go any
|
|
|
|
// further and instead head back to sign in.
|
|
|
|
throw new AbortLoginAndRebuildStorage(
|
|
|
|
"Aborting login in progress because of storage inconsistency",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 01:43:53 +08:00
|
|
|
|
2017-07-28 00:19:18 +08:00
|
|
|
Analytics.setLoggedIn(credentials.guest, credentials.homeserverUrl, credentials.identityServerUrl);
|
2017-06-01 00:28:46 +08:00
|
|
|
|
2016-08-11 01:04:22 +08:00
|
|
|
if (localStorage) {
|
|
|
|
try {
|
2017-06-16 21:33:14 +08:00
|
|
|
_persistCredentialsToLocalStorage(credentials);
|
2016-08-12 18:20:09 +08:00
|
|
|
|
2017-05-02 17:07:06 +08:00
|
|
|
// The user registered as a PWLU (PassWord-Less User), the generated password
|
|
|
|
// is cached here such that the user can change it at a later time.
|
|
|
|
if (credentials.password) {
|
2017-05-12 19:02:45 +08:00
|
|
|
// Update SessionStore
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'cached_password',
|
|
|
|
cachedPassword: credentials.password,
|
|
|
|
});
|
2017-05-02 17:07:06 +08:00
|
|
|
}
|
2016-08-11 01:04:22 +08:00
|
|
|
} catch (e) {
|
|
|
|
console.warn("Error using local storage: can't persist session!", e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn("No local storage available: can't persist session!");
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:46:42 +08:00
|
|
|
MatrixClientPeg.replaceUsingCreds(credentials);
|
2016-08-02 21:04:20 +08:00
|
|
|
|
2019-01-26 06:10:54 +08:00
|
|
|
dis.dispatch({ action: 'on_logged_in' });
|
2016-08-02 21:04:20 +08:00
|
|
|
|
2019-07-04 06:46:37 +08:00
|
|
|
await startMatrixClient(/*startSyncing=*/!softLogout);
|
2017-06-19 17:22:18 +08:00
|
|
|
return MatrixClientPeg.get();
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|
|
|
|
|
2019-03-29 00:22:17 +08:00
|
|
|
function _showStorageEvictedDialog() {
|
|
|
|
const StorageEvictedDialog = sdk.getComponent('views.dialogs.StorageEvictedDialog');
|
|
|
|
return new Promise(resolve => {
|
|
|
|
Modal.createTrackedDialog('Storage evicted', '', StorageEvictedDialog, {
|
|
|
|
onFinished: resolve,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Babel 6 requires the `transform-builtin-extend` plugin for this to satisfy
|
|
|
|
// `instanceof`. Babel 7 supports this natively in their class handling.
|
|
|
|
class AbortLoginAndRebuildStorage extends Error { }
|
|
|
|
|
2017-06-16 21:33:14 +08:00
|
|
|
function _persistCredentialsToLocalStorage(credentials) {
|
|
|
|
localStorage.setItem("mx_hs_url", credentials.homeserverUrl);
|
|
|
|
localStorage.setItem("mx_is_url", credentials.identityServerUrl);
|
|
|
|
localStorage.setItem("mx_user_id", credentials.userId);
|
|
|
|
localStorage.setItem("mx_access_token", credentials.accessToken);
|
|
|
|
localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest));
|
|
|
|
|
|
|
|
// if we didn't get a deviceId from the login, leave mx_device_id unset,
|
|
|
|
// rather than setting it to "undefined".
|
|
|
|
//
|
|
|
|
// (in this case MatrixClient doesn't bother with the crypto stuff
|
|
|
|
// - that's fine for us).
|
|
|
|
if (credentials.deviceId) {
|
|
|
|
localStorage.setItem("mx_device_id", credentials.deviceId);
|
|
|
|
}
|
|
|
|
|
2017-06-21 00:38:02 +08:00
|
|
|
console.log(`Session persisted for ${credentials.userId}`);
|
2017-06-16 21:33:14 +08:00
|
|
|
}
|
|
|
|
|
2018-07-16 05:33:00 +08:00
|
|
|
let _isLoggingOut = false;
|
|
|
|
|
2016-08-03 01:55:13 +08:00
|
|
|
/**
|
|
|
|
* Logs the current session out and transitions to the logged-out state
|
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function logout() {
|
2017-12-05 19:53:49 +08:00
|
|
|
if (!MatrixClientPeg.get()) return;
|
2017-12-05 19:38:25 +08:00
|
|
|
|
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
2016-08-02 21:04:20 +08:00
|
|
|
// logout doesn't work for guest sessions
|
|
|
|
// Also we sometimes want to re-log in a guest session
|
|
|
|
// if we abort the login
|
2016-09-17 09:19:27 +08:00
|
|
|
|
|
|
|
// use settimeout to avoid racing with react unmounting components
|
|
|
|
// which need a valid matrixclientpeg
|
|
|
|
setTimeout(()=>{
|
|
|
|
onLoggedOut();
|
|
|
|
}, 0);
|
2016-08-02 21:04:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-16 05:33:00 +08:00
|
|
|
_isLoggingOut = true;
|
2017-05-05 01:03:35 +08:00
|
|
|
MatrixClientPeg.get().logout().then(onLoggedOut,
|
2016-08-03 17:01:23 +08:00
|
|
|
(err) => {
|
|
|
|
// Just throwing an error here is going to be very unhelpful
|
|
|
|
// if you're trying to log out because your server's down and
|
|
|
|
// you want to log into a different server, so just forget the
|
|
|
|
// access token. It's annoying that this will leave the access
|
|
|
|
// token still valid, but we should fix this by having access
|
|
|
|
// tokens expire (and if you really think you've been compromised,
|
|
|
|
// change your password).
|
|
|
|
console.log("Failed to call logout API: token will not be invalidated");
|
2016-08-03 18:47:29 +08:00
|
|
|
onLoggedOut();
|
2017-05-05 01:03:35 +08:00
|
|
|
},
|
|
|
|
).done();
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 06:46:37 +08:00
|
|
|
export function softLogout() {
|
|
|
|
if (!MatrixClientPeg.get()) return;
|
|
|
|
|
|
|
|
// Track that we've detected and trapped a soft logout. This helps prevent other
|
|
|
|
// parts of the app from starting if there's no point (ie: don't sync if we've
|
|
|
|
// been soft logged out, despite having credentials and data for a MatrixClient).
|
|
|
|
localStorage.setItem("mx_soft_logout", "true");
|
|
|
|
|
|
|
|
_isLoggingOut = true; // to avoid repeated flags
|
|
|
|
stopMatrixClient(/*unsetClient=*/false);
|
|
|
|
dis.dispatch({action: 'on_client_not_viable'}); // generic version of on_logged_out
|
|
|
|
|
|
|
|
// DO NOT CALL LOGOUT. A soft logout preserves data, logout does not.
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isSoftLogout() {
|
|
|
|
return localStorage.getItem("mx_soft_logout") === "true";
|
|
|
|
}
|
|
|
|
|
2018-07-16 05:33:00 +08:00
|
|
|
export function isLoggingOut() {
|
|
|
|
return _isLoggingOut;
|
|
|
|
}
|
|
|
|
|
2016-08-03 01:56:12 +08:00
|
|
|
/**
|
|
|
|
* Starts the matrix client and all other react-sdk services that
|
|
|
|
* listen for events while a session is logged in.
|
2019-07-04 06:46:37 +08:00
|
|
|
* @param {boolean} startSyncing True (default) to actually start
|
|
|
|
* syncing the client.
|
2016-08-03 01:56:12 +08:00
|
|
|
*/
|
2019-07-04 06:46:37 +08:00
|
|
|
async function startMatrixClient(startSyncing=true) {
|
2017-07-05 23:20:21 +08:00
|
|
|
console.log(`Lifecycle: Starting MatrixClient`);
|
|
|
|
|
2016-08-02 21:04:20 +08:00
|
|
|
// dispatch this before starting the matrix client: it's used
|
|
|
|
// to add listeners for the 'sync' event so otherwise we'd have
|
|
|
|
// a race condition (and we need to dispatch synchronously for this
|
|
|
|
// to work).
|
|
|
|
dis.dispatch({action: 'will_start_client'}, true);
|
|
|
|
|
|
|
|
Notifier.start();
|
2019-03-08 20:46:38 +08:00
|
|
|
UserActivity.sharedInstance().start();
|
2019-06-27 12:36:55 +08:00
|
|
|
TypingStore.sharedInstance().reset(); // just in case
|
2019-02-09 00:44:03 +08:00
|
|
|
if (!SettingsStore.getValue("lowBandwidth")) {
|
|
|
|
Presence.start();
|
|
|
|
}
|
2016-09-27 16:56:31 +08:00
|
|
|
DMRoomMap.makeShared().start();
|
2018-07-24 23:21:43 +08:00
|
|
|
ActiveWidgetStore.start();
|
2016-08-03 01:58:18 +08:00
|
|
|
|
2019-07-04 06:46:37 +08:00
|
|
|
if (startSyncing) {
|
|
|
|
await MatrixClientPeg.start();
|
|
|
|
} else {
|
|
|
|
console.warn("Caller requested only auxiliary services be started");
|
2019-07-05 06:45:40 +08:00
|
|
|
await MatrixClientPeg.assign();
|
2019-07-04 06:46:37 +08:00
|
|
|
}
|
2017-11-05 11:13:23 +08:00
|
|
|
|
|
|
|
// dispatch that we finished starting up to wire up any other bits
|
|
|
|
// of the matrix client that cannot be set prior to starting up.
|
|
|
|
dis.dispatch({action: 'client_started'});
|
2019-07-04 06:46:37 +08:00
|
|
|
|
|
|
|
if (isSoftLogout()) {
|
|
|
|
softLogout();
|
|
|
|
}
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 17:49:34 +08:00
|
|
|
/*
|
2017-06-01 00:28:46 +08:00
|
|
|
* Stops a running client and all related services, and clears persistent
|
|
|
|
* storage. Used after a session has been logged out.
|
2016-08-04 17:49:34 +08:00
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function onLoggedOut() {
|
2018-07-16 05:33:00 +08:00
|
|
|
_isLoggingOut = false;
|
2016-08-11 20:50:38 +08:00
|
|
|
stopMatrixClient();
|
2017-06-01 00:28:46 +08:00
|
|
|
_clearStorage().done();
|
2016-08-02 21:04:20 +08:00
|
|
|
dis.dispatch({action: 'on_logged_out'});
|
|
|
|
}
|
|
|
|
|
2017-06-01 00:28:46 +08:00
|
|
|
/**
|
|
|
|
* @returns {Promise} promise which resolves once the stores have been cleared
|
|
|
|
*/
|
|
|
|
function _clearStorage() {
|
2017-05-28 03:47:09 +08:00
|
|
|
Analytics.logout();
|
2017-06-01 00:28:46 +08:00
|
|
|
|
|
|
|
if (window.localStorage) {
|
|
|
|
window.localStorage.clear();
|
2016-09-02 18:25:10 +08:00
|
|
|
}
|
2017-06-01 00:28:46 +08:00
|
|
|
|
|
|
|
// create a temporary client to clear out the persistent stores.
|
|
|
|
const cli = createMatrixClient({
|
|
|
|
// we'll never make any requests, so can pass a bogus HS URL
|
|
|
|
baseUrl: "",
|
|
|
|
});
|
|
|
|
return cli.clearStores();
|
2016-09-02 18:25:10 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 17:49:34 +08:00
|
|
|
/**
|
2017-06-01 00:28:46 +08:00
|
|
|
* Stop all the background processes related to the current client.
|
2019-07-04 06:46:37 +08:00
|
|
|
* @param {boolean} unsetClient True (default) to abandon the client
|
|
|
|
* on MatrixClientPeg after stopping.
|
2016-08-04 17:49:34 +08:00
|
|
|
*/
|
2019-07-04 06:46:37 +08:00
|
|
|
export function stopMatrixClient(unsetClient=true) {
|
2016-08-02 21:04:20 +08:00
|
|
|
Notifier.stop();
|
2019-03-08 23:09:44 +08:00
|
|
|
UserActivity.sharedInstance().stop();
|
2019-06-27 12:36:55 +08:00
|
|
|
TypingStore.sharedInstance().reset();
|
2016-08-02 21:04:20 +08:00
|
|
|
Presence.stop();
|
2018-07-24 23:39:30 +08:00
|
|
|
ActiveWidgetStore.stop();
|
2016-12-09 18:32:56 +08:00
|
|
|
if (DMRoomMap.shared()) DMRoomMap.shared().stop();
|
2017-05-05 01:03:35 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
2016-08-11 20:50:38 +08:00
|
|
|
if (cli) {
|
|
|
|
cli.stopClient();
|
|
|
|
cli.removeAllListeners();
|
2019-07-04 06:46:37 +08:00
|
|
|
|
|
|
|
if (unsetClient) {
|
|
|
|
MatrixClientPeg.unset();
|
|
|
|
}
|
2016-08-11 20:50:38 +08:00
|
|
|
}
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|