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-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-19 20:13:34 +05:30
|
|
|
this._homeserver = defaultHomeServer;
|
2021-08-18 13:05:05 +05:30
|
|
|
this._errorMessage = "";
|
2021-08-19 20:25:54 +05:30
|
|
|
this._createViewModels(this._homeserver);
|
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-19 20:13:34 +05:30
|
|
|
get defaultHomeServer() { return this._homeserver; }
|
2021-08-18 13:05:05 +05:30
|
|
|
get errorMessage() { return this._errorMessage; }
|
2021-08-19 20:35:07 +05:30
|
|
|
get showHomeserver() { return !this._completeSSOLoginViewModel; }
|
2021-08-19 21:37:26 +05:30
|
|
|
get cancelUrl() { return this.urlCreator.urlForSegment("session"); }
|
2021-08-15 16:21:00 +05:30
|
|
|
|
2021-08-19 20:25:54 +05:30
|
|
|
async _createViewModels(homeserver) {
|
2021-08-15 16:21:00 +05:30
|
|
|
if (this._loginToken) {
|
2021-08-17 17:01:03 +05:30
|
|
|
this._completeSSOLoginViewModel = this.track(new CompleteSSOLoginViewModel(this.childOptions({loginToken: this._loginToken})));
|
|
|
|
this.emitChange("completeSSOLoginViewModel");
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
else {
|
2021-08-18 13:05:05 +05:30
|
|
|
this._errorMessage = "";
|
2021-08-19 20:30:14 +05:30
|
|
|
try {
|
|
|
|
this._loginOptions = await this._sessionContainer.queryLogin(homeserver);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
this._loginOptions = null;
|
|
|
|
}
|
2021-08-18 13:05:05 +05:30
|
|
|
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 neither supports 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_showPasswordLogin() {
|
2021-08-18 13:05:05 +05:30
|
|
|
this._passwordLoginViewModel = this.track(new PasswordLoginViewModel(this.childOptions()));
|
2021-08-15 16:21:00 +05:30
|
|
|
this.emitChange("passwordLoginViewModel");
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:05:05 +05:30
|
|
|
_showSSOLogin() {
|
|
|
|
this._startSSOLoginViewModel = this.track(new StartSSOLoginViewModel(this.childOptions()));
|
2021-08-17 17:01:03 +05:30
|
|
|
this.emitChange("startSSOLoginViewModel");
|
2021-08-18 13:05:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
_showError(message) {
|
|
|
|
this._errorMessage = message;
|
|
|
|
this.emitChange("errorMessage");
|
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);
|
|
|
|
this.emitChange("disposeViewModels");
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHomeServer(newHomeserver) {
|
|
|
|
this._homeserver = newHomeserver;
|
|
|
|
this._disposeViewModels();
|
2021-08-19 20:25:54 +05:30
|
|
|
this._createViewModels(newHomeserver);
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-18 13:05:05 +05:30
|
|
|
childOptions(options = {}) {
|
2021-08-15 16:21:00 +05:30
|
|
|
return {
|
|
|
|
...super.childOptions(options),
|
|
|
|
ready: sessionContainer => {
|
|
|
|
// make sure we don't delete the session in dispose when navigating away
|
|
|
|
this._sessionContainer = null;
|
|
|
|
this._ready(sessionContainer);
|
|
|
|
},
|
|
|
|
sessionContainer: this._sessionContainer,
|
2021-08-18 13:05:05 +05:30
|
|
|
loginOptions: this._loginOptions,
|
2021-08-19 20:13:34 +05:30
|
|
|
homeserver: this._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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|