From 5312a869e4d6a48564c3cf50adf2e7b6f044a1c0 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 27 Oct 2017 18:36:59 +0100 Subject: [PATCH 1/4] Try lowercase username on login Fixes https://github.com/vector-im/riot-web/issues/5446 --- src/Login.js | 74 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/Login.js b/src/Login.js index 0eff94ce60..f1fb9a5b61 100644 --- a/src/Login.js +++ b/src/Login.js @@ -143,6 +143,48 @@ 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, + }); + }, function(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._fallbackHsUrl, + identityServerUrl: self._isUrl, + userId: data.user_id, + deviceId: data.device_id, + accessToken: data.access_token, + }); + }, function(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, @@ -151,29 +193,23 @@ export default class Login { deviceId: data.device_id, accessToken: data.access_token, }); - }, function(error) { + }).catch((error) => { + originalLoginError = error; if (error.httpStatus === 403) { if (self._fallbackHsUrl) { - 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, - }); - }, function(fallback_error) { - // throw the original error - throw error; - }); + return tryFallbackHs(originalLoginError); } } - throw error; + throw originalLoginError; + }).catch((error) => { + if ( + error.httpStatus === 403 && + loginParams.identifier.type === 'm.id.user' && + username.search(/[A-Z]/) > -1 + ) { + return tryLowercaseUsername(originalLoginError); + } + throw originalLoginError; }); } From b437a2559dd62207272bda52221c46e7c4543230 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 27 Oct 2017 18:59:13 +0100 Subject: [PATCH 2/4] PR feedback --- src/Login.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Login.js b/src/Login.js index f1fb9a5b61..9039c9f511 100644 --- a/src/Login.js +++ b/src/Login.js @@ -158,7 +158,7 @@ export default class Login { deviceId: data.device_id, accessToken: data.access_token, }); - }, function(fallback_error) { + }).catch((fallback_error) => { // throw the original error throw originalError; }); @@ -172,13 +172,13 @@ export default class Login { }); return client.login('m.login.password', loginParamsLowercase).then(function(data) { return Promise.resolve({ - homeserverUrl: self._fallbackHsUrl, + homeserverUrl: self._hsUrl, identityServerUrl: self._isUrl, userId: data.user_id, deviceId: data.device_id, accessToken: data.access_token, }); - }, function(fallback_error) { + }).catch((fallback_error) => { // throw the original error throw originalError; }); From 6874f313e335ce333e6499e141f30089f75d720b Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 30 Oct 2017 17:04:21 +0000 Subject: [PATCH 3/4] log login errors --- src/Login.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Login.js b/src/Login.js index 9039c9f511..bc28d5ce8a 100644 --- a/src/Login.js +++ b/src/Login.js @@ -210,6 +210,9 @@ export default class Login { return tryLowercaseUsername(originalLoginError); } throw originalLoginError; + }).catch((error) => { + console.log("Login failed", error); + throw error; }); } From 5ea19e27519f29ee6a9e095883f793b4e5633e01 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 30 Oct 2017 17:15:27 +0000 Subject: [PATCH 4/4] Log errors from other login attempts --- src/Login.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Login.js b/src/Login.js index bc28d5ce8a..55e996ce80 100644 --- a/src/Login.js +++ b/src/Login.js @@ -159,6 +159,7 @@ export default class Login { accessToken: data.access_token, }); }).catch((fallback_error) => { + console.log("fallback HS login failed", fallback_error); // throw the original error throw originalError; }); @@ -179,6 +180,7 @@ export default class Login { accessToken: data.access_token, }); }).catch((fallback_error) => { + console.log("Lowercase username login failed", fallback_error); // throw the original error throw originalError; });