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,
|
|
|
|
showError,
|
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;
|
|
|
|
this._showError = showError;
|
2021-08-15 16:36:03 +05:30
|
|
|
this.performSSOLoginCompletion();
|
2021-08-15 16:21:00 +05:30
|
|
|
}
|
|
|
|
|
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-15 16:36:03 +05:30
|
|
|
const loginOptions = await this._sessionContainer.queryLogin(homeserver);
|
2021-08-17 20:54:24 +05:30
|
|
|
if (!loginOptions.token) {
|
|
|
|
const path = this.navigation.pathFrom([this.navigation.segment("session")]);
|
|
|
|
this.navigation.applyPath(path);
|
|
|
|
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-20 19:08:47 +05:30
|
|
|
error = this.i18n`Your logintoken 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-20 19:08:47 +05:30
|
|
|
error = this.i18n`Something went wrong while checking your logintoken.`;
|
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
|
|
|
}
|