import { passkey } from '@better-auth/passkey'; import { declaredAddress } from './trusted-origin.ts'; /** * A passkey is bound to one origin: the browser offers a credential only to the * domain that created it, and the server verifies the ceremony against the same * origin, so an instance answering on several domains needs one canonical * address. That is `declaredAddress()`, and with neither `ORIGIN` nor * `INSTANCE_ORIGIN` better-auth names `localhost` as the relying party. */ export function passkeyOrigin(): string | undefined { return declaredAddress(); } /** * Why this request can or cannot use passkeys. The account page names the * blocking fact, because "not available" alone cannot be acted on. */ export type PasskeyAvailability = 'ready' | 'other-address' | 'insecure' | 'unconfigured'; /** * Whether this request arrived on the registered address, on a page the browser * treats as secure. With no configured origin better-auth names `localhost` as * the relying party, so the controls hide rather than start a ceremony that * cannot succeed. */ export function passkeyAvailability(requestOrigin: string): PasskeyAvailability { let url: URL; try { url = new URL(requestOrigin); } catch { return 'unconfigured'; } const loopback = url.hostname === 'localhost' || url.hostname === '127.0.0.1' || url.hostname === '[::1]'; const secure = url.protocol === 'https:' || loopback; const configured = passkeyOrigin(); if (configured) { if (requestOrigin !== configured) return 'other-address'; return secure ? 'ready' : 'insecure'; } // WebAuthn runs only in a secure context. A LAN address over plain HTTP is // not one, however clearly the operator declared it. if (!secure) return 'insecure'; // The plugin's fallback relying party is exactly `localhost`, not another // loopback address. return url.hostname === 'localhost' ? 'ready' : 'unconfigured'; } export function passkeysAvailable(requestOrigin: string): boolean { return passkeyAvailability(requestOrigin) === 'ready'; } export function passkeyPlugin() { const origin = passkeyOrigin(); if (!origin) return passkey({ rpName: 'Fablewright' }); return passkey({ rpID: new URL(origin).hostname, rpName: 'Fablewright', origin }); }