2020-05-22 15:29:53 +03:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Modal from "../Modal";
|
|
|
|
import * as sdk from "../index";
|
|
|
|
import { _t } from "../languageHandler";
|
|
|
|
import DeviceListener from "../DeviceListener";
|
2020-09-10 15:56:07 +03:00
|
|
|
import SetupEncryptionDialog from "../components/views/dialogs/security/SetupEncryptionDialog";
|
2020-09-03 16:50:49 +03:00
|
|
|
import { accessSecretStorage } from "../SecurityManager";
|
2020-05-22 15:29:53 +03:00
|
|
|
import ToastStore from "../stores/ToastStore";
|
|
|
|
import GenericToast from "../components/views/toasts/GenericToast";
|
2020-10-08 18:35:17 +03:00
|
|
|
import SecurityCustomisations from "../customisations/Security";
|
2020-05-22 15:29:53 +03:00
|
|
|
|
|
|
|
const TOAST_KEY = "setupencryption";
|
|
|
|
|
|
|
|
const getTitle = (kind: Kind) => {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind.SET_UP_ENCRYPTION:
|
2020-09-16 15:57:23 +03:00
|
|
|
return _t("Set up Secure Backup");
|
2020-05-22 15:29:53 +03:00
|
|
|
case Kind.UPGRADE_ENCRYPTION:
|
|
|
|
return _t("Encryption upgrade available");
|
|
|
|
case Kind.VERIFY_THIS_SESSION:
|
|
|
|
return _t("Verify this session");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-16 15:57:23 +03:00
|
|
|
const getIcon = (kind: Kind) => {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind.SET_UP_ENCRYPTION:
|
|
|
|
case Kind.UPGRADE_ENCRYPTION:
|
|
|
|
return "secure_backup";
|
|
|
|
case Kind.VERIFY_THIS_SESSION:
|
|
|
|
return "verification_warning";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-22 15:29:53 +03:00
|
|
|
const getSetupCaption = (kind: Kind) => {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind.SET_UP_ENCRYPTION:
|
2020-09-16 15:57:23 +03:00
|
|
|
return _t("Continue");
|
2020-05-22 15:29:53 +03:00
|
|
|
case Kind.UPGRADE_ENCRYPTION:
|
|
|
|
return _t("Upgrade");
|
|
|
|
case Kind.VERIFY_THIS_SESSION:
|
|
|
|
return _t("Verify");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getDescription = (kind: Kind) => {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind.SET_UP_ENCRYPTION:
|
|
|
|
case Kind.UPGRADE_ENCRYPTION:
|
2020-09-16 15:57:23 +03:00
|
|
|
return _t("Safeguard against losing access to encrypted messages & data");
|
2020-05-22 15:29:53 +03:00
|
|
|
case Kind.VERIFY_THIS_SESSION:
|
|
|
|
return _t("Other users may not trust it");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export enum Kind {
|
|
|
|
SET_UP_ENCRYPTION = "set_up_encryption",
|
|
|
|
UPGRADE_ENCRYPTION = "upgrade_encryption",
|
|
|
|
VERIFY_THIS_SESSION = "verify_this_session",
|
|
|
|
}
|
|
|
|
|
|
|
|
const onReject = () => {
|
|
|
|
DeviceListener.sharedInstance().dismissEncryptionSetup();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const showToast = (kind: Kind) => {
|
2020-10-08 18:35:17 +03:00
|
|
|
if (SecurityCustomisations.setupEncryptionNeeded?.(kind)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-22 15:29:53 +03:00
|
|
|
const onAccept = async () => {
|
|
|
|
if (kind === Kind.VERIFY_THIS_SESSION) {
|
|
|
|
Modal.createTrackedDialog("Verify session", "Verify session", SetupEncryptionDialog,
|
|
|
|
{}, null, /* priority = */ false, /* static = */ true);
|
|
|
|
} else {
|
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
|
|
|
const modal = Modal.createDialog(
|
|
|
|
Spinner, null, "mx_Dialog_spinner", /* priority */ false, /* static */ true,
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
await accessSecretStorage();
|
|
|
|
} finally {
|
|
|
|
modal.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ToastStore.sharedInstance().addOrReplaceToast({
|
|
|
|
key: TOAST_KEY,
|
|
|
|
title: getTitle(kind),
|
2020-09-16 15:57:23 +03:00
|
|
|
icon: getIcon(kind),
|
2020-05-22 15:29:53 +03:00
|
|
|
props: {
|
|
|
|
description: getDescription(kind),
|
|
|
|
acceptLabel: getSetupCaption(kind),
|
|
|
|
onAccept,
|
|
|
|
rejectLabel: _t("Later"),
|
|
|
|
onReject,
|
|
|
|
},
|
|
|
|
component: GenericToast,
|
|
|
|
priority: kind === Kind.VERIFY_THIS_SESSION ? 95 : 40,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const hideToast = () => {
|
|
|
|
ToastStore.sharedInstance().dismissToast(TOAST_KEY);
|
|
|
|
};
|