Files
vces/islands/KeysCreate.tsx
T
2026-10-29 17:50:21 -03:00

163 lines
5.3 KiB
TypeScript

import { useComputed, useSignal } from "@preact/signals";
import Button from "@/components/Button.tsx";
import InputField from "@/components/InputField.tsx";
import TextAreaField from "@/components/TextAreaField.tsx";
import { post_json } from "@/src/client.ts";
import { download_blob } from "@/src/files.ts";
import { save_master_key } from "@/src/key_store.ts";
import {
export_key_text,
generate_master_key,
make_key_check,
to_non_extractable,
wrap_with_passphrase,
} from "@/src/keys.ts";
const MIN_PASSPHRASE_LENGTH = 14;
interface KeysCreateProps {
user_id: string;
on_done: () => void;
// The account got a key some other way, it has to be unlocked instead
on_needs_unlock: (message: string) => void;
}
export default function KeysCreate(props: KeysCreateProps) {
const step = useSignal<"create" | "backup" | "saving">("create");
const new_key = useSignal<CryptoKey | null>(null);
const key_text = useSignal("");
const saved_file = useSignal(false);
const passphrase = useSignal("");
const passphrase_confirm = useSignal("");
const error = useSignal("");
const passphrase_error = useComputed(() => {
if (!passphrase.value && !passphrase_confirm.value) {
return "";
}
if (passphrase.value.length < MIN_PASSPHRASE_LENGTH) {
return `The passphrase must be at least ${MIN_PASSPHRASE_LENGTH} characters`;
}
if (passphrase.value !== passphrase_confirm.value) {
return "The passphrases don't match";
}
return "";
});
const can_continue = useComputed(() => !passphrase_error.value && (saved_file.value || passphrase.value !== ""));
async function create() {
const key = await generate_master_key();
new_key.value = key;
key_text.value = await export_key_text(key);
step.value = "backup";
}
function download_key_file() {
download_blob(new Blob([key_text.value + "\n"], { type: "text/plain" }), "vces-key.txt");
}
async function finish(event: SubmitEvent) {
event.preventDefault();
const key = new_key.value;
const code = localStorage.getItem("code");
if (!key || !code || !can_continue.value) {
return;
}
step.value = "saving";
error.value = "";
try {
const key_check = await make_key_check(key);
const passphrase_backup = passphrase.value ? await wrap_with_passphrase(key, passphrase.value) : undefined;
const result = await post_json("/api/create_key", { code, key_check, passphrase_backup });
if (!result.ok) {
if (result.status === 409) {
props.on_needs_unlock(result.error);
return;
}
error.value = result.error;
step.value = "backup";
return;
}
} catch (err) {
error.value = err instanceof Error ? err.message : String(err);
step.value = "backup";
return;
}
try {
await save_master_key(props.user_id, await to_non_extractable(key));
} catch {
props.on_needs_unlock(
"Your key was registered but couldn't be saved in this browser. Unlock it with your backup.",
);
return;
}
props.on_done();
}
if (step.value === "create") {
return (
<div class="mx-auto flex max-w-md flex-col items-center py-8">
<p class="mb-6 text-center leading-normal text-gray-800 dark:text-gray-400">
Your files are encrypted in this browser with a key the server never sees.
</p>
<Button variant="primary" onClick={create}>Create encryption key</Button>
</div>
);
}
return (
<form class="mx-auto max-w-md py-8" onSubmit={finish}>
<fieldset class="flex flex-col gap-4" disabled={step.value === "saving"}>
<p class="leading-normal text-gray-800 dark:text-gray-400">
Back up your key. If you lose it, your files can't be recovered, not even by the server.
</p>
<h2 class="mt-4 text-lg font-bold">Key file</h2>
<TextAreaField class="h-24 font-mono text-sm break-all" readOnly value={key_text.value} />
<Button variant="link" class="self-start" onClick={download_key_file}>Download key file</Button>
<label class="flex cursor-pointer items-center gap-2 leading-normal">
<input
type="checkbox"
class="size-5 accent-primary-500"
checked={saved_file.value}
onChange={(event) => saved_file.value = event.currentTarget.checked}
/>
I saved my key file somewhere safe
</label>
<h2 class="mt-4 text-lg font-bold">Passphrase (optional)</h2>
<p class="text-sm leading-normal text-gray-700 dark:text-gray-400">
Stores a copy of your key on the server, locked with this passphrase, so you can unlock other
devices with it. The key file is the stronger backup.
</p>
<InputField
type="password"
autoComplete="new-password"
placeholder="Passphrase"
value={passphrase.value}
onInput={(event) => passphrase.value = event.currentTarget.value}
/>
<InputField
type="password"
autoComplete="new-password"
placeholder="Confirm passphrase"
value={passphrase_confirm.value}
onInput={(event) => passphrase_confirm.value = event.currentTarget.value}
/>
{passphrase_error.value && (
<p role="alert" class="text-error-600 dark:text-error-400">{passphrase_error.value}</p>
)}
<Button type="submit" variant="primary" class="mt-4" disabled={!can_continue.value}>
{step.value === "saving" ? "Saving..." : "Continue"}
</Button>
</fieldset>
{error.value && <p role="alert" class="mt-4 text-error-600 dark:text-error-400">{error.value}</p>}
</form>
);
}