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.
|
|
|
|
*/
|
|
|
|
|
2021-12-22 21:49:37 +05:30
|
|
|
import {Client} from "../../matrix/Client.js";
|
2022-07-01 06:50:13 +05:30
|
|
|
import {Options as BaseOptions, ViewModel} from "../ViewModel";
|
2021-08-15 16:21:00 +05:30
|
|
|
import {PasswordLoginViewModel} from "./PasswordLoginViewModel.js";
|
2021-08-17 17:01:03 +05:30
|
|
|
import {StartSSOLoginViewModel} from "./StartSSOLoginViewModel.js";
|
|
|
|
import {CompleteSSOLoginViewModel} from "./CompleteSSOLoginViewModel.js";
|
2021-12-22 21:39:52 +05:30
|
|
|
import {LoadStatus} from "../../matrix/Client.js";
|
2021-08-20 15:19:42 +05:30
|
|
|
import {SessionLoadViewModel} from "../SessionLoadViewModel.js";
|
2022-07-07 06:27:57 +05:30
|
|
|
import type {PasswordLoginMethod, SSOLoginHelper, TokenLoginMethod, ILoginMethod} from "../../matrix/login";
|
2022-07-01 06:50:13 +05:30
|
|
|
|
|
|
|
type Options = {
|
|
|
|
defaultHomeserver: string;
|
|
|
|
ready: ReadyFn;
|
|
|
|
loginToken?: string;
|
|
|
|
} & BaseOptions;
|
|
|
|
|
|
|
|
export class LoginViewModel extends ViewModel<Options> {
|
|
|
|
private _ready: ReadyFn;
|
|
|
|
private _loginToken?: string;
|
|
|
|
private _client: Client;
|
|
|
|
private _loginOptions?: LoginOptions;
|
|
|
|
private _passwordLoginViewModel?: PasswordLoginViewModel;
|
|
|
|
private _startSSOLoginViewModel?: StartSSOLoginViewModel;
|
|
|
|
private _completeSSOLoginViewModel?: CompleteSSOLoginViewModel;
|
|
|
|
private _loadViewModel?: SessionLoadViewModel;
|
|
|
|
private _loadViewModelSubscription?: () => void;
|
|
|
|
private _homeserver: string;
|
|
|
|
private _queriedHomeserver?: string;
|
|
|
|
private _abortHomeserverQueryTimeout?: () => void;
|
|
|
|
private _abortQueryOperation?: () => void;
|
|
|
|
|
2022-07-07 06:27:57 +05:30
|
|
|
private _hideHomeserver: boolean = false;
|
|
|
|
private _isBusy: boolean = false;
|
|
|
|
private _errorMessage: string = "";
|
2022-07-01 06:50:13 +05:30
|
|
|
|
|
|
|
constructor(options: Readonly<Options>) {
|
2021-08-15 16:21:00 +05:30
|
|
|
super(options);
|
2021-12-22 21:49:37 +05:30
|
|
|
const {ready, defaultHomeserver, loginToken} = options;
|
2021-08-15 16:21:00 +05:30
|
|
|
this._ready = ready;
|
|
|
|
this._loginToken = loginToken;
|
2021-12-22 21:49:37 +05:30
|
|
|
this._client = new Client(this.platform);
|
2021-08-23 22:56:39 +05:30
|
|
|
this._homeserver = defaultHomeserver;
|
2022-07-01 06:50:13 +05:30
|
|
|
void this._initViewModels();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2022-07-07 06:27:57 +05:30
|
|
|
get passwordLoginViewModel(): PasswordLoginViewModel {
|
|
|
|
return this._passwordLoginViewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
get startSSOLoginViewModel(): StartSSOLoginViewModel {
|
|
|
|
return this._startSSOLoginViewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
get completeSSOLoginViewModel(): CompleteSSOLoginViewModel {
|
|
|
|
return this._completeSSOLoginViewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
get homeserver(): string {
|
|
|
|
return this._homeserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
get resolvedHomeserver(): string | undefined {
|
|
|
|
return this._loginOptions?.homeserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
get errorMessage(): string {
|
|
|
|
return this._errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
get showHomeserver(): boolean {
|
|
|
|
return !this._hideHomeserver;
|
|
|
|
}
|
|
|
|
|
|
|
|
get loadViewModel(): SessionLoadViewModel {
|
|
|
|
return this._loadViewModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isBusy(): boolean {
|
|
|
|
return this._isBusy;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isFetchingLoginOptions(): boolean {
|
|
|
|
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(
|
|
|
|
{
|
2021-12-22 21:39:52 +05:30
|
|
|
client: this._client,
|
2022-07-07 06:27:57 +05:30
|
|
|
attemptLogin: (loginMethod: TokenLoginMethod) => this.attemptLogin(loginMethod),
|
2021-08-20 15:42:43 +05:30
|
|
|
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 22:56:39 +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,
|
2022-07-01 06:50:13 +05:30
|
|
|
attemptLogin: (loginMethod: PasswordLoginMethod) => 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
|
|
|
}
|
|
|
|
|
2022-07-01 06:50:13 +05:30
|
|
|
_showError(message: string) {
|
2021-08-18 13:05:05 +05:30
|
|
|
this._errorMessage = message;
|
|
|
|
this.emitChange("errorMessage");
|
2021-08-20 15:19:42 +05:30
|
|
|
}
|
|
|
|
|
2022-07-01 06:50:13 +05:30
|
|
|
_setBusy(status: boolean) {
|
2021-08-23 16:09:40 +05:30
|
|
|
this._isBusy = status;
|
|
|
|
this._passwordLoginViewModel?.setBusy(status);
|
|
|
|
this._startSSOLoginViewModel?.setBusy(status);
|
2021-08-20 15:19:42 +05:30
|
|
|
this.emitChange("isBusy");
|
|
|
|
}
|
|
|
|
|
2022-07-27 09:24:06 +05:30
|
|
|
async attemptLogin(loginMethod: ILoginMethod): Promise<null> {
|
2021-08-23 16:09:40 +05:30
|
|
|
this._setBusy(true);
|
2022-07-27 09:24:06 +05:30
|
|
|
void this._client.startWithLogin(loginMethod, {inspectAccountSetup: true});
|
2021-12-22 21:39:52 +05:30
|
|
|
const loadStatus = this._client.loadStatus;
|
2022-07-01 06:50:13 +05:30
|
|
|
const handle = loadStatus.waitFor((status: LoadStatus) => status !== LoadStatus.Login);
|
2021-08-20 15:19:42 +05:30
|
|
|
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) {
|
2021-12-22 21:39:52 +05:30
|
|
|
return this._client.loginFailure;
|
2021-08-20 15:19:42 +05:30
|
|
|
}
|
|
|
|
this._hideHomeserver = true;
|
2021-08-20 23:03:51 +05:30
|
|
|
this.emitChange("hideHomeserver");
|
2021-08-20 15:19:42 +05:30
|
|
|
this._disposeViewModels();
|
2022-07-27 09:24:06 +05:30
|
|
|
void this._createLoadViewModel();
|
2021-08-20 15:19:42 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-27 09:24:06 +05:30
|
|
|
_createLoadViewModel(): void {
|
2021-08-20 15:19:42 +05:30
|
|
|
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({
|
2021-12-22 21:39:52 +05:30
|
|
|
ready: (client) => {
|
2021-08-20 15:42:43 +05:30
|
|
|
// make sure we don't delete the session in dispose when navigating away
|
2021-12-22 21:39:52 +05:30
|
|
|
this._client = null;
|
|
|
|
this._ready(client);
|
2021-08-20 15:42:43 +05:30
|
|
|
},
|
2021-12-22 21:39:52 +05:30
|
|
|
client: this._client,
|
2021-08-20 15:42:43 +05:30
|
|
|
homeserver: this._homeserver
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2022-07-27 09:24:06 +05:30
|
|
|
void this._loadViewModel.start();
|
2021-08-20 15:19:42 +05:30
|
|
|
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() {
|
2022-07-01 06:50:13 +05:30
|
|
|
this._startSSOLoginViewModel = this.disposeTracked(this._startSSOLoginViewModel);
|
2021-08-18 13:05:05 +05:30
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-07-27 09:24:06 +05:30
|
|
|
async setHomeserver(newHomeserver: string): void {
|
2021-08-23 19:24:40 +05:30
|
|
|
this._homeserver = newHomeserver;
|
2021-08-23 22:58:21 +05:30
|
|
|
// clear everything set by queryHomeserver
|
2022-07-01 06:50:13 +05:30
|
|
|
this._loginOptions = undefined;
|
|
|
|
this._queriedHomeserver = undefined;
|
2021-08-23 22:58:21 +05:30
|
|
|
this._showError("");
|
|
|
|
this._disposeViewModels();
|
2021-08-23 19:24:40 +05:30
|
|
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
2022-07-27 09:14:15 +05:30
|
|
|
this.emitChange("loginViewModels"); // multiple fields changing
|
2021-08-23 22:58:21 +05:30
|
|
|
// also clear the timeout if it is still running
|
2021-08-23 19:24:40 +05:30
|
|
|
this.disposeTracked(this._abortHomeserverQueryTimeout);
|
2021-08-23 22:57:24 +05:30
|
|
|
const timeout = this.clock.createTimeout(1000);
|
2021-08-23 19:24:40 +05:30
|
|
|
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);
|
2022-07-27 09:24:06 +05:30
|
|
|
void this.queryHomeserver();
|
2021-08-23 19:24:40 +05:30
|
|
|
}
|
2022-07-01 06:50:13 +05:30
|
|
|
|
2022-07-27 09:24:06 +05:30
|
|
|
async queryHomeserver(): void {
|
2021-08-23 22:58:43 +05:30
|
|
|
// don't repeat a query we've just done
|
|
|
|
if (this._homeserver === this._queriedHomeserver || this._homeserver === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._queriedHomeserver = this._homeserver;
|
2021-08-23 22:58:21 +05:30
|
|
|
// given that setHomeserver already clears everything set here,
|
|
|
|
// and that is the only way to change the homeserver,
|
|
|
|
// we don't need to reset things again here.
|
|
|
|
// However, clear things set by setHomeserver:
|
|
|
|
// if query is called before the typing timeout hits (e.g. field lost focus),
|
|
|
|
// cancel the timeout so we don't query again.
|
2021-08-23 19:31:39 +05:30
|
|
|
this._abortHomeserverQueryTimeout = this.disposeTracked(this._abortHomeserverQueryTimeout);
|
|
|
|
// cancel ongoing query operation, if any
|
2021-08-23 19:24:40 +05:30
|
|
|
this._abortQueryOperation = this.disposeTracked(this._abortQueryOperation);
|
|
|
|
try {
|
2021-12-22 21:39:52 +05:30
|
|
|
const queryOperation = this._client.queryLogin(this._homeserver);
|
2021-08-23 19:24:40 +05:30
|
|
|
this._abortQueryOperation = this.track(() => queryOperation.abort());
|
|
|
|
this.emitChange("isFetchingLoginOptions");
|
|
|
|
this._loginOptions = await queryOperation.result;
|
2021-08-23 23:39:45 +05:30
|
|
|
this.emitChange("resolvedHomeserver");
|
2021-08-23 19:24:40 +05:30
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
if (e.name === "AbortError") {
|
|
|
|
return; //aborted, bail out
|
|
|
|
} else {
|
2022-07-01 06:50:13 +05:30
|
|
|
this._loginOptions = undefined;
|
2021-08-23 19:24:40 +05:30
|
|
|
}
|
|
|
|
} 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");
|
2022-07-01 06:50:13 +05:30
|
|
|
}
|
2021-08-23 19:24:40 +05:30
|
|
|
}
|
|
|
|
else {
|
2021-08-23 22:58:21 +05:30
|
|
|
this._showError(`Could not query login methods supported by ${this.homeserver}`);
|
2021-08-23 19:24:40 +05:30
|
|
|
}
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2022-07-27 09:24:06 +05:30
|
|
|
dispose(): void {
|
2021-08-15 16:21:00 +05:30
|
|
|
super.dispose();
|
2021-12-22 21:39:52 +05:30
|
|
|
if (this._client) {
|
2021-08-15 16:21:00 +05:30
|
|
|
// if we move away before we're done with initial sync
|
|
|
|
// delete the session
|
2022-07-27 09:24:06 +05:30
|
|
|
void this._client.deleteSession();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-01 06:50:13 +05:30
|
|
|
|
|
|
|
type ReadyFn = (client: Client) => void;
|
|
|
|
|
|
|
|
// TODO: move to Client.js when its converted to typescript.
|
|
|
|
type LoginOptions = {
|
|
|
|
homeserver: string;
|
|
|
|
password?: (username: string, password: string) => PasswordLoginMethod;
|
|
|
|
sso?: SSOLoginHelper;
|
|
|
|
token?: (loginToken: string) => TokenLoginMethod;
|
|
|
|
};
|