2019-01-18 18:24:38 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2019 Vector Creations Ltd
|
|
|
|
|
|
|
|
|
|
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 React from "react";
|
2023-06-23 13:38:06 +01:00
|
|
|
import { Device } from "matrix-js-sdk/src/matrix";
|
2023-08-21 09:15:22 +01:00
|
|
|
import { GeneratedSas, EmojiMapping } from "matrix-js-sdk/src/crypto-api/verification";
|
|
|
|
|
import SasEmoji from "@matrix-org/spec/sas-emoji.json";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2023-08-21 09:15:22 +01:00
|
|
|
import { _t, getNormalizedLanguageKeys, getUserLanguage } from "../../../languageHandler";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { PendingActionSpinner } from "../right_panel/EncryptionInfo";
|
2020-01-24 16:16:46 +00:00
|
|
|
import AccessibleButton from "../elements/AccessibleButton";
|
2020-04-16 11:35:54 +02:00
|
|
|
import { fixupColorFonts } from "../../../utils/FontManager";
|
2019-02-08 14:57:36 +00:00
|
|
|
|
2021-07-03 12:43:18 +01:00
|
|
|
interface IProps {
|
|
|
|
|
pending?: boolean;
|
2021-07-05 09:19:19 +02:00
|
|
|
displayName?: string; // required if pending is true
|
2023-06-23 13:38:06 +01:00
|
|
|
|
|
|
|
|
/** Details of the other device involved in the verification, if known */
|
|
|
|
|
otherDeviceDetails?: Device;
|
|
|
|
|
|
2021-07-03 12:43:18 +01:00
|
|
|
onDone: () => void;
|
|
|
|
|
onCancel: () => void;
|
2023-05-26 10:29:32 +01:00
|
|
|
sas: GeneratedSas;
|
2021-07-03 12:43:18 +01:00
|
|
|
isSelf?: boolean;
|
|
|
|
|
inDialog?: boolean; // whether this component is being shown in a dialog and to use DialogButtons
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
|
pending: boolean;
|
|
|
|
|
cancelling?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 09:15:22 +01:00
|
|
|
const SasEmojiMap = new Map<
|
|
|
|
|
string, // lowercase
|
|
|
|
|
{
|
|
|
|
|
description: string;
|
|
|
|
|
translations: {
|
|
|
|
|
[normalizedLanguageKey: string]: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
>(
|
|
|
|
|
SasEmoji.map(({ description, translated_descriptions: translations }) => [
|
|
|
|
|
description.toLowerCase(),
|
|
|
|
|
{
|
|
|
|
|
description,
|
|
|
|
|
// Normalize the translation keys
|
|
|
|
|
translations: Object.keys(translations).reduce<Record<string, string>>((o, k) => {
|
|
|
|
|
for (const key of getNormalizedLanguageKeys(k)) {
|
|
|
|
|
o[key] = translations[k as keyof typeof translations]!;
|
|
|
|
|
}
|
|
|
|
|
return o;
|
|
|
|
|
}, {}),
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Translate given EmojiMapping into the target locale
|
|
|
|
|
* @param mapping - the given EmojiMapping to translate
|
|
|
|
|
* @param locale - the BCP 47 locale to translate to, will fall back to English as the base locale for Matrix SAS Emoji.
|
2023-07-04 13:00:09 +01:00
|
|
|
*/
|
2023-08-21 09:15:22 +01:00
|
|
|
export function tEmoji(mapping: EmojiMapping, locale: string): string {
|
|
|
|
|
const name = mapping[1];
|
|
|
|
|
const emoji = SasEmojiMap.get(name.toLowerCase());
|
|
|
|
|
if (!emoji) {
|
|
|
|
|
console.warn("Emoji not found for translation", name);
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const key of getNormalizedLanguageKeys(locale)) {
|
|
|
|
|
if (!!emoji.translations[key]) {
|
|
|
|
|
return emoji.translations[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return emoji.description;
|
2020-08-18 11:41:42 -06:00
|
|
|
}
|
|
|
|
|
|
2021-07-03 12:43:18 +01:00
|
|
|
export default class VerificationShowSas extends React.Component<IProps, IState> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2020-01-24 16:16:46 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
pending: false,
|
|
|
|
|
};
|
2019-01-18 18:24:38 +00:00
|
|
|
|
2020-04-16 11:35:54 +02:00
|
|
|
// As this component is also used before login (during complete security),
|
|
|
|
|
// also make sure we have a working emoji font to display the SAS emojis here.
|
|
|
|
|
// This is also done from LoggedInView.
|
|
|
|
|
fixupColorFonts();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-03 12:43:18 +01:00
|
|
|
private onMatchClick = (): void => {
|
2020-01-24 16:16:46 +00:00
|
|
|
this.setState({ pending: true });
|
|
|
|
|
this.props.onDone();
|
|
|
|
|
};
|
2019-01-18 18:24:38 +00:00
|
|
|
|
2021-07-03 12:43:18 +01:00
|
|
|
private onDontMatchClick = (): void => {
|
2020-02-25 13:18:27 +01:00
|
|
|
this.setState({ cancelling: true });
|
|
|
|
|
this.props.onCancel();
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2023-08-21 09:15:22 +01:00
|
|
|
const locale = getUserLanguage();
|
|
|
|
|
|
2019-02-08 14:57:36 +00:00
|
|
|
let sasDisplay;
|
|
|
|
|
let sasCaption;
|
|
|
|
|
if (this.props.sas.emoji) {
|
|
|
|
|
const emojiBlocks = this.props.sas.emoji.map((emoji, i) => (
|
|
|
|
|
<div className="mx_VerificationShowSas_emojiSas_block" key={i}>
|
|
|
|
|
<div className="mx_VerificationShowSas_emojiSas_emoji">{emoji[0]}</div>
|
2023-08-21 09:15:22 +01:00
|
|
|
<div className="mx_VerificationShowSas_emojiSas_label">{tEmoji(emoji, locale)}</div>
|
2019-02-08 14:57:36 +00:00
|
|
|
</div>
|
2022-12-12 12:24:14 +01:00
|
|
|
));
|
2019-02-08 14:57:36 +00:00
|
|
|
sasDisplay = (
|
|
|
|
|
<div className="mx_VerificationShowSas_emojiSas">
|
2021-07-19 22:43:11 +01:00
|
|
|
{emojiBlocks.slice(0, 4)}
|
|
|
|
|
<div className="mx_VerificationShowSas_emojiSas_break" />
|
|
|
|
|
{emojiBlocks.slice(4)}
|
2019-02-08 14:57:36 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2020-01-30 16:11:05 +00:00
|
|
|
sasCaption = this.props.isSelf
|
2023-09-21 18:16:04 +01:00
|
|
|
? _t("encryption|verification|sas_emoji_caption_self")
|
|
|
|
|
: _t("encryption|verification|sas_emoji_caption_user");
|
2019-02-08 14:57:36 +00:00
|
|
|
} else if (this.props.sas.decimal) {
|
|
|
|
|
const numberBlocks = this.props.sas.decimal.map((num, i) => <span key={i}>{num}</span>);
|
|
|
|
|
sasDisplay = <div className="mx_VerificationShowSas_decimalSas">{numberBlocks}</div>;
|
2020-01-30 16:11:05 +00:00
|
|
|
sasCaption = this.props.isSelf
|
2023-09-21 18:16:04 +01:00
|
|
|
? _t("encryption|verification|sas_caption_self")
|
|
|
|
|
: _t("encryption|verification|sas_caption_user");
|
2019-02-08 14:57:36 +00:00
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
2023-09-21 18:16:04 +01:00
|
|
|
{_t("encryption|verification|unsupported_method")}
|
2021-11-15 10:09:19 +01:00
|
|
|
<AccessibleButton kind="primary" onClick={this.props.onCancel}>
|
2023-08-23 11:57:22 +01:00
|
|
|
{_t("action|cancel")}
|
2020-01-24 16:16:46 +00:00
|
|
|
</AccessibleButton>
|
2019-02-08 14:57:36 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 16:16:46 +00:00
|
|
|
let confirm;
|
2021-09-08 13:55:31 -04:00
|
|
|
if (this.state.pending && this.props.isSelf) {
|
|
|
|
|
let text;
|
|
|
|
|
// device shouldn't be null in this situation but it can be, eg. if the device is
|
|
|
|
|
// logged out during verification
|
2023-06-23 13:38:06 +01:00
|
|
|
const otherDevice = this.props.otherDeviceDetails;
|
|
|
|
|
if (otherDevice) {
|
2023-09-21 18:16:04 +01:00
|
|
|
text = _t("encryption|verification|waiting_other_device_details", {
|
2023-06-23 13:38:06 +01:00
|
|
|
deviceName: otherDevice.displayName,
|
|
|
|
|
deviceId: otherDevice.deviceId,
|
2021-09-08 13:55:31 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
2023-09-21 18:16:04 +01:00
|
|
|
text = _t("encryption|verification|waiting_other_device");
|
2021-09-08 13:55:31 -04:00
|
|
|
}
|
|
|
|
|
confirm = <p>{text}</p>;
|
|
|
|
|
} else if (this.state.pending || this.state.cancelling) {
|
2020-02-25 13:18:27 +01:00
|
|
|
let text;
|
|
|
|
|
if (this.state.pending) {
|
2021-09-08 13:55:31 -04:00
|
|
|
const { displayName } = this.props;
|
2023-09-21 18:16:04 +01:00
|
|
|
text = _t("encryption|verification|waiting_other_user", { displayName });
|
2020-02-25 13:18:27 +01:00
|
|
|
} else {
|
2023-09-21 18:16:04 +01:00
|
|
|
text = _t("encryption|verification|cancelling");
|
2020-02-25 13:18:27 +01:00
|
|
|
}
|
2020-01-24 16:16:46 +00:00
|
|
|
confirm = <PendingActionSpinner text={text} />;
|
2020-04-01 12:21:18 +01:00
|
|
|
} else {
|
2021-10-14 00:46:15 -04:00
|
|
|
confirm = (
|
|
|
|
|
<div className="mx_VerificationShowSas_buttonRow">
|
2020-04-01 12:21:18 +01:00
|
|
|
<AccessibleButton onClick={this.onDontMatchClick} kind="danger">
|
2023-09-07 10:37:09 +01:00
|
|
|
{_t("encryption|verification|sas_no_match")}
|
2020-04-01 12:21:18 +01:00
|
|
|
</AccessibleButton>
|
2020-04-10 15:38:17 +01:00
|
|
|
<AccessibleButton onClick={this.onMatchClick} kind="primary">
|
2023-09-07 10:37:09 +01:00
|
|
|
{_t("encryption|verification|sas_match")}
|
2020-04-10 15:38:17 +01:00
|
|
|
</AccessibleButton>
|
2021-10-14 00:46:15 -04:00
|
|
|
</div>
|
|
|
|
|
);
|
2020-01-24 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-08 14:57:36 +00:00
|
|
|
return (
|
|
|
|
|
<div className="mx_VerificationShowSas">
|
2021-07-19 22:43:11 +01:00
|
|
|
<p>{sasCaption}</p>
|
|
|
|
|
{sasDisplay}
|
2023-09-07 10:37:09 +01:00
|
|
|
<p>{this.props.isSelf ? "" : _t("encryption|verification|in_person")}</p>
|
2021-07-19 22:43:11 +01:00
|
|
|
{confirm}
|
2019-01-18 18:24:38 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-18 18:43:40 +00:00
|
|
|
}
|