Branchwell

BioLock / source · biolock.dev / demo / app.js

Raw 3.7 KB on ca4344897262c36de86cc3c37d2f69413751f189
1// Working demo. The server part runs in this page with the same server.js a real server uses,
2// so you can try it with no account. Anything enrolled here works only for this demo.
3import * as S from '../code/server.js';
4import * as C from '../code/client.js';
5
6const rpId = location.hostname, origin = location.origin;
7const DB = 'biolock-demo';
8const db = () => { try { return JSON.parse(localStorage.getItem(DB) || '{"creds":[]}'); } catch { return { creds: [] }; } };
9const put = (v) => { try { localStorage.setItem(DB, JSON.stringify(v)); } catch { /* private mode */ } };
10const $ = (s) => document.querySelector(s);
11const say = (msg, ok = true) => { const el = $('#fp-status'); el.textContent = msg; el.dataset.ok = ok ? '1' : '0'; };
12
13function render() {
14 const list = $('#fp-devices'); list.innerHTML = '';
15 const creds = db().creds;
16 if (!creds.length) { list.innerHTML = '<li class="fp-empty">No devices yet. Enroll this one first.</li>'; return; }
17 for (const c of creds) {
18 const li = document.createElement('li');
19 const here = C.enrolledOnThisDevice(rpId).includes(c.id);
20 li.innerHTML = '<span><b></b><small></small></span><button class="fp-small" type="button">Remove</button>';
21 li.querySelector('b').textContent = c.name + (here ? ' (this device)' : '');
22 li.querySelector('small').textContent = 'Enrolled ' + new Date(c.createdAt).toLocaleString() + (c.lastUsed ? ' · last used ' + new Date(c.lastUsed).toLocaleString() : '');
23 li.querySelector('button').onclick = async () => {
24 const d = db(); d.creds = d.creds.filter((x) => x.id !== c.id); put(d);
25 await C.removedOnServer(c.id, rpId); render(); say('Removed. That device can no longer sign in.');
26 };
27 list.appendChild(li);
28 }
29}
30
31$('#fp-add').onclick = async () => {
32 try {
33 const opts = S.registrationOptions({ rpId, rpName: 'BioLock demo', userName: 'demo', displayName: 'Demo member', excludeIds: C.enrolledOnThisDevice(rpId) });
34 const cred = await C.enrollThisDevice(opts);
35 const rec = await S.verifyRegistration({ credential: cred, expectedChallenge: opts.challenge, expectedOrigin: origin, rpId });
36 rec.name = ($('#fp-name').value || '').trim() || 'This device';
37 const d = db(); d.creds.push(rec); put(d); render();
38 say('Enrolled. This device now signs in with its own fingerprint or face.');
39 } catch (e) { say(nice(e), false); }
40};
41
42$('#fp-signin').onclick = async () => {
43 try {
44 const opts = S.signInOptions({ rpId, deviceCredentialIds: C.enrolledOnThisDevice(rpId) });
45 const cred = await C.signIn(opts);
46 const d = db(); const stored = d.creds.find((c) => c.id === cred.id);
47 if (!stored) { await C.removedOnServer(cred.id, rpId); throw new Error('This device was removed. Enroll it again.'); }
48 const r = await S.verifySignIn({ credential: cred, stored, expectedChallenge: opts.challenge, expectedOrigin: origin, rpId });
49 stored.counter = r.counter; stored.lastUsed = Date.now(); put(d); render();
50 say('Signed in with "' + stored.name + '". Checked: right site, fresh challenge, valid signature, fingerprint or face used.');
51 } catch (e) { say(nice(e), false); }
52};
53
54function nice(e) {
55 if (e && e.name === 'NotAllowedError') return 'Cancelled, or the reader did not match. Try again.';
56 if (e && e.name === 'InvalidStateError') return 'This device is already enrolled.';
57 return (e && e.message) ? e.message : String(e);
58}
59
60(async () => {
61 const builtIn = await C.hasBuiltInReader();
62 $('#fp-check').textContent = builtIn ? 'This device has a fingerprint or face reader. Ready.' : 'This device has no fingerprint or face reader, so it cannot sign in. Try a phone or a computer with a reader.';
63 if (!builtIn) { $('#fp-add').disabled = true; $('#fp-signin').disabled = true; }
64 render();
65})();