From cbcccf34e89bb96b2f099892577f62dc8f040422 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 00:02:45 -0500 Subject: [PATCH] account: explicit setNativeBrowser hook for Capacitor login Native login now opens only the plugin the app passes in (Custom Tab / SFSafariViewController) and refuses rather than navigating the WebView. Test covers PKCE params and the dev.eom.wikideck redirect. Co-Authored-By: Claude Opus 5.5 --- server/account/README.md | 8 ++++++-- src/auth/session.ts | 23 ++++++++++++++++++----- tests/account-native.test.ts | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 tests/account-native.test.ts diff --git a/server/account/README.md b/server/account/README.md index de49bb7..af39324 100644 --- a/server/account/README.md +++ b/server/account/README.md @@ -49,10 +49,14 @@ Signed link message (off-chain, domain-tagged): - The UI is bundled locally, so set `globalThis.WIKIDECK_NODE_URL = 'http://192.168.1.32:3101'` before using `src/auth`. That node's POST origin check must allow the app's WebView origin. - Register the URL scheme `dev.eom.wikideck`. - Install `@capacitor/browser` and `@capacitor/app`. -- Add the listener: +- Wire the system browser and the redirect (for example in `src/native.ts`): ```ts - App.addListener('appUrlOpen', ({ url }) => { if (isCallback(url)) void completeLogin(url); }); + import { Browser } from '@capacitor/browser'; + import { completeLogin, isCallback, setNativeBrowser } from './auth/session.ts'; + setNativeBrowser(Browser); + onDeepLink(u => { if (isCallback(u.href)) void completeLogin(u.href); }); ``` + Without `setNativeBrowser`, native login refuses to open (it never falls back to the WebView). ## Known limits - **Logout is local.** Dex publishes no `end_session_endpoint`, so its SSO cookie remains. diff --git a/src/auth/session.ts b/src/auth/session.ts index 6de057d..811febd 100644 --- a/src/auth/session.ts +++ b/src/auth/session.ts @@ -44,8 +44,19 @@ export async function pkceChallenge(verifier: string): Promise { return b64url(new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)))); } -type CapacitorGlobal = { isNativePlatform?: () => boolean; Plugins?: { Browser?: { open(o: { url: string }): Promise; close?(): Promise } } }; +type NativeBrowser = { open(o: { url: string }): Promise; close?(): Promise }; +type CapacitorGlobal = { isNativePlatform?: () => boolean; Plugins?: { Browser?: NativeBrowser } }; const capacitor = () => (globalThis as { Capacitor?: CapacitorGlobal }).Capacitor; +let nativeBrowser: NativeBrowser | undefined; +/** + * Native apps: pass the imported `@capacitor/browser` plugin so login opens in the system + * browser (Custom Tab / SFSafariViewController), never inside the WebView. Example (src/native.ts): + * import { Browser } from '@capacitor/browser'; + * setNativeBrowser(Browser); + * onDeepLink(u => { if (isCallback(u.href)) void completeLogin(u.href); }); + */ +export function setNativeBrowser(browser: NativeBrowser): void { nativeBrowser = browser; } +const browserPlugin = () => nativeBrowser ?? capacitor()?.Plugins?.Browser; export const isNative = () => Boolean(capacitor()?.isNativePlatform?.()); export function redirectUri(): string { return isNative() ? NATIVE_REDIRECT : `${location.origin}${CALLBACK_PATH}`; } @@ -77,9 +88,11 @@ export async function login(returnTo = location.pathname + location.search): Pro scope: config.scopes, state: pending.state, nonce: pending.nonce, code_challenge: await pkceChallenge(pending.verifier), code_challenge_method: 'S256' }).toString(); // Capacitor: system browser (never a WebView) + custom-scheme redirect, per RFC 8252. - const browser = capacitor()?.Plugins?.Browser; - if (isNative() && browser) await browser.open({ url: url.toString() }); - else location.assign(url.toString()); + const browser = browserPlugin(); + if (isNative()) { + if (!browser) throw new Error('System browser unavailable: the app must call setNativeBrowser(Browser).'); + await browser.open({ url: url.toString() }); + } else location.assign(url.toString()); } export const isCallback = (url: string) => { @@ -101,7 +114,7 @@ export async function completeLogin(url: string): Promise<{ returnTo: string }> const s = await api('token', { method: 'POST', body: JSON.stringify({ code, codeVerifier: pending.verifier, redirectUri: pending.redirectUri, nonce: pending.nonce }) }); save(s); - if (isNative()) void capacitor()?.Plugins?.Browser?.close?.().catch(() => {}); + if (isNative()) void browserPlugin()?.close?.().catch(() => {}); return { returnTo: pending.returnTo }; } diff --git a/tests/account-native.test.ts b/tests/account-native.test.ts new file mode 100644 index 0000000..342065f --- /dev/null +++ b/tests/account-native.test.ts @@ -0,0 +1,33 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +// Capacitor: login must open the system browser with the custom-scheme redirect, never the WebView. +test('account native: login opens the system browser with PKCE and the app redirect', async () => { + const g = globalThis as Record; + const saved = { Capacitor: g.Capacitor, sessionStorage: g.sessionStorage, fetch: g.fetch, location: g.location }; + const store = new Map(); + g.sessionStorage = { getItem: (k: string) => store.get(k) ?? null, setItem: (k: string, v: string) => { store.set(k, v); }, + removeItem: (k: string) => { store.delete(k); } }; + g.Capacitor = { isNativePlatform: () => true, Plugins: {} }; + let assigned = ''; + g.location = { origin: 'https://localhost', pathname: '/', search: '', assign: (u: string) => { assigned = u; } }; + g.fetch = (async () => new Response(JSON.stringify({ issuer: 'https://dex.eom.dev/', clientId: 'wikideck', + scopes: 'openid profile email groups', authorizationEndpoint: 'https://dex.eom.dev/auth', + redirectUris: ['dev.eom.wikideck:/oauth/callback'] }), { headers: { 'content-type': 'application/json' } })) as typeof fetch; + try { + const auth = await import('../src/auth/session.ts'); + await assert.rejects(auth.login(), /setNativeBrowser/); + assert.equal(assigned, '', 'never navigates the WebView to Dex'); + const opened: string[] = []; + auth.setNativeBrowser({ open: async ({ url }) => { opened.push(url); } }); + await auth.login(); + const url = new URL(opened[0]); + assert.equal(url.origin + url.pathname, 'https://dex.eom.dev/auth'); + assert.equal(url.searchParams.get('redirect_uri'), 'dev.eom.wikideck:/oauth/callback'); + assert.equal(url.searchParams.get('code_challenge_method'), 'S256'); + assert.equal(url.searchParams.get('client_id'), 'wikideck'); + const pending = JSON.parse(store.get('wikideck.auth.pending')!); + assert.equal(url.searchParams.get('state'), pending.state); + assert.equal(auth.isCallback(`dev.eom.wikideck:/oauth/callback?code=c&state=${pending.state}`), true); + } finally { Object.assign(g, saved); } +});