Tidy up api and TransitEncrypt/Decrypt pages.
This commit is contained in:
parent
c1ce81ba0d
commit
562ced442e
|
@ -5,10 +5,14 @@ type DecryptionResult = {
|
||||||
plaintext: string;
|
plaintext: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DecryptionPayload = {
|
||||||
|
ciphertext: string;
|
||||||
|
}
|
||||||
|
|
||||||
export async function transitDecrypt(
|
export async function transitDecrypt(
|
||||||
baseMount: string,
|
baseMount: string,
|
||||||
name: string,
|
name: string,
|
||||||
data: string
|
payload: DecryptionPayload
|
||||||
): Promise<DecryptionResult> {
|
): Promise<DecryptionResult> {
|
||||||
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), {
|
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -16,7 +20,7 @@ export async function transitDecrypt(
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...getHeaders(),
|
...getHeaders(),
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ ciphertext: data })
|
body: JSON.stringify(payload)
|
||||||
});
|
});
|
||||||
const response = await fetch(request);
|
const response = await fetch(request);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
|
@ -5,10 +5,14 @@ type EncryptionResult = {
|
||||||
ciphertext: string;
|
ciphertext: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EncryptionPayload = {
|
||||||
|
plaintext: string;
|
||||||
|
}
|
||||||
|
|
||||||
export async function transitEncrypt(
|
export async function transitEncrypt(
|
||||||
baseMount: string,
|
baseMount: string,
|
||||||
name: string,
|
name: string,
|
||||||
data: string
|
payload: EncryptionPayload
|
||||||
): Promise<EncryptionResult> {
|
): Promise<EncryptionResult> {
|
||||||
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
|
const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -16,7 +20,7 @@ export async function transitEncrypt(
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...getHeaders(),
|
...getHeaders(),
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ plaintext: data })
|
body: JSON.stringify(payload)
|
||||||
});
|
});
|
||||||
const response = await fetch(request);
|
const response = await fetch(request);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|
|
@ -80,7 +80,7 @@ export class TransitDecryptPage extends Page {
|
||||||
|
|
||||||
async transitDecryptFormHandler(): Promise<void> {
|
async transitDecryptFormHandler(): Promise<void> {
|
||||||
const formData = new FormData(this.transitDecryptForm);
|
const formData = new FormData(this.transitDecryptForm);
|
||||||
|
|
||||||
const decodeBase64 = formData.get("decodeBase64Checkbox") as string;
|
const decodeBase64 = formData.get("decodeBase64Checkbox") as string;
|
||||||
|
|
||||||
let ciphertext = formData.get("ciphertext") 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,", ""));
|
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;
|
let plaintext = res.plaintext;
|
||||||
if (decodeBase64 == "on") {
|
if (decodeBase64 == "on") {
|
||||||
plaintext = atob(plaintext);
|
plaintext = atob(plaintext);
|
||||||
|
@ -98,9 +103,9 @@ export class TransitDecryptPage extends Page {
|
||||||
const modal = CopyableModal(i18next.t("transit_decrypt_decryption_result_modal_title"), plaintext);
|
const modal = CopyableModal(i18next.t("transit_decrypt_decryption_result_modal_title"), plaintext);
|
||||||
document.body.querySelector("#pageContent").appendChild(modal);
|
document.body.querySelector("#pageContent").appendChild(modal);
|
||||||
UIkit.modal(modal).show();
|
UIkit.modal(modal).show();
|
||||||
}).catch(e => {
|
} catch (e) {
|
||||||
setErrorText(`API Error: ${e.message}`);
|
setErrorText(`API Error: ${e.message}`);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get titleSuffix(): string {
|
get titleSuffix(): string {
|
||||||
|
|
|
@ -81,7 +81,7 @@ export class TransitEncryptPage extends Page {
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
async transitEncryptFormHandler(): Promise<void> {
|
async transitEncryptFormHandler(): Promise<void> {
|
||||||
const formData = new FormData(this.transitEncryptForm);
|
const formData = new FormData(this.transitEncryptForm);
|
||||||
|
|
||||||
const base64Checkbox = formData.get("base64Checkbox") as string;
|
const base64Checkbox = formData.get("base64Checkbox") as string;
|
||||||
|
@ -96,14 +96,18 @@ export class TransitEncryptPage extends Page {
|
||||||
plaintext = base64Checkbox == "on" ? plaintext : btoa(plaintext);
|
plaintext = base64Checkbox == "on" ? plaintext : btoa(plaintext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
transitEncrypt(pageState.currentBaseMount, pageState.currentSecret, plaintext).then(res => {
|
let res = await transitEncrypt(
|
||||||
|
pageState.currentBaseMount,
|
||||||
|
pageState.currentSecret,
|
||||||
|
{ plaintext: plaintext }
|
||||||
|
);
|
||||||
const modal = CopyableModal(i18next.t("transit_encrypt_encryption_result_modal_title"), res.ciphertext);
|
const modal = CopyableModal(i18next.t("transit_encrypt_encryption_result_modal_title"), res.ciphertext);
|
||||||
document.body.querySelector("#pageContent").appendChild(modal);
|
document.body.querySelector("#pageContent").appendChild(modal);
|
||||||
UIkit.modal(modal).show();
|
UIkit.modal(modal).show();
|
||||||
}).catch(e => {
|
} catch (e) {
|
||||||
setErrorText(`API Error: ${e.message}`);
|
setErrorText(`API Error: ${e.message}`);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get titleSuffix(): string {
|
get titleSuffix(): string {
|
||||||
|
|
Loading…
Reference in a new issue