mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
fix(native): the handoff's passkey sign-in lives in native/browserSignIn, so the page hook reaches no API on its own
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { sha256 } from '@noble/hashes/sha2';
|
||||
import { bytesToHex } from '@noble/hashes/utils';
|
||||
import { startAuthentication } from '@simplewebauthn/browser';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { authApi } from '../api/client';
|
||||
import { nativeAuthApi } from '../api/nativeAuth';
|
||||
import {
|
||||
BrowserSignInCancelled,
|
||||
@@ -9,11 +11,14 @@ import {
|
||||
rememberHandoffReturn,
|
||||
requestHandoffCode,
|
||||
signInThroughBrowser,
|
||||
signInWithPasskey,
|
||||
takeHandoffReturn,
|
||||
} from './browserSignIn';
|
||||
import { TrekShell } from './trekShell';
|
||||
|
||||
vi.mock('./trekShell', () => ({ APP_URL_SCHEME: 'com.liketrek.trek', TrekShell: { authenticate: vi.fn() } }));
|
||||
vi.mock('@simplewebauthn/browser', () => ({ startAuthentication: vi.fn() }));
|
||||
vi.mock('../api/client', () => ({ authApi: { passkey: { loginOptions: vi.fn(), loginVerify: vi.fn() } } }));
|
||||
vi.mock('../api/nativeAuth', () => ({ nativeAuthApi: { exchange: vi.fn(), handoff: vi.fn() } }));
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -110,3 +115,14 @@ describe('the remembered handoff return', () => {
|
||||
expect(takeHandoffReturn()).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('signInWithPasskey', () => {
|
||||
it('asks for the passkey and has the server verify it', async () => {
|
||||
vi.mocked(authApi.passkey.loginOptions).mockResolvedValue({ challenge: 'x' });
|
||||
vi.mocked(startAuthentication).mockResolvedValue({ id: 'cred' } as never);
|
||||
vi.mocked(authApi.passkey.loginVerify).mockResolvedValue({ token: '', user: {} });
|
||||
await signInWithPasskey();
|
||||
expect(startAuthentication).toHaveBeenCalledWith({ optionsJSON: { challenge: 'x' } });
|
||||
expect(authApi.passkey.loginVerify).toHaveBeenCalledWith({ id: 'cred' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { sha256 } from '@noble/hashes/sha2';
|
||||
import { bytesToHex } from '@noble/hashes/utils';
|
||||
import { startAuthentication } from '@simplewebauthn/browser';
|
||||
import { authApi } from '../api/client';
|
||||
import { nativeAuthApi } from '../api/nativeAuth';
|
||||
import { APP_URL_SCHEME, TrekShell } from './trekShell';
|
||||
|
||||
@@ -76,6 +78,13 @@ export async function requestHandoffCode(challenge: string): Promise<string> {
|
||||
return code;
|
||||
}
|
||||
|
||||
/** The browser side of a passkey sign-in the app started: the prompt, then the session cookie. */
|
||||
export async function signInWithPasskey(): Promise<void> {
|
||||
const options = await authApi.passkey.loginOptions();
|
||||
const assertion = await startAuthentication({ optionsJSON: options });
|
||||
await authApi.passkey.loginVerify(assertion);
|
||||
}
|
||||
|
||||
export class BrowserSignInCancelled extends Error {
|
||||
constructor() {
|
||||
super('Sign-in cancelled');
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// FE-NATIVE-HANDOFF-001 to FE-NATIVE-HANDOFF-011
|
||||
import { startAuthentication } from '@simplewebauthn/browser';
|
||||
import React from 'react';
|
||||
import { MemoryRouter } from 'react-router';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { act, renderHook, waitFor } from '../../../tests/helpers/render';
|
||||
import { resetAllStores } from '../../../tests/helpers/store';
|
||||
import { authApi } from '../../api/client';
|
||||
import { rememberHandoffReturn, requestHandoffCode } from '../../native/browserSignIn';
|
||||
import { rememberHandoffReturn, requestHandoffCode, signInWithPasskey } from '../../native/browserSignIn';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useNativeHandoff } from './useNativeHandoff';
|
||||
|
||||
@@ -19,17 +17,9 @@ vi.mock('../../native/browserSignIn', () => ({
|
||||
HANDOFF_PATH: '/native-handoff',
|
||||
rememberHandoffReturn: vi.fn(),
|
||||
requestHandoffCode: vi.fn(),
|
||||
signInWithPasskey: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@simplewebauthn/browser', () => ({ startAuthentication: vi.fn() }));
|
||||
vi.mock('../../api/client', async () => {
|
||||
const actual = await vi.importActual<typeof import('../../api/client')>('../../api/client');
|
||||
return {
|
||||
...actual,
|
||||
authApi: { ...actual.authApi, passkey: { ...actual.authApi.passkey, loginOptions: vi.fn(), loginVerify: vi.fn() } },
|
||||
};
|
||||
});
|
||||
|
||||
const challenge = 'ab'.repeat(32);
|
||||
const realLocation = window.location;
|
||||
const logout = vi.fn(async () => {});
|
||||
@@ -155,11 +145,7 @@ describe('useNativeHandoff', () => {
|
||||
setSearch(`?challenge=${challenge}&start=passkey`);
|
||||
useAuthStore.setState({ isAuthenticated: false, loadUser });
|
||||
loadUser.mockClear();
|
||||
vi.mocked(authApi.passkey.loginOptions).mockReset().mockResolvedValue({ challenge: 'x' });
|
||||
vi.mocked(authApi.passkey.loginVerify).mockReset().mockResolvedValue({ token: '', user: {} });
|
||||
vi.mocked(startAuthentication)
|
||||
.mockReset()
|
||||
.mockResolvedValue({ id: 'cred' } as never);
|
||||
vi.mocked(signInWithPasskey).mockReset().mockResolvedValue();
|
||||
});
|
||||
|
||||
it('FE-NATIVE-HANDOFF-009: asks for the passkey itself instead of sending the user to the login', () => {
|
||||
@@ -174,14 +160,14 @@ describe('useNativeHandoff', () => {
|
||||
await act(async () => {
|
||||
await result.current.continueWithPasskey();
|
||||
});
|
||||
expect(authApi.passkey.loginVerify).toHaveBeenCalledWith({ id: 'cred' });
|
||||
expect(signInWithPasskey).toHaveBeenCalled();
|
||||
expect(loadUser).toHaveBeenCalled();
|
||||
expect(requestHandoffCode).toHaveBeenCalledWith(challenge);
|
||||
expect(window.location.href).toBe('com.liketrek.trek://auth?code=c1');
|
||||
});
|
||||
|
||||
it('FE-NATIVE-HANDOFF-011: stays quiet when the prompt is dismissed and offers the full login on request', async () => {
|
||||
vi.mocked(startAuthentication).mockRejectedValue(Object.assign(new Error('no'), { name: 'NotAllowedError' }));
|
||||
vi.mocked(signInWithPasskey).mockRejectedValue(Object.assign(new Error('no'), { name: 'NotAllowedError' }));
|
||||
const { result } = renderHandoff();
|
||||
await act(async () => {
|
||||
await result.current.continueWithPasskey();
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { startAuthentication } from '@simplewebauthn/browser';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useNavigate } from 'react-router';
|
||||
import { useShallow } from 'zustand/react/shallow';
|
||||
import { authApi } from '../../api/client';
|
||||
import { HANDOFF_PATH, rememberHandoffReturn, requestHandoffCode } from '../../native/browserSignIn';
|
||||
import { HANDOFF_PATH, rememberHandoffReturn, requestHandoffCode, signInWithPasskey } from '../../native/browserSignIn';
|
||||
import { APP_URL_SCHEME } from '../../native/trekShell';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
|
||||
@@ -65,9 +63,7 @@ export function useNativeHandoff() {
|
||||
const continueWithPasskey = async (): Promise<void> => {
|
||||
setStatus('sending');
|
||||
try {
|
||||
const options = await authApi.passkey.loginOptions();
|
||||
const assertion = await startAuthentication({ optionsJSON: options });
|
||||
await authApi.passkey.loginVerify(assertion);
|
||||
await signInWithPasskey();
|
||||
await loadUser({ silent: true });
|
||||
} catch (err: unknown) {
|
||||
const name = (err as { name?: string })?.name;
|
||||
|
||||
Reference in New Issue
Block a user