1
0
Fork 0
VaultUI/src/pages/Transit/TransitDecrypt.ts

114 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-05-08 00:30:44 +01:00
import { CopyableModal } from "../../elements/CopyableModal";
import { FileUploadInput } from "../../elements/FileUploadInput";
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";
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';
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: {
placeholder: i18next.t("transit_decrypt_input_placeholder"),
2021-04-17 10:39:07 +01:00
name: "ciphertext",
}
})),
Margin(FileUploadInput("ciphertext_file")),
Margin([
makeElement({
tag: "div",
class: "uk-form-label",
text: i18next.t("transit_decrypt_decode_checkbox"),
}),
makeElement({
tag: "div",
class: ["uk-form-controls", "uk-form-controls-text"],
children: makeElement({
tag: "input",
class: "uk-checkbox",
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"],
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);
this.transitDecryptForm.addEventListener("submit", async function (e: Event) {
2021-04-17 10:39:07 +01:00
e.preventDefault();
await this.transitDecryptFormHandler();
2021-04-17 10:39:07 +01:00
}.bind(this));
}
async transitDecryptFormHandler(): Promise<void> {
2021-05-08 03:33:22 +01:00
const formData = new FormData(this.transitDecryptForm);
const decodeBase64 = formData.get("decodeBase64Checkbox") as string;
2021-04-17 10:39:07 +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 => {
let plaintext = res.plaintext;
if (decodeBase64 == "on") {
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 {
return i18next.t("transit_decrypt_suffix");
}
2021-05-08 03:33:22 +01:00
get name(): string {
return i18next.t("transit_decrypt_title");
2021-04-17 10:39:07 +01:00
}
}