1
0
Fork 0

Tidy up api and TransitEncrypt/Decrypt pages.

This commit is contained in:
Kitteh 2021-05-09 15:04:20 +01:00
parent c1ce81ba0d
commit 562ced442e
4 changed files with 30 additions and 13 deletions

View file

@ -5,10 +5,14 @@ type DecryptionResult = {
plaintext: string;
}
type DecryptionPayload = {
ciphertext: string;
}
export async function transitDecrypt(
baseMount: string,
name: string,
data: string
payload: DecryptionPayload
): Promise<DecryptionResult> {
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), {
method: 'POST',
@ -16,7 +20,7 @@ export async function transitDecrypt(
'Content-Type': 'application/json',
...getHeaders(),
},
body: JSON.stringify({ ciphertext: data })
body: JSON.stringify(payload)
});
const response = await fetch(request);
if (!response.ok) {

View file

@ -5,10 +5,14 @@ type EncryptionResult = {
ciphertext: string;
}
type EncryptionPayload = {
plaintext: string;
}
export async function transitEncrypt(
baseMount: string,
name: string,
data: string
payload: EncryptionPayload
): Promise<EncryptionResult> {
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
method: 'POST',
@ -16,7 +20,7 @@ export async function transitEncrypt(
'Content-Type': 'application/json',
...getHeaders(),
},
body: JSON.stringify({ plaintext: data })
body: JSON.stringify(payload)
});
const response = await fetch(request);
if (!response.ok) {

View file

@ -80,7 +80,7 @@ export class TransitDecryptPage extends Page {
async transitDecryptFormHandler(): Promise<void> {
const formData = new FormData(this.transitDecryptForm);
const decodeBase64 = formData.get("decodeBase64Checkbox") as string;
let ciphertext = formData.get("ciphertext") as string;
@ -90,7 +90,12 @@ export class TransitDecryptPage extends Page {
ciphertext = atob((await fileToBase64(ciphertext_file) as string).replace("data:text/plain;base64,", ""));
}
transitDecrypt(pageState.currentBaseMount, pageState.currentSecret, ciphertext).then(res => {
try {
let res = await transitDecrypt(
pageState.currentBaseMount,
pageState.currentSecret,
{ ciphertext: ciphertext },
);
let plaintext = res.plaintext;
if (decodeBase64 == "on") {
plaintext = atob(plaintext);
@ -98,9 +103,9 @@ export class TransitDecryptPage extends Page {
const modal = CopyableModal(i18next.t("transit_decrypt_decryption_result_modal_title"), plaintext);
document.body.querySelector("#pageContent").appendChild(modal);
UIkit.modal(modal).show();
}).catch(e => {
} catch (e) {
setErrorText(`API Error: ${e.message}`);
});
}
}
get titleSuffix(): string {

View file

@ -81,7 +81,7 @@ export class TransitEncryptPage extends Page {
}.bind(this));
}
async transitEncryptFormHandler(): Promise<void> {
async transitEncryptFormHandler(): Promise<void> {
const formData = new FormData(this.transitEncryptForm);
const base64Checkbox = formData.get("base64Checkbox") as string;
@ -96,14 +96,18 @@ export class TransitEncryptPage extends Page {
plaintext = base64Checkbox == "on" ? plaintext : btoa(plaintext);
}
transitEncrypt(pageState.currentBaseMount, pageState.currentSecret, plaintext).then(res => {
try {
let res = await transitEncrypt(
pageState.currentBaseMount,
pageState.currentSecret,
{ plaintext: plaintext }
);
const modal = CopyableModal(i18next.t("transit_encrypt_encryption_result_modal_title"), res.ciphertext);
document.body.querySelector("#pageContent").appendChild(modal);
UIkit.modal(modal).show();
}).catch(e => {
} catch (e) {
setErrorText(`API Error: ${e.message}`);
});
}
}
get titleSuffix(): string {