Skip to content

Commit 2ae8ead

Browse files
fix(mosaic): support second-factor passkeys in reverification (#10570)
1 parent aa9a6a3 commit 2ae8ead

5 files changed

Lines changed: 99 additions & 46 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/mosaic': patch
3+
---
4+
5+
When users are asked to confirm their identity again, they can now use a passkey set up as their second step, as long as their browser supports passkeys.

‎packages/mosaic/src/features/reverification/__tests__/reverification.feature.test.tsx‎

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const secondPhoneFactor: SignInSecondFactorJSON = {
8282
phone_number_id: 'idn_phone',
8383
safe_identifier: '+1 555 0100',
8484
};
85+
const secondPasskeyFactor: SignInSecondFactorJSON = { strategy: 'passkey' };
8586

8687
const TITLE = 'Verification required';
8788
const NO_FACTORS_TITLE = 'Cannot verify your account';
@@ -497,9 +498,22 @@ describe('Reverification', () => {
497498
expect(await screen.findByText(STEP.backupCode)).toBeVisible();
498499
});
499500

500-
// This is a future behavior we will need to account for, here's the PR that builds it for the existing reverification: https://github.com/clerk/javascript/pull/9127
501-
it.todo('offer a passkey listed as a second factor', () => {
502-
// The passkey is not offered on the second factor step
501+
it('starts a supported second factor with passkey before code factors', async () => {
502+
await renderReverification(
503+
guardedAction({ level: 'second_factor', afterMinutes: 10 }),
504+
verificationSeed({ secondFactors: [totpFactor, secondPasskeyFactor, secondPhoneFactor] }),
505+
);
506+
const restore = pretendToBeARealBrowser();
507+
508+
try {
509+
const user = await startVerification();
510+
expect(await screen.findByText(STEP.passkey)).toBeVisible();
511+
await useAnotherMethod(user);
512+
expect(await screen.findByRole('button', { name: 'Use your authenticator app' })).toBeVisible();
513+
expect(screen.getByRole('button', { name: 'Send SMS code to +1 555 0100' })).toBeVisible();
514+
} finally {
515+
restore();
516+
}
503517
});
504518
});
505519

@@ -789,6 +803,30 @@ describe('Reverification', () => {
789803
await pasteCode(user, CODE);
790804
await untilResolved();
791805
});
806+
807+
it('offers an advertised passkey after the first factor of a multi-factor request', async () => {
808+
const { action, clerk } = await renderReverification(
809+
guardedAction({ level: 'multi_factor', afterMinutes: 10 }),
810+
verificationSeed({ firstFactors: [passwordFactor], secondFactors: [totpFactor, secondPasskeyFactor] }),
811+
);
812+
clerk.__internal_getPublicCredentials = vi.fn().mockResolvedValue({
813+
publicKeyCredential: fakeAssertion(),
814+
error: null,
815+
});
816+
const restore = pretendToBeARealBrowser();
817+
818+
try {
819+
const user = await startVerification();
820+
expect(await screen.findByText(STEP.password)).toBeVisible();
821+
await user.type(passwordField(), `${PASSWORD}{Enter}`);
822+
expect(await screen.findByText(STEP.passkey)).toBeVisible();
823+
await user.click(screen.getByRole('button', { name: 'Continue' }));
824+
await untilResolved();
825+
expect(action).toHaveBeenCalledTimes(2);
826+
} finally {
827+
restore();
828+
}
829+
});
792830
});
793831

794832
describe('second factor alternatives', () => {
@@ -838,12 +876,36 @@ describe('Reverification', () => {
838876
});
839877
});
840878

