1
1
/*
2
2
Copyright 2015, 2016 OpenMarket Ltd
3
3
Copyright 2017 Vector Creations Ltd
4
+ Copyright 2018 New Vector Ltd
4
5
5
6
Licensed under the Apache License, Version 2.0 (the "License");
6
7
you may not use this file except in compliance with the License.
@@ -17,7 +18,6 @@ limitations under the License.
17
18
18
19
import Matrix from "matrix-js-sdk" ;
19
20
20
- import Promise from 'bluebird' ;
21
21
import url from 'url' ;
22
22
23
23
export default class Login {
@@ -141,83 +141,27 @@ export default class Login {
141
141
} ;
142
142
Object . assign ( loginParams , legacyParams ) ;
143
143
144
- const client = this . _createTemporaryClient ( ) ;
145
-
146
144
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 ) => {
161
148
console . log ( "fallback HS login failed" , fallback_error ) ;
162
149
// throw the original error
163
150
throw originalError ;
164
151
} ) ;
165
152
} ;
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
- } ;
187
153
188
154
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 ) => {
198
158
originalLoginError = error ;
199
159
if ( error . httpStatus === 403 ) {
200
160
if ( self . _fallbackHsUrl ) {
201
161
return tryFallbackHs ( originalLoginError ) ;
202
162
}
203
163
}
204
164
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 ;
221
165
} ) . catch ( ( error ) => {
222
166
console . log ( "Login failed" , error ) ;
223
167
throw error ;
@@ -239,3 +183,32 @@ export default class Login {
239
183
return client . getSsoLoginUrl ( url . format ( parsedUrl ) , loginType ) ;
240
184
}
241
185
}
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