2021-10-27 15:08:53 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-02-14 17:53:59 +01:00
|
|
|
import {ViewModel} from "./ViewModel";
|
2021-11-29 09:24:44 +01:00
|
|
|
import {KeyType} from "../matrix/ssss/index";
|
2022-01-26 09:51:48 +01:00
|
|
|
import {Status} from "./session/settings/KeyBackupViewModel.js";
|
2021-10-27 15:08:53 +02:00
|
|
|
|
|
|
|
export class AccountSetupViewModel extends ViewModel {
|
2022-02-17 09:24:18 +01:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this._accountSetup = options.accountSetup;
|
2021-10-27 15:08:53 +02:00
|
|
|
this._dehydratedDevice = undefined;
|
2021-10-29 16:40:35 +02:00
|
|
|
this._decryptDehydratedDeviceViewModel = undefined;
|
|
|
|
if (this._accountSetup.encryptedDehydratedDevice) {
|
|
|
|
this._decryptDehydratedDeviceViewModel = new DecryptDehydratedDeviceViewModel(this, dehydratedDevice => {
|
|
|
|
this._dehydratedDevice = dehydratedDevice;
|
|
|
|
this._decryptDehydratedDeviceViewModel = undefined;
|
|
|
|
this.emitChange("deviceDecrypted");
|
|
|
|
});
|
|
|
|
}
|
2021-10-27 15:08:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 16:40:35 +02:00
|
|
|
get decryptDehydratedDeviceViewModel() {
|
|
|
|
return this._decryptDehydratedDeviceViewModel;
|
2021-10-27 15:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get deviceDecrypted() {
|
|
|
|
return !!this._dehydratedDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
get dehydratedDeviceId() {
|
|
|
|
return this._accountSetup.encryptedDehydratedDevice.deviceId;
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:40:35 +02:00
|
|
|
finish() {
|
|
|
|
this._accountSetup.finish(this._dehydratedDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:51:48 +01:00
|
|
|
// this vm adopts the same shape as KeyBackupViewModel so the same view can be reused.
|
2021-10-29 16:40:35 +02:00
|
|
|
class DecryptDehydratedDeviceViewModel extends ViewModel {
|
|
|
|
constructor(accountSetupViewModel, decryptedCallback) {
|
2022-02-17 09:24:18 +01:00
|
|
|
super(accountSetupViewModel.options);
|
2021-10-29 16:40:35 +02:00
|
|
|
this._accountSetupViewModel = accountSetupViewModel;
|
|
|
|
this._isBusy = false;
|
|
|
|
this._status = Status.SetupKey;
|
|
|
|
this._error = undefined;
|
|
|
|
this._decryptedCallback = decryptedCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
get decryptAction() {
|
|
|
|
return this.i18n`Restore`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get purpose() {
|
|
|
|
return this.i18n`claim your dehydrated device`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get offerDehydratedDeviceSetup() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get dehydratedDeviceId() {
|
|
|
|
return this._accountSetupViewModel._dehydratedDevice?.deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isBusy() {
|
|
|
|
return this._isBusy;
|
|
|
|
}
|
|
|
|
|
|
|
|
get backupVersion() { return 0; }
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get error() {
|
|
|
|
return this._error?.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
showPhraseSetup() {
|
|
|
|
if (this._status === Status.SetupKey) {
|
|
|
|
this._status = Status.SetupPhrase;
|
|
|
|
this.emitChange("status");
|
2021-10-27 15:08:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 16:40:35 +02:00
|
|
|
showKeySetup() {
|
|
|
|
if (this._status === Status.SetupPhrase) {
|
|
|
|
this._status = Status.SetupKey;
|
|
|
|
this.emitChange("status");
|
|
|
|
}
|
2021-10-27 15:08:53 +02:00
|
|
|
}
|
2021-10-29 16:40:35 +02:00
|
|
|
|
|
|
|
async _enterCredentials(keyType, credential) {
|
|
|
|
if (credential) {
|
|
|
|
try {
|
|
|
|
this._isBusy = true;
|
|
|
|
this.emitChange("isBusy");
|
|
|
|
const {encryptedDehydratedDevice} = this._accountSetupViewModel._accountSetup;
|
|
|
|
const dehydratedDevice = await encryptedDehydratedDevice.decrypt(keyType, credential);
|
|
|
|
this._decryptedCallback(dehydratedDevice);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
this._error = err;
|
|
|
|
this.emitChange("error");
|
|
|
|
} finally {
|
|
|
|
this._isBusy = false;
|
|
|
|
this.emitChange("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enterSecurityPhrase(passphrase) {
|
|
|
|
this._enterCredentials(KeyType.Passphrase, passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
enterSecurityKey(securityKey) {
|
|
|
|
this._enterCredentials(KeyType.RecoveryKey, securityKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {}
|
2021-10-27 15:08:53 +02:00
|
|
|
}
|