2021-08-15 16:21:00 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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";
|
2021-08-20 15:19:42 +05:30
|
|
|
import {LoginFailure} from "../../matrix/SessionContainer.js";
|
2021-08-15 16:21:00 +05:30
|
|
|
|
2021-08-17 17:01:03 +05:30
|
|
|
export class CompleteSSOLoginViewModel extends ViewModel {
|
2021-08-15 16:21:00 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
const {
|
|
|
|
loginToken,
|
|
|
|
sessionContainer,
|
2021-08-20 15:19:42 +05:30
|
|
|
attemptLogin,
|
2021-08-15 16:21:00 +05:30
|
|
|
} = options;
|
|
|
|
this._loginToken = loginToken;
|
|
|
|
this._sessionContainer = sessionContainer;
|
2021-08-20 15:19:42 +05:30
|
|
|
this._attemptLogin = attemptLogin;
|
2021-08-20 21:22:12 +05:30
|
|
|
this._errorMessage = "";
|
2021-08-15 16:36:03 +05:30
|
|
|
this.performSSOLoginCompletion();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
2021-08-20 21:22:12 +05:30
|
|
|
get errorMessage() { return this._errorMessage; }
|
|
|
|
|
|
|
|
_showError(message) {
|
|
|
|
this._errorMessage = message;
|
|
|
|
this.emitChange("errorMessage");
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:36:03 +05:30
|
|
|
async performSSOLoginCompletion() {
|
|
|
|
if (!this._loginToken) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-17 14:20:48 +05:30
|
|
|
const homeserver = await this.platform.settingsStorage.getString("sso_ongoing_login_homeserver");
|
2021-08-23 16:58:54 +05:30
|
|
|
let loginOptions;
|
|
|
|
try {
|
2021-08-23 23:23:52 +05:30
|
|
|
loginOptions = await this._sessionContainer.queryLogin(homeserver).result;
|
2021-08-23 16:58:54 +05:30
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
this._showError(err.message);
|
|
|
|
return;
|
|
|
|
}
|
2021-08-17 20:54:24 +05:30
|
|
|
if (!loginOptions.token) {
|
2021-08-23 17:00:41 +05:30
|
|
|
this.navigation.push("session");
|
2021-08-17 20:54:24 +05:30
|
|
|
return;
|
|
|
|
}
|
2021-08-20 15:19:42 +05:30
|
|
|
const status = await this._attemptLogin(loginOptions.token(this._loginToken));
|
|
|
|
let error = "";
|
|
|
|
switch (status) {
|
|
|
|
case LoginFailure.Credentials:
|
2021-08-23 15:06:16 +05:30
|
|
|
error = this.i18n`Your login token is invalid.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
case LoginFailure.Connection:
|
2021-08-20 19:08:47 +05:30
|
|
|
error = this.i18n`Can't connect to ${homeserver}.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
case LoginFailure.Unknown:
|
2021-08-23 15:06:16 +05:30
|
|
|
error = this.i18n`Something went wrong while checking your login token.`;
|
2021-08-20 15:19:42 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
this._showError(error);
|
2021-08-15 16:36:03 +05:30
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|