From a53e29767f8d69a03c68dcd7128d77c883400dd5 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 30 Jul 2021 13:45:35 +0530 Subject: [PATCH] Rewrite password login to use PasswordLoginMethod Signed-off-by: RMidhunSuresh --- src/domain/LoginViewModel.js | 8 +++++--- src/domain/SessionLoadViewModel.js | 2 +- src/matrix/SessionContainer.js | 24 ++++++++++++++++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/domain/LoginViewModel.js b/src/domain/LoginViewModel.js index 9e1db503..4e9993aa 100644 --- a/src/domain/LoginViewModel.js +++ b/src/domain/LoginViewModel.js @@ -47,10 +47,12 @@ export class LoginViewModel extends ViewModel { this._loadViewModel = this.disposeTracked(this._loadViewModel); } this._loadViewModel = this.track(new SessionLoadViewModel(this.childOptions({ - createAndStartSessionContainer: () => { + createAndStartSessionContainer: async () => { this._sessionContainer = this._createSessionContainer(); - this._sessionContainer.queryLogin(homeserver); - this._sessionContainer.startWithLogin(homeserver, username, password); + const loginOptions = await this._sessionContainer.queryLogin(homeserver); + if (loginOptions.password) { + this._sessionContainer.startWithLogin(loginOptions.password(username, password)); + } return this._sessionContainer; }, ready: sessionContainer => { diff --git a/src/domain/SessionLoadViewModel.js b/src/domain/SessionLoadViewModel.js index 0b785e47..83bf5966 100644 --- a/src/domain/SessionLoadViewModel.js +++ b/src/domain/SessionLoadViewModel.js @@ -38,7 +38,7 @@ export class SessionLoadViewModel extends ViewModel { try { this._loading = true; this.emitChange("loading"); - this._sessionContainer = this._createAndStartSessionContainer(); + this._sessionContainer = await this._createAndStartSessionContainer(); this._waitHandle = this._sessionContainer.loadStatus.waitFor(s => { this.emitChange("loadLabel"); // wait for initial sync, but not catchup sync diff --git a/src/matrix/SessionContainer.js b/src/matrix/SessionContainer.js index 1106644c..cfc83255 100644 --- a/src/matrix/SessionContainer.js +++ b/src/matrix/SessionContainer.js @@ -23,6 +23,7 @@ import {MediaRepository} from "./net/MediaRepository.js"; import {RequestScheduler} from "./net/RequestScheduler.js"; import {Sync, SyncStatus} from "./Sync.js"; import {Session} from "./Session.js"; +import {PasswordLoginMethod} from "./PasswordLoginMethod.js"; export const LoadStatus = createEnum( "NotLoading", @@ -97,25 +98,40 @@ export class SessionContainer { }); } + parseLoginOptions(options, homeServer) { + /* Take server response and return new object which has two props password and sso which + implements LoginMethod + */ + const flows = options.flows; + const result = {}; + for (const flow of flows) { + if (flow.type === "m.login.password") { + result.password = (username, password) => new PasswordLoginMethod({homeServer, username, password, platform: this._platform}); + } + } + return result; + } + async queryLogin(homeServer) { homeServer = normalizeHomeserver(homeServer); const hsApi = new HomeServerApi({homeServer, request: this._platform.request}); - const response = hsApi.queryLogin(); + const response = await hsApi.queryLogin().response(); + return this.parseLoginOptions(response, homeServer); } - async startWithLogin(homeServer, username, password) { + async startWithLogin(loginMethod) { if (this._status.get() !== LoadStatus.NotLoading) { return; } await this._platform.logger.run("login", async log => { this._status.set(LoadStatus.Login); - homeServer = normalizeHomeserver(homeServer); const clock = this._platform.clock; let sessionInfo; try { const request = this._platform.request; + const homeServer = normalizeHomeserver(loginMethod.homeServer); const hsApi = new HomeServerApi({homeServer, request}); - const loginData = await hsApi.passwordLogin(username, password, "Hydrogen", {log}).response(); + const loginData = await loginMethod.login(hsApi, "Hydrogen", log); const sessionId = this.createNewSessionId(); sessionInfo = { id: sessionId,