841-
describe('passkey', () => {
879+
describe.each([
880+
{
881+
stage: 'first factor',
882+
action: () => guardedAction(),
883+
seed: { firstFactors: [passwordFactor, passkeyFactor] },
884+
verifiedOn: 'firstFactorVerification' as const,
885+
skippedOn: 'secondFactorVerification' as const,
886+
otherMethod: { name: 'Continue with your password', step: STEP.password },
887+
withoutWebAuthn: {
888+
seed: { firstFactors: [passwordFactor, passkeyFactor, emailFactor] },
889+
step: STEP.password,
890+
alternative: 'Email code to alice@example.com',
891+
},
892+
},
893+
{
894+
stage: 'second factor',
895+
action: () => guardedAction({ level: 'second_factor', afterMinutes: 10 }),
896+
seed: { secondFactors: [totpFactor, secondPasskeyFactor] },
897+
verifiedOn: 'secondFactorVerification' as const,
898+
skippedOn: 'firstFactorVerification' as const,
899+
otherMethod: { name: 'Use your authenticator app', step: STEP.totp },
900+
withoutWebAuthn: {
901+
seed: { secondFactors: [totpFactor, secondPasskeyFactor, backupCodeFactor] },
902+
step: STEP.totp,
903+
alternative: 'Use a backup code',
904+
},
905+
},
906+
])('passkey as the $stage', ({ action, seed, verifiedOn, skippedOn, otherMethod, withoutWebAuthn }) => {
842907
it('completes the challenge with the passkey', async () => {
843-
const { clerk } = await renderReverification(
844-
guardedAction(),
845-
verificationSeed({ firstFactors: [passwordFactor, passkeyFactor] }),
846-
);
908+
const { clerk, fapi } = await renderReverification(action(), verificationSeed(seed));
847909
let acceptCredential: () => void = () => {};
848910
const getCredential = vi
849911
.fn()
@@ -882,16 +944,15 @@ describe('Reverification', () => {
882944
await waitFor(() => expect(getCredential).toHaveBeenCalledTimes(2));
883945
acceptCredential();
884946
await untilResolved();
947+
expect(fapi.verification[verifiedOn]?.strategy).toBe('passkey');
948+
expect(fapi.verification[skippedOn]).toBeNull();
885949
} finally {
886950
restore();
887951
}
888952
});
889953

890954
it('shows no error when the passkey prompt is aborted', async () => {
891-
const { clerk } = await renderReverification(
892-
guardedAction(),
893-
verificationSeed({ firstFactors: [passwordFactor, passkeyFactor] }),
894-
);
955+
const { clerk } = await renderReverification(action(), verificationSeed(seed));
895956
const getCredential = vi.fn().mockResolvedValue({
896957
publicKeyCredential: null,
897958
error: new ClerkWebAuthnError('The operation was aborted', { code: 'passkey_operation_aborted' }),
@@ -917,10 +978,7 @@ describe('Reverification', () => {
917978
});
918979

919980
it('clears an earlier passkey error when the next prompt is aborted', async () => {
920-
const { clerk } = await renderReverification(
921-
guardedAction(),
922-
verificationSeed({ firstFactors: [passwordFactor, passkeyFactor] }),
923-
);
981+
const { clerk } = await renderReverification(action(), verificationSeed(seed));
924982
const getCredential = vi
925983
.fn()
926984
.mockResolvedValueOnce({
@@ -955,15 +1013,15 @@ describe('Reverification', () => {
9551013
});
9561014

9571015
it('offers the passkey from the picker when another method is showing', async () => {
958-
await renderReverification(guardedAction(), verificationSeed({ firstFactors: [passwordFactor, passkeyFactor] }));
1016+
await renderReverification(action(), verificationSeed(seed));
9591017
const restore = pretendToBeARealBrowser();
9601018

9611019
try {
9621020
const user = await startVerification();
9631021
expect(await screen.findByText(STEP.passkey)).toBeVisible();
9641022
await useAnotherMethod(user);
965-
await user.click(await screen.findByRole('button', { name: 'Continue with your password' }));
966-
expect(await screen.findByText(STEP.password)).toBeVisible();
1023+
await user.click(await screen.findByRole('button', { name: otherMethod.name }));
1024+
expect(await screen.findByText(otherMethod.step)).toBeVisible();
9671025

9681026
// The passkey is listed once the user is on another method
9691027
await useAnotherMethod(user);
@@ -975,20 +1033,17 @@ describe('Reverification', () => {
9751033
});
9761034

9771035
it('does not offer a passkey when the browser does not support WebAuthn', async () => {
978-
await renderReverification(
979-
guardedAction(),
980-
verificationSeed({ firstFactors: [passwordFactor, passkeyFactor, emailFactor] }),
981-
);
1036+
await renderReverification(action(), verificationSeed(withoutWebAuthn.seed));
9821037
const restore = pretendWebAuthnIsUnsupported();
9831038

9841039
try {
9851040
// The passkey is not the starting method
9861041
const user = await startVerification();
987-
expect(await screen.findByText(STEP.password)).toBeVisible();
1042+
expect(await screen.findByText(withoutWebAuthn.step)).toBeVisible();
9881043

9891044
// The passkey is not listed in the picker
9901045
await useAnotherMethod(user);
991-
expect(await screen.findByRole('button', { name: 'Email code to alice@example.com' })).toBeVisible();
1046+
expect(await screen.findByRole('button', { name: withoutWebAuthn.alternative })).toBeVisible();
9921047
expect(screen.queryByRole('button', { name: 'Use your passkey' })).toBeNull();
9931048
} finally {
9941049
restore();

‎packages/mosaic/src/features/reverification/reverification.model.tsx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ function toMethod(
4040
webAuthnSupported: boolean,
4141
): ReverificationMethod | null {
4242
if (factor.strategy === 'passkey') {
43-
if (stage !== 'first' || !webAuthnSupported) {
43+
if (!webAuthnSupported) {
4444
return null;
4545
}
46-
return { id: 'passkey', stage: 'first', strategy: 'passkey' };
46+
return { id: 'passkey', stage, strategy: 'passkey' };
4747
}
4848

4949
if (factor.strategy === 'password') {
@@ -104,7 +104,7 @@ function toResult(
104104
return {
105105
status: resource.status,
106106
methods,
107-
startingMethod: pickStartingMethod(methods, preferredSignInStrategy, webAuthnSupported),
107+
startingMethod: pickStartingMethod(methods, preferredSignInStrategy),
108108
};
109109
}
110110

@@ -183,7 +183,9 @@ export function useReverificationModel(reverificationState: ReverificationState)
183183
await session.attemptSecondFactorVerification({ strategy: method.strategy, code: value }),
184184
);
185185
case 'passkey':
186-
return handleResponse(await session.verifyWithPasskey());
186+
return handleResponse(
187+
await session.verifyWithPasskey({ level: method.stage === 'second' ? 'second_factor' : 'first_factor' }),
188+
);
187189
}
188190
},
189191
finish: async () => {

‎packages/mosaic/src/features/reverification/reverification.types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export type ReverificationErrorReason = 'noFactors' | 'generic';
1313
export type ReverificationStep = 'password' | 'passkey' | 'otp' | 'backup-code' | 'method-picker' | 'help';
1414

1515
export type ReverificationMethod =
16-
| { id: string; stage: 'first'; strategy: 'password' | 'passkey' }
16+
| { id: string; stage: 'first'; strategy: 'password' }
17+
| { id: string; stage: 'first' | 'second'; strategy: 'passkey' }
1718
| { id: string; stage: 'first'; strategy: 'email_code'; emailAddressId: string; identifier: string }
1819
| { id: string; stage: 'first' | 'second'; strategy: 'phone_code'; phoneNumberId: string; identifier: string }
1920
| { id: string; stage: 'second'; strategy: 'totp' | 'backup_code' };

‎packages/mosaic/src/features/reverification/reverification.utils.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,7 @@ export function needsPrepare(method: ReverificationMethod): method is Reverifica
2727
function pickStartingFirstFactor(
2828
methods: readonly ReverificationMethod[],
2929
preferredSignInStrategy: PreferredSignInStrategy | undefined,
30-
webAuthnSupported: boolean,
3130
): ReverificationMethod | null {
32-
if (methods.length === 0) {
33-
return null;
34-
}
35-
36-
if (webAuthnSupported) {
37-
const passkey = methods.find(method => method.strategy === 'passkey');
38-
if (passkey) {
39-
return passkey;
40-
}
41-
}
42-
4331
if (preferredSignInStrategy === 'password') {
4432
return methods.find(method => method.strategy === 'password') ?? methods[0] ?? null;
4533
}
@@ -61,9 +49,11 @@ function pickStartingSecondFactor(methods: readonly ReverificationMethod[]): Rev
6149
export function pickStartingMethod(
6250
methods: readonly ReverificationMethod[],
6351
preferredSignInStrategy: PreferredSignInStrategy | undefined,
64-
webAuthnSupported: boolean,
6552
): ReverificationMethod | null {
66-
return methods[0]?.stage === 'second'
67-
? pickStartingSecondFactor(methods)
68-
: pickStartingFirstFactor(methods, preferredSignInStrategy, webAuthnSupported);
53+
return (
54+
methods.find(method => method.strategy === 'passkey') ??
55+
(methods[0]?.stage === 'second'
56+
? pickStartingSecondFactor(methods)
57+
: pickStartingFirstFactor(methods, preferredSignInStrategy))
58+
);
6959
}

0 commit comments

Comments
 (0)