Something went wrong. Try again.
A music player that connects to your cloud/distributed storage. diffuse.sh
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";import { managedNonce } from "@noble/ciphers/utils.js";
import * as IDB from "idb-keyval";import { base64url } from "iso-base/rfc4648";import { utf8 } from "iso-base/utf8";
////////////////////////////////////////////// CONSTANTS////////////////////////////////////////////
const IDB_PREFIX = "diffuse/transformer/output/refiner/passkey-encryption";
/** * @param {string} namespace * @returns {{ credential: string, cipher: string }} */function idbKeys(namespace) { const prefix = namespace?.length ? `${IDB_PREFIX}/${namespace}` : IDB_PREFIX; return { credential: `${prefix}/passkey`, cipher: `${prefix}/passkey/cipher-key`, };}
////////////////////////////////////////////// PRF HELPERS////////////////////////////////////////////
/** * Normalize a PRF result to the ArrayBuffer the rest of this module expects. * The DOM types describe PRF results as `BufferSource`, but authenticators * return an `ArrayBuffer`. * * @param {BufferSource | undefined} value * @returns {ArrayBuffer | undefined} */function prfBuffer(value) { if (!value) return undefined; if (value instanceof ArrayBuffer) return value;
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength) .slice().buffer;}
/** * The `prf` extension input for both sign-up and sign-in: separate PRF inputs * for the account signing key and the output encryption key. * * @param {string} rpId * @returns {AuthenticationExtensionsClientInputs} */function prfExtensions(rpId) { return { prf: { eval: { first: new TextEncoder().encode(rpId + "signing"), second: new TextEncoder().encode(rpId + "encryption"), }, }, };}
////////////////////////////////////////////// RELYING PARTY////////////////////////////////////////////
/** * @returns {{ name: string, id: string }} */export function relyingParty() { const id = document.location.hostname; return { name: id, id };}
////////////////////////////////////////////// PASSKEY MANAGEMENT////////////////////////////////////////////
/** * Register a new passkey with the PRF extension. * * @param {string} namespace * @returns {Promise<{ supported: true, credentialId: Uint8Array, prfFirst?: ArrayBuffer, prfSecond: ArrayBuffer } | { supported: false, reason: string }>} */export async function createPasskey(namespace) { const rp = relyingParty(); const challenge = crypto.getRandomValues(new Uint8Array(32)); const userId = crypto.getRandomValues(new Uint8Array(16));
/** @type {PublicKeyCredential | null} */ let credential;
try { credential = /** @type {PublicKeyCredential} */ ( await navigator.credentials.create({ publicKey: { challenge, rp, user: { id: userId, name: rp.id, displayName: "Diffuse – " + rp.id, }, pubKeyCredParams: [ { type: "public-key", alg: -7 }, { type: "public-key", alg: -257 }, ], attestation: "none", authenticatorSelection: { userVerification: "required", requireResidentKey: true, residentKey: "required", }, extensions: prfExtensions(rp.id), }, }) ); } catch (err) { return { supported: false, reason: err instanceof Error ? err.message : String(err), }; }
if (!credential) { return { supported: false, reason: "Credential creation returned null" }; }
const extensions = credential.getClientExtensionResults();
if (extensions.prf?.enabled !== true) { return { supported: false, reason: "This authenticator does not support the WebAuthn PRF extension", }; }
const prfFirst = prfBuffer(extensions.prf?.results?.first); const prfSecond = prfBuffer(extensions.prf?.results?.second);
if (!prfSecond) { return { supported: false, reason: "Authenticator did not return PRF results at registration time", }; }
const credentialId = new Uint8Array(credential.rawId); await IDB.set(idbKeys(namespace).credential, { credentialId: Array.from(credentialId), });
return { supported: true, credentialId, prfFirst, prfSecond, };}
/** * Authenticate with an existing passkey via discoverable-credential lookup * (no `allowCredentials`), so it works on a new device that has no stored * credential ID yet. Saves the credential ID to IDB and returns PRF material. * * @param {string} namespace * @returns {Promise<{ supported: true, credentialId: Uint8Array, prfFirst?: ArrayBuffer, prfSecond: ArrayBuffer } | { supported: false, reason: string }>} */export async function adoptPasskeyPrfResult(namespace) { const rp = relyingParty(); const challenge = crypto.getRandomValues(new Uint8Array(32));
/** @type {PublicKeyCredential | null} */ let assertion;
try { assertion = /** @type {PublicKeyCredential} */ ( await navigator.credentials.get({ publicKey: { challenge, rpId: rp.id, userVerification: "required", extensions: prfExtensions(rp.id), }, }) ); } catch (err) { return { supported: false, reason: err instanceof Error ? err.message : String(err), }; }
if (!assertion) { return { supported: false, reason: "Credential get returned null" }; }
const extensions = assertion.getClientExtensionResults();
const prfFirst = prfBuffer(extensions.prf?.results?.first); const prfSecond = prfBuffer(extensions.prf?.results?.second);
if (!prfSecond) { return { supported: false, reason: "This authenticator did not return PRF results — PRF extension may not be supported", }; }
const credentialId = new Uint8Array(assertion.rawId); await IDB.set(idbKeys(namespace).credential, { credentialId: Array.from(credentialId), });
return { supported: true, credentialId, prfFirst, prfSecond, };}
/** * Remove the stored passkey credential ID and cached cipher key from IDB. * * @param {string} namespace * @returns {Promise<void>} */export async function removeStoredPasskey(namespace) { const keys = idbKeys(namespace); await Promise.all([IDB.del(keys.credential), IDB.del(keys.cipher)]);}
/** * Persist the derived cipher key to IDB so it survives page reloads. * * @param {string} namespace * @param {Uint8Array} key * @returns {Promise<void>} */export async function storeCipherKey(namespace, key) { await IDB.set(idbKeys(namespace).cipher, key);}
/** * Retrieve the previously persisted cipher key from IDB. * * @param {string} namespace * @returns {Promise<Uint8Array | undefined>} */export async function loadStoredCipherKey(namespace) { return await IDB.get(idbKeys(namespace).cipher);}
////////////////////////////////////////////// KEY DERIVATION////////////////////////////////////////////
/** HKDF salt shared by every key derived from the passkey. */const PASSKEY_SALT = "diffuse-atproto-passkey-salt";
/** Default purpose: the key that hides track URIs in a collection. */export const TRACK_URI_INFO = "diffuse-atproto-track-uri";
/** * Derive a 256-bit key from the PRF "second" output via HKDF. * Returns raw bytes suitable for use with XChaCha20-Poly1305 or AES-GCM. * * `info` separates purposes: the same passkey derives a different key per * label, so encrypting output entries never reuses the track-URI key. * * @param {ArrayBuffer} prfSecond * @param {string} [info] * @returns {Promise<Uint8Array>} */export async function deriveCipherKey(prfSecond, info = TRACK_URI_INFO) { const keyMaterial = await crypto.subtle.importKey( "raw", prfSecond, { name: "HKDF" }, false, ["deriveBits"], );
const bits = await crypto.subtle.deriveBits( { name: "HKDF", hash: "SHA-256",
salt: new TextEncoder().encode(PASSKEY_SALT), info: new TextEncoder().encode(info), }, keyMaterial, 256, );
return new Uint8Array(bits);}
////////////////////////////////////////////// ENCRYPT / DECRYPT////////////////////////////////////////////
/** * Detect whether a URI is encrypted by this module. * * @param {string} uri * @returns {boolean} */export function isEncryptedUri(uri) { return uri.startsWith("encrypted://");}
const xchacha = managedNonce(xchacha20poly1305);
/** * Encrypt a plaintext URI with XChaCha20-Poly1305. * Returns a string of the form: `encrypted://<base64url(nonce || ciphertext)>` * The nonce is prepended automatically by `managedNonce`. * * @param {Uint8Array} key * @param {string} plaintext * @returns {string} */export function encryptUri(key, plaintext) { const ciphertext = xchacha(key).encrypt(utf8.decode(plaintext)); return "encrypted://" + base64url.encode(ciphertext);}
/** * Decrypt an encrypted URI produced by `encryptUri`. * * @param {Uint8Array} key * @param {string} encryptedUri * @returns {string} */export function decryptUri(key, encryptedUri) { const ciphertext = base64url.decode(encryptedUri.slice(12)); return utf8.encode(xchacha(key).decrypt(ciphertext));}