2020-10-16 21:36:20 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 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.
|
|
|
|
*/
|
|
|
|
|
2020-10-20 15:40:41 +05:30
|
|
|
import {ViewModel} from "../../ViewModel.js";
|
2021-11-29 13:54:44 +05:30
|
|
|
import {KeyType} from "../../../matrix/ssss/index";
|
2021-11-17 15:54:44 +05:30
|
|
|
import {createEnum} from "../../../utils/enum";
|
2021-10-29 19:18:28 +05:30
|
|
|
|
2022-01-28 19:44:23 +05:30
|
|
|
export const Status = createEnum("Enabled", "SetupKey", "SetupPhrase", "Pending", "NewVersionAvailable");
|
2020-10-16 21:36:20 +05:30
|
|
|
|
2022-01-26 14:21:48 +05:30
|
|
|
export class KeyBackupViewModel extends ViewModel {
|
2020-10-19 21:59:13 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this._session = options.session;
|
|
|
|
this._error = null;
|
|
|
|
this._isBusy = false;
|
2021-10-29 19:18:28 +05:30
|
|
|
this._dehydratedDeviceId = undefined;
|
|
|
|
this._status = undefined;
|
2022-01-28 21:06:13 +05:30
|
|
|
this._backupOperation = this._session.keyBackup.flatMap(keyBackup => keyBackup.operationInProgress);
|
|
|
|
this._progress = this._backupOperation.flatMap(op => op.progress);
|
|
|
|
this.track(this._backupOperation.subscribe(() => this.emitChange("isBackingUp")));
|
|
|
|
this.track(this._progress.subscribe(() => this.emitChange("backupPercentage")));
|
2021-10-29 19:18:28 +05:30
|
|
|
this._reevaluateStatus();
|
2022-01-28 19:44:23 +05:30
|
|
|
this.track(this._session.keyBackup.subscribe(() => {
|
2021-10-29 19:18:28 +05:30
|
|
|
if (this._reevaluateStatus()) {
|
|
|
|
this.emitChange("status");
|
|
|
|
}
|
2020-10-23 16:27:47 +05:30
|
|
|
}));
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
2021-10-29 19:18:28 +05:30
|
|
|
|
|
|
|
_reevaluateStatus() {
|
|
|
|
if (this._isBusy) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let status;
|
2022-01-28 19:44:23 +05:30
|
|
|
const keyBackup = this._session.keyBackup.get();
|
|
|
|
if (keyBackup) {
|
|
|
|
status = keyBackup.needsNewKey.get() ? Status.NewVersionAvailable : Status.Enabled;
|
2021-10-29 19:18:28 +05:30
|
|
|
} else {
|
2022-01-28 19:44:23 +05:30
|
|
|
status = this.showPhraseSetup() ? Status.SetupPhrase : Status.SetupKey;
|
|
|
|
} /* TODO: bring back "waiting to get online"
|
|
|
|
else {
|
2021-10-29 19:18:28 +05:30
|
|
|
status = Status.Pending;
|
2022-01-28 19:44:23 +05:30
|
|
|
} */
|
2021-10-29 19:18:28 +05:30
|
|
|
const changed = status !== this._status;
|
|
|
|
this._status = status;
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
get decryptAction() {
|
|
|
|
return this.i18n`Set up`;
|
|
|
|
}
|
|
|
|
|
2021-10-29 20:10:35 +05:30
|
|
|
get purpose() {
|
2022-01-26 14:21:48 +05:30
|
|
|
return this.i18n`set up key backup`;
|
2021-10-29 20:10:35 +05:30
|
|
|
}
|
|
|
|
|
2021-10-29 19:18:28 +05:30
|
|
|
offerDehydratedDeviceSetup() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get dehydratedDeviceId() {
|
|
|
|
return this._dehydratedDeviceId;
|
|
|
|
}
|
2020-10-19 21:59:13 +05:30
|
|
|
|
|
|
|
get isBusy() {
|
|
|
|
return this._isBusy;
|
|
|
|
}
|
|
|
|
|
|
|
|
get backupVersion() {
|
2022-01-28 19:44:23 +05:30
|
|
|
return this._session.keyBackup.get()?.version;
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
2021-10-29 19:18:28 +05:30
|
|
|
return this._status;
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get error() {
|
|
|
|
return this._error?.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
showPhraseSetup() {
|
2021-10-29 19:18:28 +05:30
|
|
|
if (this._status === Status.SetupKey) {
|
|
|
|
this._status = Status.SetupPhrase;
|
|
|
|
this.emitChange("status");
|
|
|
|
}
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
showKeySetup() {
|
2021-10-29 19:18:28 +05:30
|
|
|
if (this._status === Status.SetupPhrase) {
|
|
|
|
this._status = Status.SetupKey;
|
|
|
|
this.emitChange("status");
|
|
|
|
}
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
|
2021-10-29 19:18:28 +05:30
|
|
|
async _enterCredentials(keyType, credential, setupDehydratedDevice) {
|
|
|
|
if (credential) {
|
2020-10-19 21:59:13 +05:30
|
|
|
try {
|
|
|
|
this._isBusy = true;
|
|
|
|
this.emitChange("isBusy");
|
2021-10-29 19:18:28 +05:30
|
|
|
const key = await this._session.enableSecretStorage(keyType, credential);
|
|
|
|
if (setupDehydratedDevice) {
|
|
|
|
this._dehydratedDeviceId = await this._session.setupDehydratedDevice(key);
|
|
|
|
}
|
2020-10-19 21:59:13 +05:30
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
this._error = err;
|
|
|
|
this.emitChange("error");
|
|
|
|
} finally {
|
|
|
|
this._isBusy = false;
|
2021-10-29 19:18:28 +05:30
|
|
|
this._reevaluateStatus();
|
2020-10-20 18:56:22 +05:30
|
|
|
this.emitChange("");
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 19:18:28 +05:30
|
|
|
enterSecurityPhrase(passphrase, setupDehydratedDevice) {
|
|
|
|
this._enterCredentials(KeyType.Passphrase, passphrase, setupDehydratedDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
enterSecurityKey(securityKey, setupDehydratedDevice) {
|
|
|
|
this._enterCredentials(KeyType.RecoveryKey, securityKey, setupDehydratedDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
async disable() {
|
|
|
|
try {
|
|
|
|
this._isBusy = true;
|
|
|
|
this.emitChange("isBusy");
|
|
|
|
await this._session.disableSecretStorage();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
this._error = err;
|
|
|
|
this.emitChange("error");
|
|
|
|
} finally {
|
|
|
|
this._isBusy = false;
|
|
|
|
this._reevaluateStatus();
|
|
|
|
this.emitChange("");
|
2020-10-19 21:59:13 +05:30
|
|
|
}
|
|
|
|
}
|
2022-01-28 19:44:23 +05:30
|
|
|
|
|
|
|
get isBackingUp() {
|
2022-01-28 21:06:13 +05:30
|
|
|
return !!this._backupOperation.get();
|
2022-01-28 19:44:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get backupPercentage() {
|
2022-01-28 21:06:13 +05:30
|
|
|
const progress = this._progress.get();
|
|
|
|
if (progress) {
|
|
|
|
return Math.round(progress.finished / progress.total) * 100;
|
2022-01-28 19:44:23 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get backupInProgressLabel() {
|
2022-01-28 21:06:13 +05:30
|
|
|
const progress = this._progress.get();
|
|
|
|
if (progress) {
|
|
|
|
return this.i18n`${progress.finished} of ${progress.total}`;
|
2022-01-28 19:44:23 +05:30
|
|
|
}
|
2022-01-28 21:06:13 +05:30
|
|
|
return this.i18n`…`;
|
2022-01-28 19:44:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
cancelBackup() {
|
|
|
|
this._session.keyBackup.get()?.operationInProgress.get()?.abort();
|
|
|
|
}
|
2020-10-16 21:36:20 +05:30
|
|
|
}
|
2022-01-28 19:44:23 +05:30
|
|
|
|