Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit c553323

Browse files
authored
Factor out common login code (#2307)
Removes the duplication between the various points where we send off a login request and parse the response.
1 parent a3382eb commit c553323

File tree

2 files changed

+42
-78
lines changed

2 files changed

+42
-78
lines changed

src/Lifecycle.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Modal from './Modal';
3232
import sdk from './index';
3333
import ActiveWidgetStore from './stores/ActiveWidgetStore';
3434
import PlatformPeg from "./PlatformPeg";
35+
import {sendLoginRequest} from "./Login";
3536

3637
/**
3738
* Called at startup, to attempt to build a logged-in Matrix session. It tries
@@ -129,27 +130,17 @@ export function attemptTokenLogin(queryParams, defaultDeviceDisplayName) {
129130
return Promise.resolve(false);
130131
}
131132

132-
// create a temporary MatrixClient to do the login
133-
const client = Matrix.createClient({
134-
baseUrl: queryParams.homeserver,
135-
});
136-
137-
return client.login(
133+
return sendLoginRequest(
134+
queryParams.homeserver,
135+
queryParams.identityServer,
138136
"m.login.token", {
139137
token: queryParams.loginToken,
140138
initial_device_display_name: defaultDeviceDisplayName,
141139
},
142-
).then(function(data) {
140+
).then(function(creds) {
143141
console.log("Logged in with token");
144142
return _clearStorage().then(() => {
145-
_persistCredentialsToLocalStorage({
146-
userId: data.user_id,
147-
deviceId: data.device_id,
148-
accessToken: data.access_token,
149-
homeserverUrl: queryParams.homeserver,
150-
identityServerUrl: queryParams.identityServer,
151-
guest: false,
152-
});
143+
_persistCredentialsToLocalStorage(creds);
153144
return true;
154145
});
155146
}).catch((err) => {

src/Login.js

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
Copyright 2015, 2016 OpenMarket Ltd
33
Copyright 2017 Vector Creations Ltd
4+
Copyright 2018 New Vector Ltd
45
56
Licensed under the Apache License, Version 2.0 (the "License");
67
you may not use this file except in compliance with the License.
@@ -17,7 +18,6 @@ limitations under the License.
1718

1819
import Matrix from "matrix-js-sdk";
1920

20-
import Promise from 'bluebird';
2121
import url from 'url';
2222

2323
export default class Login {
@@ -141,83 +141,27 @@ export default class Login {
141141
};
142142
Object.assign(loginParams, legacyParams);
143143

144-
const client = this._createTemporaryClient();
145-
146144
const tryFallbackHs = (originalError) => {
147-
const fbClient = Matrix.createClient({
148-
baseUrl: self._fallbackHsUrl,
149-
idBaseUrl: this._isUrl,
150-
});
151-
152-
return fbClient.login('m.login.password', loginParams).then(function(data) {
153-
return Promise.resolve({
154-
homeserverUrl: self._fallbackHsUrl,
155-
identityServerUrl: self._isUrl,
156-
userId: data.user_id,
157-
deviceId: data.device_id,
158-
accessToken: data.access_token,
159-
});
160-
}).catch((fallback_error) => {
145+
return sendLoginRequest(
146+
self._fallbackHsUrl, this._isUrl, 'm.login.password', loginParams,
147+
).catch((fallback_error) => {
161148
console.log("fallback HS login failed", fallback_error);
162149
// throw the original error
163150
throw originalError;
164151
});
165152
};
166-
const tryLowercaseUsername = (originalError) => {
167-
const loginParamsLowercase = Object.assign({}, loginParams, {
168-
user: username.toLowerCase(),
169-
identifier: {
170-
user: username.toLowerCase(),
171-
},
172-
});
173-
return client.login('m.login.password', loginParamsLowercase).then(function(data) {
174-
return Promise.resolve({
175-
homeserverUrl: self._hsUrl,
176-
identityServerUrl: self._isUrl,
177-
userId: data.user_id,
178-
deviceId: data.device_id,
179-
accessToken: data.access_token,
180-
});
181-
}).catch((fallback_error) => {
182-
console.log("Lowercase username login failed", fallback_error);
183-
// throw the original error
184-
throw originalError;
185-
});
186-
};
187153

188154
let originalLoginError = null;
189-
return client.login('m.login.password', loginParams).then(function(data) {
190-
return Promise.resolve({
191-
homeserverUrl: self._hsUrl,
192-
identityServerUrl: self._isUrl,
193-
userId: data.user_id,
194-
deviceId: data.device_id,
195-
accessToken: data.access_token,
196-
});
197-
}).catch((error) => {
155+
return sendLoginRequest(
156+
self._hsUrl, self._isUrl, 'm.login.password', loginParams,
157+
).catch((error) => {
198158
originalLoginError = error;
199159
if (error.httpStatus === 403) {
200160
if (self._fallbackHsUrl) {
201161
return tryFallbackHs(originalLoginError);
202162
}
203163
}
204164
throw originalLoginError;
205-
}).catch((error) => {
206-
// We apparently squash case at login serverside these days:
207-
// https://github.com/matrix-org/synapse/blob/1189be43a2479f5adf034613e8d10e3f4f452eb9/synapse/handlers/auth.py#L475
208-
// so this wasn't needed after all. Keeping the code around in case the
209-
// the situation changes...
210-
211-
/*
212-
if (
213-
error.httpStatus === 403 &&
214-
loginParams.identifier.type === 'm.id.user' &&
215-
username.search(/[A-Z]/) > -1
216-
) {
217-
return tryLowercaseUsername(originalLoginError);
218-
}
219-
*/
220-
throw originalLoginError;
221165
}).catch((error) => {
222166
console.log("Login failed", error);
223167
throw error;
@@ -239,3 +183,32 @@ export default class Login {
239183
return client.getSsoLoginUrl(url.format(parsedUrl), loginType);
240184
}
241185
}
186+
187+
188+
/**
189+
* Send a login request to the given server, and format the response
190+
* as a MatrixClientCreds
191+
*
192+
* @param {string} hsUrl the base url of the Homeserver used to log in.
193+
* @param {string} isUrl the base url of the default identity server
194+
* @param {string} loginType the type of login to do
195+
* @param {object} loginParams the parameters for the login
196+
*
197+
* @returns {MatrixClientCreds}
198+
*/
199+
export async function sendLoginRequest(hsUrl, isUrl, loginType, loginParams) {
200+
const client = Matrix.createClient({
201+
baseUrl: hsUrl,
202+
idBaseUrl: isUrl,
203+
});
204+
205+
const data = await client.login(loginType, loginParams);
206+
207+
return {
208+
homeserverUrl: hsUrl,
209+
identityServerUrl: isUrl,
210+
userId: data.user_id,
211+
deviceId: data.device_id,
212+
accessToken: data.access_token,
213+
};
214+
}

0 commit comments

Comments
 (0)