Encryption keys
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
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>
|
||||
<p>This account already has an encryption key. Unlock it on this device with one of your backups.</p>
|
||||
|
||||
<fieldset disabled={working.value}>
|
||||
{props.account.passphrase_backup && (
|
||||
<form onSubmit={unlock_with_passphrase}>
|
||||
<h2>Passphrase</h2>
|
||||
<input
|
||||
type="password"
|
||||
autoComplete="current-password"
|
||||
placeholder="Passphrase"
|
||||
value={passphrase.value}
|
||||
onInput={(event) => passphrase.value = event.currentTarget.value}
|
||||
/>
|
||||
<button type="submit">Unlock</button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
<h2>Key file</h2>
|
||||
<input type="file" accept=".txt,text/plain" onChange={unlock_with_key_file} />
|
||||
|
||||
<form onSubmit={unlock_with_key_text}>
|
||||
<p>Or paste the key:</p>
|
||||
<textarea
|
||||
autoComplete="off"
|
||||
spellcheck={false}
|
||||
placeholder="vces-key-v1:..."
|
||||
value={key_text.value}
|
||||
onInput={(event) => key_text.value = event.currentTarget.value}
|
||||
/>
|
||||
<button type="submit">Unlock</button>
|
||||
</form>
|
||||
</fieldset>
|
||||
|
||||
{working.value && <p>Unlocking...</p>}
|
||||
{error.value && <p role="alert">{error.value}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user