2016-08-02 21:04:20 +08:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-08-10 18:33:58 +08:00
|
|
|
import q from 'q';
|
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';
|
|
|
|
import Notifier from './Notifier'
|
|
|
|
import UserActivity from './UserActivity';
|
|
|
|
import Presence from './Presence';
|
|
|
|
import dis from './dispatcher';
|
|
|
|
|
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:
|
|
|
|
*
|
|
|
|
* 0. if it looks like we are in the middle of a registration process, it does
|
|
|
|
* nothing.
|
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* 1. if we have a loginToken in the (real) query params, it uses that to log
|
|
|
|
* in.
|
2016-08-10 18:33:58 +08:00
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* 2. if we have a guest access token in the fragment query params, it uses
|
|
|
|
* that.
|
|
|
|
*
|
|
|
|
* 3. if an access token is stored in local storage (from a previous session),
|
2016-08-10 18:33:58 +08:00
|
|
|
* it uses that.
|
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* 4. it attempts to auto-register as a guest user.
|
2016-08-10 18:33:58 +08:00
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* If any of steps 1-4 are successful, it will call {setLoggedIn}, which in
|
2016-08-10 18:33:58 +08:00
|
|
|
* turn will raise on_logged_in and will_start_client events.
|
|
|
|
*
|
|
|
|
* It returns a promise which resolves when the above process completes.
|
|
|
|
*
|
2016-08-11 18:00:15 +08:00
|
|
|
* @param {object} opts.realQueryParams: string->string map of the
|
|
|
|
* query-parameters extracted from the real query-string of the starting
|
|
|
|
* URI.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export function loadSession(opts) {
|
2016-08-11 18:00:15 +08:00
|
|
|
const realQueryParams = opts.realQueryParams || {};
|
|
|
|
const fragmentQueryParams = opts.fragmentQueryParams || {};
|
2016-08-10 18:33:58 +08:00
|
|
|
let enableGuest = opts.enableGuest || false;
|
2016-08-11 08:30:43 +08:00
|
|
|
const guestHsUrl = opts.guestHsUrl;
|
|
|
|
const guestIsUrl = opts.guestIsUrl;
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2016-08-11 18:00:15 +08:00
|
|
|
if (fragmentQueryParams.client_secret && fragmentQueryParams.sid) {
|
2016-08-10 18:33:58 +08:00
|
|
|
// this happens during email validation: the email contains a link to the
|
|
|
|
// IS, which in turn redirects back to vector. We let MatrixChat create a
|
|
|
|
// Registration component which completes the next stage of registration.
|
|
|
|
console.log("Not registering as guest: registration already in progress.");
|
|
|
|
return q();
|
|
|
|
}
|
|
|
|
|
2016-08-11 08:30:43 +08:00
|
|
|
if (!guestHsUrl) {
|
2016-08-10 18:33:58 +08:00
|
|
|
console.warn("Cannot enable guest access: can't determine HS URL to use");
|
|
|
|
enableGuest = false;
|
|
|
|
}
|
|
|
|
|
2016-08-11 18:00:15 +08:00
|
|
|
if (realQueryParams.loginToken) {
|
|
|
|
if (!realQueryParams.homeserver) {
|
|
|
|
console.warn("Cannot log in with token: can't determine HS URL to use");
|
|
|
|
} else {
|
|
|
|
return _loginWithToken(realQueryParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:33:58 +08:00
|
|
|
if (enableGuest &&
|
2016-08-11 18:00:15 +08:00
|
|
|
fragmentQueryParams.guest_user_id &&
|
|
|
|
fragmentQueryParams.guest_access_token
|
2016-08-10 18:33:58 +08:00
|
|
|
) {
|
|
|
|
console.log("Using guest access credentials");
|
|
|
|
setLoggedIn({
|
2016-08-11 18:00:15 +08:00
|
|
|
userId: fragmentQueryParams.guest_user_id,
|
|
|
|
accessToken: fragmentQueryParams.guest_access_token,
|
2016-08-11 08:30:43 +08:00
|
|
|
homeserverUrl: guestHsUrl,
|
|
|
|
identityServerUrl: guestIsUrl,
|
2016-08-10 18:33:58 +08:00
|
|
|
guest: true,
|
|
|
|
});
|
|
|
|
return q();
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:33:27 +08:00
|
|
|
if (_restoreFromLocalStorage()) {
|
2016-08-10 18:33:58 +08:00
|
|
|
return q();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableGuest) {
|
2016-08-11 08:30:43 +08:00
|
|
|
return _registerAsGuest(guestHsUrl, guestIsUrl);
|
2016-08-10 18:33:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// fall back to login screen
|
|
|
|
return q();
|
|
|
|
}
|
|
|
|
|
2016-08-11 18:00:15 +08:00
|
|
|
function _loginWithToken(queryParams) {
|
|
|
|
// create a temporary MatrixClient to do the login
|
|
|
|
var client = Matrix.createClient({
|
|
|
|
baseUrl: queryParams.homeserver,
|
|
|
|
});
|
|
|
|
|
|
|
|
return client.loginWithToken(queryParams.loginToken).then(function(data) {
|
|
|
|
console.log("Logged in with token");
|
|
|
|
setLoggedIn({
|
|
|
|
userId: data.user_id,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: data.device_id,
|
2016-08-11 18:00:15 +08:00
|
|
|
accessToken: data.access_token,
|
|
|
|
homeserverUrl: queryParams.homeserver,
|
|
|
|
identityServerUrl: queryParams.identityServer,
|
|
|
|
guest: false,
|
|
|
|
})
|
|
|
|
}, (err) => {
|
|
|
|
console.error("Failed to log in with login token: " + err + " " +
|
|
|
|
err.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:33:58 +08:00
|
|
|
function _registerAsGuest(hsUrl, isUrl) {
|
|
|
|
console.log("Doing guest login on %s", hsUrl);
|
|
|
|
|
2016-08-11 20:50:38 +08:00
|
|
|
// create a temporary MatrixClient to do the login
|
|
|
|
var client = Matrix.createClient({
|
|
|
|
baseUrl: hsUrl,
|
|
|
|
});
|
|
|
|
|
|
|
|
return client.registerGuest().then((creds) => {
|
2016-08-10 18:33:58 +08:00
|
|
|
console.log("Registered as guest: %s", creds.user_id);
|
|
|
|
setLoggedIn({
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
console.error("Failed to register as guest: " + err + " " + err.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:33:27 +08:00
|
|
|
// returns true if a session is found in localstorage
|
|
|
|
function _restoreFromLocalStorage() {
|
|
|
|
if (!localStorage) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const hs_url = localStorage.getItem("mx_hs_url");
|
|
|
|
const is_url = localStorage.getItem("mx_is_url") || 'https://matrix.org';
|
|
|
|
const access_token = localStorage.getItem("mx_access_token");
|
|
|
|
const user_id = localStorage.getItem("mx_user_id");
|
2016-08-11 21:21:52 +08:00
|
|
|
const device_id = localStorage.getItem("mx_device_id");
|
2016-08-10 17:33:27 +08:00
|
|
|
|
|
|
|
let is_guest;
|
|
|
|
if (localStorage.getItem("mx_is_guest") !== null) {
|
|
|
|
is_guest = localStorage.getItem("mx_is_guest") === "true";
|
|
|
|
} else {
|
|
|
|
// legacy key name
|
|
|
|
is_guest = localStorage.getItem("matrix-is-guest") === "true";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (access_token && user_id && hs_url) {
|
|
|
|
console.log("Restoring session for %s", user_id);
|
|
|
|
setLoggedIn({
|
|
|
|
userId: user_id,
|
2016-08-11 21:21:52 +08:00
|
|
|
deviceId: device_id,
|
2016-08-10 17:33:27 +08:00
|
|
|
accessToken: access_token,
|
|
|
|
homeserverUrl: hs_url,
|
|
|
|
identityServerUrl: is_url,
|
|
|
|
guest: is_guest,
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
console.log("No previous session found.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-10 18:33:58 +08:00
|
|
|
|
2016-08-03 01:52:56 +08:00
|
|
|
/**
|
|
|
|
* Transitions to a logged-in state using the given credentials
|
2016-08-03 23:31:42 +08:00
|
|
|
* @param {MatrixClientCreds} credentials The credentials to use
|
2016-08-03 01:52:56 +08:00
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function setLoggedIn(credentials) {
|
2016-08-02 21:04:20 +08:00
|
|
|
credentials.guest = Boolean(credentials.guest);
|
2016-08-10 18:33:58 +08:00
|
|
|
console.log("setLoggedIn => %s (guest=%s) hs=%s",
|
2016-08-10 07:03:29 +08:00
|
|
|
credentials.userId, credentials.guest,
|
|
|
|
credentials.homeserverUrl);
|
2016-08-11 01:04:22 +08:00
|
|
|
|
|
|
|
// persist the session
|
|
|
|
if (localStorage) {
|
|
|
|
try {
|
2016-08-11 07:58:48 +08:00
|
|
|
localStorage.setItem("mx_hs_url", credentials.homeserverUrl);
|
2016-08-11 06:52:09 +08:00
|
|
|
localStorage.setItem("mx_is_url", credentials.identityServerUrl);
|
|
|
|
|
|
|
|
localStorage.setItem("mx_user_id", credentials.userId);
|
2016-08-11 21:21:52 +08:00
|
|
|
localStorage.setItem("mx_device_id", credentials.deviceId);
|
2016-08-11 06:52:09 +08:00
|
|
|
localStorage.setItem("mx_access_token", credentials.accessToken);
|
|
|
|
localStorage.setItem("mx_is_guest", JSON.stringify(credentials.guest));
|
|
|
|
console.log("Session persisted for %s", credentials.userId);
|
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
|
|
|
|
|
|
|
dis.dispatch({action: 'on_logged_in'});
|
|
|
|
|
2016-08-03 01:52:56 +08:00
|
|
|
startMatrixClient();
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
2016-08-02 21:04:20 +08:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
// logout doesn't work for guest sessions
|
|
|
|
// Also we sometimes want to re-log in a guest session
|
|
|
|
// if we abort the login
|
2016-08-03 18:47:29 +08:00
|
|
|
onLoggedOut();
|
2016-08-02 21:04:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:47:29 +08:00
|
|
|
return 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();
|
2016-08-03 17:01:23 +08:00
|
|
|
}
|
2016-08-02 21:04:20 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function startMatrixClient() {
|
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();
|
|
|
|
UserActivity.start();
|
|
|
|
Presence.start();
|
2016-08-03 01:58:18 +08:00
|
|
|
|
2016-08-03 23:39:47 +08:00
|
|
|
MatrixClientPeg.start();
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|
|
|
|
|
2016-08-04 17:49:34 +08:00
|
|
|
/*
|
|
|
|
* Stops a running client and all related services, used after
|
|
|
|
* a session has been logged out / ended.
|
|
|
|
*/
|
2016-08-10 18:33:58 +08:00
|
|
|
export function onLoggedOut() {
|
2016-08-02 21:04:20 +08:00
|
|
|
if (window.localStorage) {
|
|
|
|
const hsUrl = window.localStorage.getItem("mx_hs_url");
|
|
|
|
const isUrl = window.localStorage.getItem("mx_is_url");
|
|
|
|
window.localStorage.clear();
|
|
|
|
// preserve our HS & IS URLs for convenience
|
|
|
|
// N.B. we cache them in hsUrl/isUrl and can't really inline them
|
|
|
|
// as getCurrentHsUrl() may call through to localStorage.
|
2016-08-11 01:04:22 +08:00
|
|
|
// NB. We do clear the device ID (as well as all the settings)
|
2016-08-02 21:04:20 +08:00
|
|
|
if (hsUrl) window.localStorage.setItem("mx_hs_url", hsUrl);
|
|
|
|
if (isUrl) window.localStorage.setItem("mx_is_url", isUrl);
|
|
|
|
}
|
2016-08-11 20:50:38 +08:00
|
|
|
stopMatrixClient();
|
2016-08-02 21:04:20 +08:00
|
|
|
|
|
|
|
dis.dispatch({action: 'on_logged_out'});
|
|
|
|
}
|
|
|
|
|
2016-08-04 17:49:34 +08:00
|
|
|
/**
|
|
|
|
* Stop all the background processes related to the current client
|
|
|
|
*/
|
2016-08-11 20:50:38 +08:00
|
|
|
export function stopMatrixClient() {
|
2016-08-02 21:04:20 +08:00
|
|
|
Notifier.stop();
|
|
|
|
UserActivity.stop();
|
|
|
|
Presence.stop();
|
2016-08-11 20:50:38 +08:00
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
if (cli) {
|
|
|
|
cli.stopClient();
|
|
|
|
cli.removeAllListeners();
|
|
|
|
MatrixClientPeg.unset();
|
|
|
|
}
|
2016-08-02 21:04:20 +08:00
|
|
|
}
|