2021-08-15 16:21:00 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {ViewModel} from "../ViewModel.js";
|
|
|
|
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js";
|
2021-08-17 17:01:03 +05:30
|
|
|
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js";
|
|
|
|
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js";
|
2021-08-20 15:19:42 +05:30
|
|
|
import {LoadStatus} from "../../matrix/SessionContainer.js";
|
|
|
|
import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
|
2021-08-15 16:21:00 +05:30
|
|
|
|
|
|
|
export class LoginViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {ready, defaultHomeServer, createSessionContainer, loginToken} = options;
|
|
|
|
this._createSessionContainer = createSessionContainer;
|
|
|
|
this._ready = ready;
|
|
|
|
this._loginToken = loginToken;
|
|
|
|
this._sessionContainer = this._createSessionContainer();
|
|
|
|
this._loginOptions = null;
|
2021-08-17 17:01:03 +05:30
|
|
|
this._passwordLoginViewModel = null;
|
|
|
|
this._startSSOLoginViewModel = null;
|
|
|
|
this._completeSSOLoginViewModel = null;
|
2021-08-20 15:19:42 +05:30
|
|
|
this._loadViewModel = null;
|
|
|
|
this._loadViewModelSubscription = null;
|
2021-08-19 20:13:34 +05:30
|
|
|
this._homeserver = defaultHomeServer;
|
2021-08-18 13:05:05 +05:30
|
|
|
this._errorMessage = "";
|
2021-08-20 15:19:42 +05:30
|
|
|
this._hideHomeserver = false;
|
|
|
|
this._isBusy = false;
|
2021-08-23 19:24:40 +05:30
|
|
|
this._abortHomeserverQueryTimeout = null;
|
|
|
|
this._abortQueryOperation = null;
|
|
|
|
this._initViewModels();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get passwordLoginViewModel() { return this._passwordLoginViewModel; }
|
2021-08-17 17:01:03 +05:30
|
|
|
get startSSOLoginViewModel() { return this._startSSOLoginViewModel; }
|
|
|
|
get completeSSOLoginViewModel(){ return this._completeSSOLoginViewModel; }
|
2021-08-20 20:09:12 +05:30
|
|
|
get homeserver() { return this._homeserver; }
|
2021-08-18 13:05:05 +05:30
|
|
|
get errorMessage() { return this._errorMessage; }
|
2021-08-20 15:19:42 +05:30
|
|
|
get showHomeserver() { return !this._hideHomeserver; }
|
|
|
|
get loadViewModel() {return this._loadViewModel; }
|
|
|
|
get isBusy() { return this._isBusy; }
|
2021-08-23 19:24:40 +05:30
|
|
|
get isFetchingLoginOptions() { return !!this._abortQueryOperation; }
|
2021-08-15 16:21:00 +05:30
|
|
|
|
2021-08-20 23:16:03 +05:30
|
|
|
goBack() {
|
2021-08-23 15:55:07 +05:30
|
|
|
this.navigation.push("session");
|
2021-08-20 23:16:03 +05:30
|
|
|
}
|
|
|
|
|
2021-08-23 19:24:40 +05:30
|
|
|
async _initViewModels() {
|
2021-08-15 16:21:00 +05:30
|
|
|
if (this._loginToken) {
|
2021-08-20 15:19:42 +05:30
|
|
|
this._hideHomeserver = true;
|
2021-08-20 15:42:43 +05:30
|
|
|
this._completeSSOLoginViewModel = this.track(new CompleteSSOLoginViewModel(
|
|
|
|
this.childOptions(
|
|
|
|
{
|
|
|
|
sessionContainer: this._sessionContainer,
|
|
|
|
attemptLogin: loginMethod => this.attemptLogin(loginMethod),
|
|
|
|
loginToken: this._loginToken
|
|
|
|
})));
|
2021-08-17 17:01:03 +05:30
|
|
|
this.emitChange("completeSSOLoginViewModel");
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
else {
|
2021-08-23 19:24:40 +05:30
|
|
|
await this.queryHomeServer();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showPasswordLogin() {
|
2021-08-20 15:42:43 +05:30
|
|
|
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(
|
|
|
|
this.childOptions({
|
|
|
|
loginOptions: this._loginOptions,
|
2021-08-20 23:02:06 +05:30
|
|
|
attemptLogin: loginMethod => this.attemptLogin(loginMethod)
|
2021-08-20 15:42:43 +05:30
|
|
|
})));
|
2021-08-15 16:21:00 +05:30
|
|
|
this.emitChange("passwordLoginViewModel");
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:05:05 +05:30
|
|
|
_showSSOLogin() {
|
2021-08-20 15:42:43 +05:30
|
|
|
this._startSSOLoginViewModel = this.track(
|
2021-08-20 15:47:30 +05:30
|
|
|
new StartSSOLoginViewModel(this.childOptions({loginOptions: this._loginOptions}))
|
2021-08-20 15:42:43 +05:30
|
|
|
);
|
2021-08-17 17:01:03 +05:30
|
|
|
this.emitChange("startSSOLoginViewModel");
|
2021-08-18 13:05:05 +05:30
|
|
|
}
|
|
|
|
|
2021-08-20 21:43:42 +05:30
|
|
|
_showError(message) {
|
2021-08-18 13:05:05 +05:30
|
|
|
this._errorMessage = message;
|
|
|
|
this.emitChange("errorMessage");
|
2021-08-20 15:19:42 +05:30
|
|
|
}
|
|
|
|
|
2021-08-23 16:09:40 +05:30
|
|
|
_setBusy(status) {
|
|
|
|
this._isBusy = status;
|
|
|
|
this._passwordLoginViewModel?.setBusy(status);
|
|
|
|
this._startSSOLoginViewModel?.setBusy(status);
|
2021-08-20 15:19:42 +05:30
|
|
|
this.emitChange("isBusy");
|
|
|
|
}
|
|
|
|
|
|
|
|
async attemptLogin(loginMethod) {
|
2021-08-23 16:09:40 +05:30
|
|
|
this._setBusy(true);
|
2021-08-20 15:19:42 +05:30
|
|
|
this._sessionContainer.startWithLogin(loginMethod);
|
|
|
|
const loadStatus = this._sessionContainer.loadStatus;
|
|
|
|
const handle = loadStatus.waitFor(status => status !== LoadStatus.Login);
|
|
|
|
await handle.promise;
|
2021-08-23 16:09:40 +05:30
|
|
|
this._setBusy(false);
|
2021-08-20 15:19:42 +05:30
|
|
|
const status = loadStatus.get();
|
|
|
|
if (status === LoadStatus.LoginFailed) {
|
|
|
|
return this._sessionContainer.loginFailure;
|
|
|
|
}
|
|
|
|
this._hideHomeserver = true;
|
2021-08-20 23:03:51 +05:30
|
|
|
this.emitChange("hideHomeserver");
|
2021-08-20 15:19:42 +05:30
|
|
|
this._disposeViewModels();
|
|
|
|
this._createLoadViewModel();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_createLoadViewModel() {
|
|
|
|
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
|
2021-08-20 20:10:31 +05:30
|
|
|
this._loadViewModel = this.disposeTracked(this._loadViewModel);
|
2021-08-20 15:42:43 +05:30
|
|
|
this._loadViewModel = this.track(
|
|
|
|
new SessionLoadViewModel(
|
|
|
|
this.childOptions({
|
|
|
|
ready: (sessionContainer) => {
|
|
|
|
// make sure we don't delete the session in dispose when navigating away
|
|
|
|
this._sessionContainer = null;
|
|
|
|
this._ready(sessionContainer);
|
|
|
|
},
|
|
|
|
sessionContainer: this._sessionContainer,
|
|
|
|
homeserver: this._homeserver
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 15:19:42 +05:30
|
|
|
this._loadViewModel.start();
|
|
|
|
this.emitChange("loadViewModel");
|
|
|
|
this._loadViewModelSubscription = this.track(
|
|
|
|
this._loadViewModel.disposableOn("change", () => {
|
|
|
|
if (!this._loadViewModel.loading) {
|
|
|
|
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
|
|
|
|
}
|
2021-08-23 16:09:40 +05:30
|
|
|
this._setBusy(false);
|
2021-08-20 15:19:42 +05:30
|
|
|
})
|
|
|
|
);
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-18 13:05:05 +05:30
|
|
|
_disposeViewModels() {
|
|
|
|
this._startSSOLoginViewModel = this.disposeTracked(this._ssoLoginViewModel);
|
|
|
|
this._passwordLoginViewModel = this.disposeTracked(this._passwordLoginViewModel);
|
2021-08-20 15:19:42 +05:30
|
|
|
this._completeSSOLoginViewModel = this.disposeTracked(this._completeSSOLoginViewModel);
|
2021-08-18 13:05:05 +05:30
|
|
|
this.emitChange("disposeViewModels");
|
|
|
|
}
|
|
|
|
|
2021-08-23 19:27:16 +05:30
|
|
|
async setHomeServer(newHomeserver) {
|
2021-08-23 19:24:40 +05:30
|
|
|
this._homeserver = newHomeserver;
|
|
|
|
// abort ongoing query, if any
|
|
|
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
|
|
|
this.emitChange("isFetchingLoginOptions");
|
|
|
|
this.disposeTracked(this._abortHomeserverQueryTimeout);
|
|
|
|
const timeout = this.clock.createTimeout(2000);
|
|
|
|
this._abortHomeserverQueryTimeout = this.track(() => timeout.abort());
|
|
|
|
try {
|
|
|
|
await timeout.elapsed();
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name === "AbortError") {
|
|
|
|
return; // still typing, don't query
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
|
|
|
|
this.queryHomeServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
async queryHomeServer() {
|
2021-08-23 15:28:44 +05:30
|
|
|
this._errorMessage = "";
|
|
|
|
this.emitChange("errorMessage");
|
2021-08-23 19:24:40 +05:30
|
|
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
2021-08-18 13:05:05 +05:30
|
|
|
this._disposeViewModels();
|
2021-08-23 19:24:40 +05:30
|
|
|
try {
|
|
|
|
const queryOperation = this._sessionContainer.queryLogin(this._homeserver);
|
|
|
|
this._abortQueryOperation = this.track(() => queryOperation.abort());
|
|
|
|
this.emitChange("isFetchingLoginOptions");
|
|
|
|
this._loginOptions = await queryOperation.result;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log("error", e);
|
|
|
|
if (e.name === "AbortError") {
|
|
|
|
return; //aborted, bail out
|
|
|
|
} else {
|
|
|
|
this._loginOptions = null;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
|
|
|
this.emitChange("isFetchingLoginOptions");
|
|
|
|
}
|
|
|
|
if (this._loginOptions) {
|
|
|
|
if (this._loginOptions.sso) { this._showSSOLogin(); }
|
|
|
|
if (this._loginOptions.password) { this._showPasswordLogin(); }
|
|
|
|
if (!this._loginOptions.sso && !this._loginOptions.password) {
|
|
|
|
this._showError("This homeserver supports neither SSO nor password based login flows");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._showError("Could not query login methods supported by the homeserver");
|
|
|
|
}
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
super.dispose();
|
|
|
|
if (this._sessionContainer) {
|
|
|
|
// if we move away before we're done with initial sync
|
|
|
|
// delete the session
|
|
|
|
this._sessionContainer.deleteSession();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|