diff --git a/frontend/src/lib/api.js b/frontend/src/lib/api.js index 6a7e620..a74bda0 100644 --- a/frontend/src/lib/api.js +++ b/frontend/src/lib/api.js @@ -3,7 +3,11 @@ export const IS_APPLE = /iPhone|iPad|iPod|Macintosh/.test(navigator.userAgent) export const IS_ANDROID = /Android/.test(navigator.userAgent) export const BIO = IS_APPLE ? 'Face ID / Touch ID' : IS_ANDROID ? 'fingerprint or face unlock' : 'your fingerprint, face or PIN' export const VAULT = IS_APPLE ? 'iCloud Keychain' : IS_ANDROID ? 'Google Password Manager' : 'your password manager' -export const webauthnOK = () => !!(window.PublicKeyCredential && navigator.credentials) +// PublicKeyCredential is the WebAuthn-specific capability signal. Do not also gate the UI on +// navigator.credentials: some browsers expose WebAuthn while that generic Credential Management +// API check produces a false negative (notably Chrome on iOS). The real create/get calls still run +// only after the user chooses a passkey action and surface any genuine browser error there. +export const webauthnOK = () => typeof window.PublicKeyCredential !== 'undefined' export async function api(path, opts) { const r = await fetch(path, Object.assign({ headers: { 'Content-Type': 'application/json' } }, opts)) diff --git a/frontend/src/lib/api.test.js b/frontend/src/lib/api.test.js new file mode 100644 index 0000000..53360dc --- /dev/null +++ b/frontend/src/lib/api.test.js @@ -0,0 +1,35 @@ +// @vitest-environment happy-dom +import { afterEach, describe, expect, it } from 'vitest' +import { webauthnOK } from './api.js' + +const originalPublicKeyCredential = window.PublicKeyCredential +const originalCredentials = navigator.credentials + +function setCapability(target, property, value) { + Object.defineProperty(target, property, { configurable: true, value }) +} + +afterEach(() => { + setCapability(window, 'PublicKeyCredential', originalPublicKeyCredential) + setCapability(navigator, 'credentials', originalCredentials) +}) + +describe('webauthnOK', () => { + it('accepts WebAuthn when PublicKeyCredential is exposed', () => { + setCapability(window, 'PublicKeyCredential', class PublicKeyCredential {}) + setCapability(navigator, 'credentials', {}) + expect(webauthnOK()).toBe(true) + }) + + it('does not reject WebAuthn when the generic credentials check is unavailable', () => { + setCapability(window, 'PublicKeyCredential', class PublicKeyCredential {}) + setCapability(navigator, 'credentials', undefined) + expect(webauthnOK()).toBe(true) + }) + + it('rejects browsers without the WebAuthn credential type', () => { + setCapability(window, 'PublicKeyCredential', undefined) + setCapability(navigator, 'credentials', {}) + expect(webauthnOK()).toBe(false) + }) +})