During a session authenticated by both device identities, one person proposes a WebTorrent tracker and the other confirms it. Both contribute a nonce over the data channel; HKDF binds both device keys, the session fingerprints and the tracker URL into a non-extractable base key stored with the connection. Forget device, a replaced paired device and Turn off reconnect clear it. Reconnect joins a daily swarm derived from that key. Offers, answers and codeless wakes travel as AES-GCM envelopes bound to the tracker routing fields. The lower device key offers; the other device wakes it when it arrives second. Retries are spaced and capped, one attempt per slot runs under a Web Lock, and fresh device proofs still authenticate the session. Checked against tracker.openwebtorrent.com and tracker.webtorrent.dev with the new opt-in browser check (--tracker=<url>). Signaling v5, midnight rollover, clock skew, CSP and real-network checks remain open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
5.0 KiB
Vue
85 lines
5.0 KiB
Vue
<script setup lang="ts">
|
||
import { onActivated, onMounted, ref, shallowRef } from 'vue';
|
||
import { clearIdentityStorage, createIdentity, loadIdentityState, saveIdentityState, type IdentityState } from '../lib/identity.ts';
|
||
import type { ConnectionUpdate } from '../lib/connections.ts';
|
||
import { asError } from '../lib/validation.ts';
|
||
const props = defineProps<{ disabled: boolean; paired: string; remember: boolean; reconnect: string }>();
|
||
const emit = defineEmits<{ update: [change: ConnectionUpdate] }>();
|
||
const supported = (() => { try { return isSecureContext && !!crypto.subtle && !!window.indexedDB; } catch { return false; } })();
|
||
const state = shallowRef<IdentityState>({ identity: null });
|
||
const working = ref(false), loaded = ref(false);
|
||
const error = ref(''), confirmDelete = ref(false);
|
||
onMounted(async () => {
|
||
if (!supported) return;
|
||
try { state.value = await loadIdentityState(); loaded.value = true; }
|
||
catch (err) { error.value = asError(err).message; }
|
||
});
|
||
onActivated(async () => {
|
||
// Another slot may have replaced the shared identity while this workspace
|
||
// was cached. Never interrupt an active session to refresh it.
|
||
if (!loaded.value || props.disabled) return;
|
||
try {
|
||
const next = await loadIdentityState();
|
||
if (props.disabled) return;
|
||
if (next.revision !== state.value.revision) confirmDelete.value = false;
|
||
state.value = next;
|
||
} catch (err) { error.value = asError(err).message; }
|
||
});
|
||
async function run(action: () => Promise<void>) {
|
||
if (props.disabled || working.value) return;
|
||
working.value = true; error.value = '';
|
||
try { await action(); } catch (err) { error.value = asError(err).message; }
|
||
finally { working.value = false; }
|
||
}
|
||
function recover() { void run(async () => { if (!confirmDelete.value) return; await clearIdentityStorage(); state.value = { identity: null }; loaded.value = true; confirmDelete.value = false; }); }
|
||
// Creating an identity from a connection opts that connection in.
|
||
function create() { void run(async () => { state.value = await saveIdentityState({ identity: await createIdentity() }, state.value.revision); emit('update', { rememberDevice: true }); }); }
|
||
function reset() { void run(async () => { if (!confirmDelete.value) return; state.value = await saveIdentityState({ identity: null }, state.value.revision); confirmDelete.value = false; }); }
|
||
function toggle(event: Event) { if (!props.disabled && event.target instanceof HTMLInputElement) emit('update', { rememberDevice: event.target.checked }); }
|
||
function forget() { if (!props.disabled) emit('update', { peerKey: '' }); }
|
||
function stopReconnect() { if (!props.disabled) emit('update', { reconnect: null }); }
|
||
async function refreshIdentity() {
|
||
if (working.value) throw new Error('Wait for the device settings operation to finish before connecting.');
|
||
if (!supported) return null;
|
||
try {
|
||
const next = await loadIdentityState();
|
||
if (next.revision !== state.value.revision) confirmDelete.value = false;
|
||
state.value = next; loaded.value = true; error.value = '';
|
||
return next.identity;
|
||
} catch (err) {
|
||
error.value = asError(err).message;
|
||
throw err;
|
||
}
|
||
}
|
||
defineExpose({ refreshIdentity });
|
||
</script>
|
||
|
||
<template>
|
||
<details v-if="supported" class="config-details">
|
||
<summary>Remembered device</summary>
|
||
<p class="status">Optionally remember the device on this connection, so later sessions verify it automatically. This uses a device identity stored in this browser and shared by all connections. Clearing site data loses it. Trusted app code is required: it can use this identity.</p>
|
||
<fieldset class="device-settings" :disabled="disabled || working || !loaded">
|
||
<button v-if="!state.identity" @click="create">Create device identity</button>
|
||
<template v-else>
|
||
<template v-if="paired">
|
||
<p class="status">Paired device <code>{{ paired.slice(-12) }}</code>. Later sessions verify it automatically.</p>
|
||
<template v-if="reconnect">
|
||
<p class="status">Reconnects without codes through <strong>{{ reconnect }}</strong>.</p>
|
||
<button @click="stopReconnect">Turn off reconnect</button>
|
||
</template>
|
||
<button @click="forget">Forget device</button>
|
||
</template>
|
||
<label v-else><input id="remember-device" :checked="remember" type="checkbox" @change="toggle" /> Use my device identity on this connection</label>
|
||
<p class="status">When enabled, your public identity is included in this connection’s codes and can link your sessions.</p>
|
||
<label><input v-model="confirmDelete" type="checkbox" /> Delete this browser’s device identity</label>
|
||
<button :disabled="!confirmDelete" @click="reset">Delete device identity</button>
|
||
</template>
|
||
</fieldset>
|
||
<div v-if="!loaded && error">
|
||
<label><input v-model="confirmDelete" type="checkbox" :disabled="disabled || working" /> Delete unreadable device storage</label>
|
||
<button :disabled="disabled || working || !confirmDelete" @click="recover">Clear device storage</button>
|
||
</div>
|
||
<p v-if="error" role="alert" class="error">{{ error }}</p>
|
||
</details>
|
||
</template>
|