From bdc2c1087986599f1478590c278c622c83be8dae Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 28 Nov 2018 00:10:29 +0000 Subject: [PATCH 1/2] Factor out common login code Removes the duplication between the various points where we send off a login request and parse the response. --- src/Lifecycle.js | 21 +++------- src/Login.js | 99 ++++++++++++++++++------------------------------ 2 files changed, 42 insertions(+), 78 deletions(-) diff --git a/src/Lifecycle.js b/src/Lifecycle.js index b0912c759e0..f781f36fe4c 100644 --- a/src/Lifecycle.js +++ b/src/Lifecycle.js @@ -32,6 +32,7 @@ import Modal from './Modal'; import sdk from './index'; import ActiveWidgetStore from './stores/ActiveWidgetStore'; import PlatformPeg from "./PlatformPeg"; +import {sendLoginRequest} from "./Login"; /** * Called at startup, to attempt to build a logged-in Matrix session. It tries @@ -129,27 +130,17 @@ export function attemptTokenLogin(queryParams, defaultDeviceDisplayName) { return Promise.resolve(false); } - // create a temporary MatrixClient to do the login - const client = Matrix.createClient({ - baseUrl: queryParams.homeserver, - }); - - return client.login( + return sendLoginRequest( + queryParams.homeserver, + queryParams.identityServer, "m.login.token", { token: queryParams.loginToken, initial_device_display_name: defaultDeviceDisplayName, }, - ).then(function(data) { + ).then(function(creds) { console.log("Logged in with token"); return _clearStorage().then(() => { - _persistCredentialsToLocalStorage({ - userId: data.user_id, - deviceId: data.device_id, - accessToken: data.access_token, - homeserverUrl: queryParams.homeserver, - identityServerUrl: queryParams.identityServer, - guest: false, - }); + _persistCredentialsToLocalStorage(creds); return true; }); }).catch((err) => { diff --git a/src/Login.js b/src/Login.js index ec55a1e8c7a..417351bc9a9 100644 --- a/src/Login.js +++ b/src/Login.js @@ -1,6 +1,7 @@ /* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd +Copyright 2018 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -17,7 +18,6 @@ limitations under the License. import Matrix from "matrix-js-sdk"; -import Promise from 'bluebird'; import url from 'url'; export default class Login { @@ -141,60 +141,20 @@ export default class Login { }; Object.assign(loginParams, legacyParams); - const client = this._createTemporaryClient(); - const tryFallbackHs = (originalError) => { - const fbClient = Matrix.createClient({ - baseUrl: self._fallbackHsUrl, - idBaseUrl: this._isUrl, - }); - - return fbClient.login('m.login.password', loginParams).then(function(data) { - return Promise.resolve({ - homeserverUrl: self._fallbackHsUrl, - identityServerUrl: self._isUrl, - userId: data.user_id, - deviceId: data.device_id, - accessToken: data.access_token, - }); - }).catch((fallback_error) => { + return sendLoginRequest( + self._fallbackHsUrl, this._isUrl, 'm.login.password', loginParams, + ).catch((fallback_error) => { console.log("fallback HS login failed", fallback_error); // throw the original error throw originalError; }); }; - const tryLowercaseUsername = (originalError) => { - const loginParamsLowercase = Object.assign({}, loginParams, { - user: username.toLowerCase(), - identifier: { - user: username.toLowerCase(), - }, - }); - return client.login('m.login.password', loginParamsLowercase).then(function(data) { - return Promise.resolve({ - homeserverUrl: self._hsUrl, - identityServerUrl: self._isUrl, - userId: data.user_id, - deviceId: data.device_id, - accessToken: data.access_token, - }); - }).catch((fallback_error) => { - console.log("Lowercase username login failed", fallback_error); - // throw the original error - throw originalError; - }); - }; let originalLoginError = null; - return client.login('m.login.password', loginParams).then(function(data) { - return Promise.resolve({ - homeserverUrl: self._hsUrl, - identityServerUrl: self._isUrl, - userId: data.user_id, - deviceId: data.device_id, - accessToken: data.access_token, - }); - }).catch((error) => { + return sendLoginRequest( + self._hsUrl, self._isUrl, 'm.login.password', loginParams, + ).catch((error) => { originalLoginError = error; if (error.httpStatus === 403) { if (self._fallbackHsUrl) { @@ -202,22 +162,6 @@ export default class Login { } } throw originalLoginError; - }).catch((error) => { - // We apparently squash case at login serverside these days: - // https://github.com/matrix-org/synapse/blob/1189be43a2479f5adf034613e8d10e3f4f452eb9/synapse/handlers/auth.py#L475 - // so this wasn't needed after all. Keeping the code around in case the - // the situation changes... - - /* - if ( - error.httpStatus === 403 && - loginParams.identifier.type === 'm.id.user' && - username.search(/[A-Z]/) > -1 - ) { - return tryLowercaseUsername(originalLoginError); - } - */ - throw originalLoginError; }).catch((error) => { console.log("Login failed", error); throw error; @@ -239,3 +183,32 @@ export default class Login { return client.getSsoLoginUrl(url.format(parsedUrl), loginType); } } + + +/** + * Send a login request to the given server, and format the response + * as a MatrixClientCreds + * + * @param {string} hs the base url of the Homeserver used to log in. + * @param {string} is the default identity server + * @param {string} loginType the type of login to do + * @param {object} loginParams the parameters for the login + * + * @returns {MatrixClientCreds} + */ +export async function sendLoginRequest(hs, is, loginType, loginParams) { + const client = Matrix.createClient({ + baseUrl: hs, + idBaseUrl: is, + }); + + const data = await client.login(loginType, loginParams); + + return { + homeserverUrl: hs, + identityServerUrl: is, + userId: data.user_id, + deviceId: data.device_id, + accessToken: data.access_token, + }; +} From f2f92a779a2bae1be348a1e172ea2767519e104b Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Wed, 5 Dec 2018 15:54:09 +0000 Subject: [PATCH 2/2] Fix docs and naming --- src/Login.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Login.js b/src/Login.js index 417351bc9a9..330eb8a8f54 100644 --- a/src/Login.js +++ b/src/Login.js @@ -189,24 +189,24 @@ export default class Login { * Send a login request to the given server, and format the response * as a MatrixClientCreds * - * @param {string} hs the base url of the Homeserver used to log in. - * @param {string} is the default identity server + * @param {string} hsUrl the base url of the Homeserver used to log in. + * @param {string} isUrl the base url of the default identity server * @param {string} loginType the type of login to do * @param {object} loginParams the parameters for the login * * @returns {MatrixClientCreds} */ -export async function sendLoginRequest(hs, is, loginType, loginParams) { +export async function sendLoginRequest(hsUrl, isUrl, loginType, loginParams) { const client = Matrix.createClient({ - baseUrl: hs, - idBaseUrl: is, + baseUrl: hsUrl, + idBaseUrl: isUrl, }); const data = await client.login(loginType, loginParams); return { - homeserverUrl: hs, - identityServerUrl: is, + homeserverUrl: hsUrl, + identityServerUrl: isUrl, userId: data.user_id, deviceId: data.device_id, accessToken: data.access_token,