117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
import { useSignal } from "@preact/signals";
|
|
|
|
import Button, { button_class } from "@/components/Button.tsx";
|
|
import InputField from "@/components/InputField.tsx";
|
|
import TextAreaField from "@/components/TextAreaField.tsx";
|
|
import { save_master_key } from "@/src/key_store.ts";
|
|
import { type GetKeyResponse, import_key_text, unwrap_with_passphrase, verify_key_check } from "@/src/keys.ts";
|
|
|
|
const MAX_KEY_FILE_SIZE = 1024;
|
|
|
|
interface KeysUnlockProps {
|
|
account: GetKeyResponse;
|
|
message: string;
|
|
on_done: () => void;
|
|
}
|
|
|
|
export default function KeysUnlock(props: KeysUnlockProps) {
|
|
const passphrase = useSignal("");
|
|
const key_text = useSignal("");
|
|
const working = useSignal(false);
|
|
const error = useSignal(props.message);
|
|
|
|
async function unlock(get_key: () => Promise<CryptoKey>) {
|
|
working.value = true;
|
|
error.value = "";
|
|
try {
|
|
const key = await get_key();
|
|
if (!props.account.key_check || !(await verify_key_check(key, props.account.key_check))) {
|
|
error.value = "That key doesn't belong to this account";
|
|
return;
|
|
}
|
|
await save_master_key(props.account.user_id, key);
|
|
props.on_done();
|
|
} catch (err) {
|
|
error.value = err instanceof Error ? err.message : String(err);
|
|
} finally {
|
|
working.value = false;
|
|
}
|
|
}
|
|
|
|
function unlock_with_passphrase(event: SubmitEvent) {
|
|
event.preventDefault();
|
|
const backup = props.account.passphrase_backup;
|
|
if (backup && passphrase.value) {
|
|
unlock(() => unwrap_with_passphrase(backup, passphrase.value));
|
|
}
|
|
}
|
|
|
|
function unlock_with_key_text(event: SubmitEvent) {
|
|
event.preventDefault();
|
|
if (key_text.value) {
|
|
unlock(() => import_key_text(key_text.value));
|
|
}
|
|
}
|
|
|
|
async function unlock_with_key_file(event: Event) {
|
|
const input = event.currentTarget as HTMLInputElement;
|
|
const file = input.files?.[0];
|
|
input.value = "";
|
|
if (!file) {
|
|
return;
|
|
}
|
|
if (file.size > MAX_KEY_FILE_SIZE) {
|
|
error.value = "That doesn't look like a VCES key";
|
|
return;
|
|
}
|
|
const text = await file.text();
|
|
unlock(() => import_key_text(text));
|
|
}
|
|
|
|
return (
|
|
<div class="mx-auto max-w-md py-8">
|
|
<p class="leading-normal text-gray-800 dark:text-gray-400">
|
|
This account already has an encryption key. Unlock it on this device with one of your backups.
|
|
</p>
|
|
|
|
<fieldset class="flex flex-col gap-4" disabled={working.value}>
|
|
{props.account.passphrase_backup && (
|
|
<form class="flex flex-col gap-4" onSubmit={unlock_with_passphrase}>
|
|
<h2 class="mt-6 text-lg font-bold">Passphrase</h2>
|
|
<InputField
|
|
type="password"
|
|
autoComplete="current-password"
|
|
placeholder="Passphrase"
|
|
value={passphrase.value}
|
|
onInput={(event) => passphrase.value = event.currentTarget.value}
|
|
/>
|
|
<Button type="submit" variant="primary">Unlock</Button>
|
|
</form>
|
|
)}
|
|
|
|
<h2 class="mt-6 text-lg font-bold">Key file</h2>
|
|
<label class={button_class("link", "self-start")}>
|
|
<input type="file" accept=".txt,text/plain" class="sr-only" onChange={unlock_with_key_file} />
|
|
Choose key file
|
|
</label>
|
|
|
|
<form class="flex flex-col gap-4" onSubmit={unlock_with_key_text}>
|
|
<p class="text-sm text-gray-700 dark:text-gray-400">Or paste the key:</p>
|
|
<TextAreaField
|
|
class="h-24 font-mono text-sm break-all"
|
|
autoComplete="off"
|
|
spellcheck={false}
|
|
placeholder="vces-key-v1:..."
|
|
value={key_text.value}
|
|
onInput={(event) => key_text.value = event.currentTarget.value}
|
|
/>
|
|
<Button type="submit" variant="primary">Unlock</Button>
|
|
</form>
|
|
</fieldset>
|
|
|
|
{working.value && <p class="mt-4 text-gray-800 dark:text-gray-400">Unlocking...</p>}
|
|
{error.value && <p role="alert" class="mt-4 text-error-600 dark:text-error-400">{error.value}</p>}
|
|
</div>
|
|
);
|
|
}
|