From 3fefdbc90d2afed6e0d68ac09faa54d82350de1c Mon Sep 17 00:00:00 2001 From: Kitteh Date: Sat, 17 Apr 2021 10:39:07 +0100 Subject: [PATCH] Implement Transit Decrypt Page. --- src/api.js | 19 +++++++++ src/main.js | 2 + src/pages/TransitDecrypt.js | 70 ++++++++++++++++++++++++++++++++++ src/pages/TransitEncrypt.js | 3 +- src/pages/TransitViewSecret.js | 9 ++++- src/pages/index.js | 3 +- src/scss/main.scss | 4 ++ 7 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 src/pages/TransitDecrypt.js diff --git a/src/api.js b/src/api.js index a8070ad..0260c25 100644 --- a/src/api.js +++ b/src/api.js @@ -296,6 +296,25 @@ export async function transitEncrypt(baseMount, name, data) { } } +export async function transitDecrypt(baseMount, name, data) { + const request = new Request(getAPIURL() + removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Vault-Token': getToken() + }, + body: JSON.stringify({ciphertext: data}) + }); + let response = await fetch(request); + if (!response.ok) { + let json = await response.json(); + throw new Error(json.errors[0]); + } else { + let json = await response.json(); + return json.data; + } +} + export async function getTOTPKeys(baseMount) { const request = new Request(getAPIURL() + `/v1/${baseMount}/keys?list=true`, { diff --git a/src/main.js b/src/main.js index 7ad850a..d73e52d 100644 --- a/src/main.js +++ b/src/main.js @@ -26,6 +26,7 @@ import { TransitViewPage, TransitViewSecretPage, TransitEncryptPage, + TransitDecryptPage, KeyValueViewPage, KeyValueSecretsPage, KeyValueVersionsPage, @@ -47,6 +48,7 @@ const pages = { TRANSIT_VIEW: new TransitViewPage(), TRANSIT_VIEW_SECRET: new TransitViewSecretPage(), TRANSIT_ENCRYPT: new TransitEncryptPage(), + TRANSIT_DECRYPT: new TransitDecryptPage(), KEY_VALUE_VIEW: new KeyValueViewPage(), KEY_VALUE_SECRETS: new KeyValueSecretsPage(), KEY_VALUE_VERSIONS: new KeyValueVersionsPage(), diff --git a/src/pages/TransitDecrypt.js b/src/pages/TransitDecrypt.js new file mode 100644 index 0000000..bea329f --- /dev/null +++ b/src/pages/TransitDecrypt.js @@ -0,0 +1,70 @@ +import { Page } from "../types/Page.js"; +import { transitDecrypt } from "../api.js"; +import { setPageContent, setTitleElement, setErrorText } from "../pageUtils.js"; +import { makeElement } from "../htmlUtils.js"; +import { Margin } from "../elements/Margin.js"; +import { CopyableModal } from "../elements/CopyableModal.js"; +import UIkit from 'uikit/dist/js/uikit.min.js'; + + +export class TransitDecryptPage extends Page { + constructor() { + super(); + } + goBack() { + changePage(pages.TRANSIT_VIEW_SECRET); + } + async render() { + 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: "Ciphertext", + name: "ciphertext", + } + })), + makeElement({ + tag: "p", + id: "errorText", + class: "uk-text-danger" + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: "Decrypt", + attributes: { + type: "submit", + } + }) + ] + }); + setPageContent(this.transitDecryptForm); + this.transitDecryptForm.addEventListener("submit", function (e) { + e.preventDefault(); + this.transitEncryptFormHandler(); + }.bind(this)); + } + + transitEncryptFormHandler() { + let formData = new FormData(this.transitDecryptForm); + + transitDecrypt(pageState.currentBaseMount, pageState.currentSecret, formData.get("ciphertext")).then(res => { + let modal = CopyableModal("Decryption Result", res.plaintext); + pageContent.appendChild(modal); + UIkit.modal(modal).show(); + }).catch(e => { + setErrorText(`API Error: ${e.message}`); + }); + } + + get name() { + return "Transit Decrypt"; + } +} diff --git a/src/pages/TransitEncrypt.js b/src/pages/TransitEncrypt.js index f5900a8..050f162 100644 --- a/src/pages/TransitEncrypt.js +++ b/src/pages/TransitEncrypt.js @@ -12,8 +12,7 @@ export class TransitEncryptPage extends Page { super(); } goBack() { - pageState.currentSecret = ""; - changePage(pages.TRANSIT_VIEW); + changePage(pages.TRANSIT_VIEW_SECRET); } async render() { setTitleElement(pageState); diff --git a/src/pages/TransitViewSecret.js b/src/pages/TransitViewSecret.js index 401f7ef..102cc0c 100644 --- a/src/pages/TransitViewSecret.js +++ b/src/pages/TransitViewSecret.js @@ -6,7 +6,7 @@ export class TransitViewSecretPage extends Page { constructor() { super(); } - goBack(){ + goBack() { changePage(pages.TRANSIT_VIEW); } @@ -59,7 +59,12 @@ export class TransitViewSecretPage extends Page { "lock", _ => { changePage(pages.TRANSIT_ENCRYPT); } ), - this.makeTile("Decrypt", "Decrypt some cyphertext.", "mail"), + this.makeTile( + "Decrypt", + "Decrypt some cyphertext.", + "mail", + _ => { changePage(pages.TRANSIT_DECRYPT); } + ), ] }), ] diff --git a/src/pages/index.js b/src/pages/index.js index bccd9de..a4e6d0a 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -13,4 +13,5 @@ export { KeyValueSecretsEditPage } from "./KeyValueSecretsEdit.js"; export { PwGenPage } from "./PwGen.js"; export { TransitViewPage } from "./TransitView.js"; export { TransitViewSecretPage } from "./TransitViewSecret.js"; -export { TransitEncryptPage } from "./TransitEncrypt.js"; \ No newline at end of file +export { TransitEncryptPage } from "./TransitEncrypt.js"; +export { TransitDecryptPage } from "./TransitDecrypt.js"; \ No newline at end of file diff --git a/src/scss/main.scss b/src/scss/main.scss index 85c922c..9957fae 100644 --- a/src/scss/main.scss +++ b/src/scss/main.scss @@ -10,5 +10,9 @@ padding-bottom: 10px !important; padding-left: 10px !important; } + +.wrap-pre { + white-space: pre-wrap; +} @import "./prism-nord.scss"; @import "./uikit.scss"; \ No newline at end of file