2021-05-08 00:30:44 +01:00
|
|
|
import { CopyableModal } from "../../elements/CopyableModal";
|
2021-05-09 11:06:45 +01:00
|
|
|
import { FileUploadInput } from "../../elements/FileUploadInput";
|
2021-05-08 00:38:02 +01:00
|
|
|
import { Margin } from "../../elements/Margin";
|
2021-05-07 23:33:58 +01:00
|
|
|
import { Page } from "../../types/Page";
|
2021-05-07 23:21:38 +01:00
|
|
|
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils";
|
2021-05-09 11:06:45 +01:00
|
|
|
import { fileToBase64, makeElement } from "../../htmlUtils";
|
2021-05-08 03:33:22 +01:00
|
|
|
import { pageState } from "../../globalPageState";
|
2021-05-09 11:18:18 +01:00
|
|
|
import { transitDecrypt } from "../../api/transit/transitDecrypt";
|
2021-04-17 10:39:07 +01:00
|
|
|
import UIkit from 'uikit/dist/js/uikit.min.js';
|
2021-04-29 11:03:24 +01:00
|
|
|
import i18next from "i18next";
|
2021-04-17 10:39:07 +01:00
|
|
|
|
|
|
|
export class TransitDecryptPage extends Page {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
}
|
2021-05-08 03:33:22 +01:00
|
|
|
|
|
|
|
goBack(): void {
|
2021-04-17 11:24:43 +01:00
|
|
|
changePage("TRANSIT_VIEW_SECRET");
|
2021-04-17 10:39:07 +01:00
|
|
|
}
|
2021-05-08 03:33:22 +01:00
|
|
|
|
|
|
|
transitDecryptForm: HTMLFormElement;
|
|
|
|
|
|
|
|
async render(): Promise<void> {
|
2021-04-17 10:39:07 +01:00
|
|
|
setTitleElement(pageState);
|
|
|
|
setPageContent(makeElement({
|
|
|
|
tag: "div"
|
|
|
|
}));
|
|
|
|
this.transitDecryptForm = makeElement({
|
|
|
|
tag: "form",
|
|
|
|
children: [
|
|
|
|
Margin(makeElement({
|
|
|
|
tag: "textarea",
|
|
|
|
class: ["uk-textarea", "uk-form-width-medium"],
|
|
|
|
attributes: {
|
2021-04-29 11:03:24 +01:00
|
|
|
placeholder: i18next.t("transit_decrypt_input_placeholder"),
|
2021-04-17 10:39:07 +01:00
|
|
|
name: "ciphertext",
|
|
|
|
}
|
|
|
|
})),
|
2021-05-09 11:06:45 +01:00
|
|
|
Margin(FileUploadInput("ciphertext_file")),
|
2021-04-17 12:53:55 +01:00
|
|
|
Margin([
|
|
|
|
makeElement({
|
|
|
|
tag: "div",
|
|
|
|
class: "uk-form-label",
|
2021-04-29 11:03:24 +01:00
|
|
|
text: i18next.t("transit_decrypt_decode_checkbox"),
|
2021-04-17 12:53:55 +01:00
|
|
|
}),
|
|
|
|
makeElement({
|
|
|
|
tag: "div",
|
|
|
|
class: ["uk-form-controls", "uk-form-controls-text"],
|
|
|
|
children: makeElement({
|
|
|
|
tag: "input",
|
2021-04-30 19:35:34 +01:00
|
|
|
class: "uk-checkbox",
|
2021-04-17 12:53:55 +01:00
|
|
|
attributes: {
|
|
|
|
type: "checkbox",
|
|
|
|
name: "decodeBase64Checkbox",
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
]),
|
2021-04-17 10:39:07 +01:00
|
|
|
makeElement({
|
|
|
|
tag: "p",
|
|
|
|
id: "errorText",
|
|
|
|
class: "uk-text-danger"
|
|
|
|
}),
|
|
|
|
makeElement({
|
|
|
|
tag: "button",
|
|
|
|
class: ["uk-button", "uk-button-primary"],
|
2021-04-29 11:03:24 +01:00
|
|
|
text: i18next.t("transit_decrypt_decrypt_btn"),
|
2021-04-17 10:39:07 +01:00
|
|
|
attributes: {
|
|
|
|
type: "submit",
|
|
|
|
}
|
|
|
|
})
|
|
|
|
]
|
2021-05-08 03:33:22 +01:00
|
|
|
}) as HTMLFormElement;
|
2021-04-17 10:39:07 +01:00
|
|
|
setPageContent(this.transitDecryptForm);
|
2021-05-09 11:06:45 +01:00
|
|
|
this.transitDecryptForm.addEventListener("submit", async function (e: Event) {
|
2021-04-17 10:39:07 +01:00
|
|
|
e.preventDefault();
|
2021-05-09 11:06:45 +01:00
|
|
|
await this.transitDecryptFormHandler();
|
2021-04-17 10:39:07 +01:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
2021-05-09 11:06:45 +01:00
|
|
|
async transitDecryptFormHandler(): Promise<void> {
|
2021-05-08 03:33:22 +01:00
|
|
|
const formData = new FormData(this.transitDecryptForm);
|
2021-05-09 11:06:45 +01:00
|
|
|
|
|
|
|
const decodeBase64 = formData.get("decodeBase64Checkbox") as string;
|
2021-04-17 10:39:07 +01:00
|
|
|
|
2021-05-09 11:06:45 +01:00
|
|
|
let ciphertext = formData.get("ciphertext") as string;
|
|
|
|
|
|
|
|
const ciphertext_file = formData.get("ciphertext_file") as File;
|
|
|
|
if (ciphertext_file.size > 0) {
|
|
|
|
ciphertext = atob((await fileToBase64(ciphertext_file) as string).replace("data:text/plain;base64,", ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
transitDecrypt(pageState.currentBaseMount, pageState.currentSecret, ciphertext).then(res => {
|
2021-04-17 12:53:55 +01:00
|
|
|
let plaintext = res.plaintext;
|
2021-05-09 11:06:45 +01:00
|
|
|
if (decodeBase64 == "on") {
|
2021-04-17 12:53:55 +01:00
|
|
|
plaintext = atob(plaintext);
|
|
|
|
}
|
2021-05-08 03:33:22 +01:00
|
|
|
const modal = CopyableModal(i18next.t("transit_decrypt_decryption_result_modal_title"), plaintext);
|
|
|
|
document.body.querySelector("#pageContent").appendChild(modal);
|
2021-04-17 10:39:07 +01:00
|
|
|
UIkit.modal(modal).show();
|
|
|
|
}).catch(e => {
|
|
|
|
setErrorText(`API Error: ${e.message}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-08 03:33:22 +01:00
|
|
|
get titleSuffix(): string {
|
2021-04-29 11:03:24 +01:00
|
|
|
return i18next.t("transit_decrypt_suffix");
|
2021-04-17 11:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-05-08 03:33:22 +01:00
|
|
|
get name(): string {
|
2021-04-29 11:03:24 +01:00
|
|
|
return i18next.t("transit_decrypt_title");
|
2021-04-17 10:39:07 +01:00
|
|
|
}
|
|
|
|
}
|