Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/core/auth-js/src/lib/webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,15 @@ export class WebAuthnApi {
await this.client.mfa
.listFactors()
.then((factors) =>
// Only an unverified factor is leftover from the enrollment that just
// failed, and removing it is what lets a retry with the same name
// succeed. A verified factor of that name is a passkey the user is
// already using, so it must never be selected here.
factors.data?.all.find(
(v) =>
v.factor_type === 'webauthn' &&
v.friendly_name === friendlyName &&
v.status !== 'unverified'
v.status === 'unverified'
)
)
.then((factor) => (factor ? this.client.mfa.unenroll({ factorId: factor?.id }) : void 0))
Expand Down
74 changes: 74 additions & 0 deletions packages/core/auth-js/test/passkey.methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,77 @@ describe('admin.passkey', () => {
expect((error as AuthApiError).code).toEqual('passkey_not_found')
})
})

describe('mfa.webauthn.register cleanup after a failed enrollment', () => {
let browser: ReturnType<typeof setupBrowserEnvironment>

beforeEach(() => {
browser = setupBrowserEnvironment()
})

afterEach(() => {
browser.restore()
jest.restoreAllMocks()
})

const existingFactor = (status: string, id: string) => ({
id,
factor_type: 'webauthn',
friendly_name: 'My YubiKey',
status,
created_at: '2026-01-01T00:00:00.000Z',
updated_at: '2026-01-01T00:00:00.000Z',
})

const registerAgainstFailedEnroll = async (existing: ReturnType<typeof existingFactor>[]) => {
const { client, mockFetch } = await createPasskeyClient({ withSession: true })
mockFetch.mockResolvedValueOnce(apiErrorResponse('enrollment failed', 422))

jest.spyOn(client.mfa, 'listFactors').mockResolvedValue({
data: { all: existing, totp: [], phone: [] },
error: null,
} as any)
const unenroll = jest
.spyOn(client.mfa, 'unenroll')
.mockResolvedValue({ data: null, error: null } as any)

await client.mfa.webauthn.register({
friendlyName: 'My YubiKey',
webauthn: { rpId: 'localhost' },
})

return unenroll
}

it('leaves an existing verified factor of the same name alone', async () => {
const unenroll = await registerAgainstFailedEnroll([
existingFactor('verified', 'verified-factor-id'),
])

expect(unenroll).not.toHaveBeenCalled()
})

it('removes the leftover unverified factor', async () => {
const unenroll = await registerAgainstFailedEnroll([
existingFactor('unverified', 'unverified-factor-id'),
])

expect(unenroll).toHaveBeenCalledWith({ factorId: 'unverified-factor-id' })
})

it('removes only the unverified one when both share a name', async () => {
const unenroll = await registerAgainstFailedEnroll([
existingFactor('verified', 'verified-factor-id'),
existingFactor('unverified', 'unverified-factor-id'),
])

expect(unenroll).toHaveBeenCalledTimes(1)
expect(unenroll).toHaveBeenCalledWith({ factorId: 'unverified-factor-id' })
})

it('does nothing when no factor of that name exists', async () => {
const unenroll = await registerAgainstFailedEnroll([])

expect(unenroll).not.toHaveBeenCalled()
})
})
Loading