// BioLock: browser side. // Sign in with this device's own fingerprint or face reader. Nothing else: no security keys, // no QR codes, no phone-to-computer sign-in. Uses the browser's W3C Web Authentication API, // limited to the built-in reader. Public domain (Unlicense). const KEY = 'biolock:enrolled-here'; function toB64u(buf) { const b = new Uint8Array(buf); let s = ''; for (let i = 0; i < b.length; i++) s += String.fromCharCode(b[i]); return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); } function fromB64u(str) { const s = String(str).replace(/-/g, '+').replace(/_/g, '/'); const bin = atob(s + '='.repeat((4 - (s.length % 4)) % 4)); const out = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i); return out; } function load() { try { return JSON.parse(localStorage.getItem(KEY) || '{}'); } catch { return {}; } } function save(v) { try { localStorage.setItem(KEY, JSON.stringify(v)); } catch { /* private mode: still works, just less smooth */ } } // Which IDs this device enrolled, per site. Only IDs, never secrets. export function enrolledOnThisDevice(rpId = location.hostname) { return load()[rpId] || []; } export function rememberEnrolled(id, rpId = location.hostname) { const all = load(); const list = new Set(all[rpId] || []); list.add(id); all[rpId] = [...list]; save(all); } export function forgetEnrolled(id, rpId = location.hostname) { const all = load(); all[rpId] = (all[rpId] || []).filter((x) => x !== id); save(all); } // Does this device have its own fingerprint or face reader the browser can use? export async function hasBuiltInReader() { if (!window.PublicKeyCredential) return false; try { if (PublicKeyCredential.getClientCapabilities) { const c = await PublicKeyCredential.getClientCapabilities(); if (typeof c.userVerifyingPlatformAuthenticator === 'boolean') return c.userVerifyingPlatformAuthenticator; } return await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(); } catch { return false; } } function creationFromJSON(o) { if (PublicKeyCredential.parseCreationOptionsFromJSON) return PublicKeyCredential.parseCreationOptionsFromJSON(o); return { ...o, challenge: fromB64u(o.challenge), user: { ...o.user, id: fromB64u(o.user.id) }, excludeCredentials: (o.excludeCredentials || []).map((c) => ({ ...c, id: fromB64u(c.id) })) }; } function requestFromJSON(o) { if (PublicKeyCredential.parseRequestOptionsFromJSON) return PublicKeyCredential.parseRequestOptionsFromJSON(o); return { ...o, challenge: fromB64u(o.challenge), allowCredentials: (o.allowCredentials || []).map((c) => ({ ...c, id: fromB64u(c.id) })) }; } function credToJSON(c) { const r = c.response, out = { id: c.id, rawId: toB64u(c.rawId), type: c.type, authenticatorAttachment: c.authenticatorAttachment || null, response: { clientDataJSON: toB64u(r.clientDataJSON) } }; if (r.attestationObject) { out.response.attestationObject = toB64u(r.attestationObject); out.response.transports = r.getTransports ? r.getTransports() : []; } else { out.response.authenticatorData = toB64u(r.authenticatorData); out.response.signature = toB64u(r.signature); out.response.userHandle = r.userHandle ? toB64u(r.userHandle) : null; } return out; } // Enroll this device's reader. `options` comes from server.registrationOptions(). export async function enrollThisDevice(options) { if (!(await hasBuiltInReader())) throw new Error('This device has no fingerprint or face reader.'); const cred = await navigator.credentials.create({ publicKey: creationFromJSON(options) }); if (cred.authenticatorAttachment && cred.authenticatorAttachment !== 'platform') throw new Error('Only this device\'s own reader can be used.'); const json = credToJSON(cred); rememberEnrolled(json.id, options.rp.id); return json; } // Sign in with this device's reader. `options` comes from // server.signInOptions({ deviceCredentialIds: enrolledOnThisDevice() }). // With no saved IDs the browser offers any key this device's own reader holds for the site. export async function signIn(options, { signal } = {}) { const o = { ...options, hints: ['client-device'], allowCredentials: (options.allowCredentials || []).map((c) => ({ ...c, transports: ['internal'] })) }; const cred = await navigator.credentials.get({ publicKey: requestFromJSON(o), signal }); if (cred.authenticatorAttachment && cred.authenticatorAttachment !== 'platform') throw new Error('Only this device\'s own reader can be used.'); return credToJSON(cred); } // After a device is removed on the server, forget it here and tell the browser to drop it, // where supported, so it is never offered again. export async function removedOnServer(id, rpId = location.hostname) { forgetEnrolled(id, rpId); try { if (PublicKeyCredential.signalUnknownCredential) await PublicKeyCredential.signalUnknownCredential({ rpId, credentialId: id }); } catch { /* fine */ } }